[nycphp-talk] generate random unique 8-digit number
Chris Snyder
chsnyder at gmail.com
Tue Mar 10 13:57:42 EDT 2009
On Tue, Mar 10, 2009 at 11:54 AM, Dan Cech <dcech at phpwerx.net> wrote:
> Chris Snyder wrote:
>> Add a random number to a sufficiently deep timestamp (microseconds)
>> and you will have a non-repeating random number. But you can't shorten
>> it to 8 characters or whatever -- you have to keep the full timestamp
>> in order to maintain non-repeatability.
>
> Not really, if you add a random number then you can easily end up with 2
> results that are the same unless the frequency with which you generate
> numbers is less than the variation you're introducing, and at that point
> why bother with the random number at all...
Oh yeah, I meant "concatenate" not "add".
Doing something like $random = $microtime . "_" . rand( 0, 256 );
gives you non-repeating pseudo-randomness. Whether that's random
enough for some applications is another issue, but it won't repeat as
long as the timestamp used for $microtime doesn't roll over to 0.
More information about the talk
mailing list