Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 23rd, 2006, 10:13 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
Unique, random number generator

I hestitate to say never but I think that this function might never produce the same unique number. I mean 0%, not just a very small likelihood. If you know of a way to improve this, please post updates.

asp Code:
Original - asp Code
  1. Function getRandomnum()
  2. Randomize
  3. dim randomVar1, randomVar2, randomVar3, currDate, ip
  4.  
  5. if Request.ServerVariables("HTTP_X_FORWARDED_FOR") <> "" then
  6.  ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
  7. else
  8.  ip = Request.ServerVariables("REMOTE_ADDR")
  9. end if
  10.  
  11. ip = Replace(ip, ".", "")
  12.  
  13. currDate = Replace(Now(), "/", "")
  14. currDate = Replace(currDate, " ", "")
  15. currDate = Replace(currDate, ":", "")
  16. currDate = Replace(currDate, "AM", "")
  17. currDate = Replace(currDate, "PM", "")
  18.  
  19. randomVar1 = (Rnd() * 1000000)
  20. randomVar2 = Sqr(randomVar1)
  21. randomVar3 = Round(1000000 * (randomVar1 * randomVar2))
  22.  
  23. getRandomnum = (currDate & randomVar3 & ip)
  24. End Function
__________________
If you found a post of mine helpful, please click on the on my post to add to my reputation.


Reply With Quote
  #2  
Old March 24th, 2006, 02:19 PM
blicci's Avatar
blicci blicci is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Philly
Posts: 351 blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 5 sec
Reputation Power: 52
Look at the size of the number you are returning. You can run it for a very long time and never hit the same number twice but you will eventually. It may take a while but you will. There is no such thing as a true random number generator in computers. If there is any logic behind it them it will repeat. Take slot machines for instance. They use a "random" number generator but eventually the 3 cherries will come out again and again.

Plus I can produce a "random" number that will never repeat in one line of code without any bounds

MONTH(NOW()) & DAY(NOW()) & YEAR(NOW()) & HOUR(NOW()) & MINUTE(NOW()) & SECOND(NOW())
you see it will never be 324200632651 ever again in time.

Last edited by blicci : March 24th, 2006 at 02:27 PM.

Reply With Quote
  #3  
Old March 24th, 2006, 02:39 PM
Black&White's Avatar
Black&White Black&White is offline
Play up Pompey!
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Feb 2005
Location: Hampshire, ENG
Posts: 1,100 Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)Black&White User rank is Second Lieutenant (5000 - 10000 Reputation Level)  Folding Points: 9032 Folding Title: Novice Folder
Time spent in forums: 1 Week 1 Day 22 h 26 m 43 sec
Reputation Power: 62
Quote:
Originally Posted by blicci
Plus I can produce a "random" number that will never repeat in one line of code without any bounds

MONTH(NOW()) & DAY(NOW()) & YEAR(NOW()) & HOUR(NOW()) & MINUTE(NOW()) & SECOND(NOW())
you see it will never be 324200632651 ever again in time.


Well thats not exactly what I'd call "random" though
__________________
Matt

Folding At Home - fold to cure!

Reply With Quote
  #4  
Old March 24th, 2006, 03:01 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
Quote:
Originally Posted by blicci
Look at the size of the number you are returning. You can run it for a very long time and never hit the same number twice but you will eventually. It may take a while but you will. There is no such thing as a true random number generator in computers. If there is any logic behind it them it will repeat. Take slot machines for instance. They use a "random" number generator but eventually the 3 cherries will come out again and again.

Plus I can produce a "random" number that will never repeat in one line of code without any bounds

MONTH(NOW()) & DAY(NOW()) & YEAR(NOW()) & HOUR(NOW()) & MINUTE(NOW()) & SECOND(NOW())
you see it will never be 324200632651 ever again in time.


This isn't random at all. Suppose you have 1000 hits/second. You're going to have 1000 of the exact same number. I think the one I posed is about as close to random as possible. For it to not be random it would have to have the same person with the same IP address submit the form over 1 million times per second. There is a 1/9999999 chance that if the person submits the form twice in one second from the same IP address would get the same number. That's good odds.

Reply With Quote
  #5  
Old March 24th, 2006, 03:31 PM
blicci's Avatar
blicci blicci is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Philly
Posts: 351 blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level)blicci User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 5 sec
Reputation Power: 52
fine, multiply it by my IP address and it is just as random as yours is.
My point is that there is no such thing as a computer generated random number.
I certainly wouldn't use either solution to generate a unique index in my database.

Reply With Quote
  #6  
Old March 24th, 2006, 03:45 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
Notice the 9+ digit random number generated at the end of the time and IP address.

If you are ok with your 1/9999999 odds that this could happen if the SAME computer can submit the form TWICE in 1 second then I think you could use this in your database and after a 10000 years you would find that the same number never appears. Pretty good odds huh.

Reply With Quote
  #7  
Old March 25th, 2006, 08:23 AM
elijathegold's Avatar
elijathegold elijathegold is offline
Senior Fire Wizard
Click here for more information
 
Join Date: Feb 2005
Location: Ashford, Kent. England
Posts: 5,651 elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 2 Months 2 Weeks 2 Days 3 h 59 m 9 sec
Reputation Power: 301
You could always try this

Code:
<%guid = server.createobject("scriptlet.typelib").guid %>


Not bad for a one liner

Although it is not really random, it should be unique
__________________
And he picked it all up... in his pick-up.





Friends of Shemzilla

Reply With Quote
  #8  
Old March 25th, 2006, 11:15 AM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 34
Send a message via AIM to shamrog12
Quote:
Originally Posted by elijathegold
You could always try this

Code:
<%guid = server.createobject("scriptlet.typelib").guid %>


Not bad for a one liner

Although it is not really random, it should be unique


Could you explain this a little bit more. I'm unfamiliar with this object and what guid is. sounds cool!

Reply With Quote
  #9  
Old March 25th, 2006, 11:47 AM
elijathegold's Avatar
elijathegold elijathegold is offline
Senior Fire Wizard
Click here for more information
 
Join Date: Feb 2005
Location: Ashford, Kent. England
Posts: 5,651 elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)elijathegold User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1594273 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 2 Months 2 Weeks 2 Days 3 h 59 m 9 sec
Reputation Power: 301
A GUID is a Globally Unique IDentifier. An algorythm is run against
the current date / time and processor id to generate the identifier
you see.

A good starting place for more info

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Unique, random number generator


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |