| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
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:
__________________
If you found a post of mine helpful, please click on the on my post to add to my reputation.
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
Quote:
Well thats not exactly what I'd call "random" though |
|
#4
|
||||
|
||||
|
Quote:
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. |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
||||
|
||||
|
Quote:
Could you explain this a little bit more. I'm unfamiliar with this object and what guid is. sounds cool! |
|
#9
|
||||
|
||||
|
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 |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Unique, random number generator |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|