[nycphp-talk] generate random unique 8-digit number
Edward Potter
edwardpotter at gmail.com
Tue Mar 10 12:31:59 EDT 2009
Even the numbers game is not random. Some lotto numbers have come up 10X
then others. It's the ink on the ball. Shifts the weight.
But back to reality, you can get a pretty decent random number, for all
practical purposes using the tips covers in this discussion.
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...
>
> It is not possible to produce a fixed-length number/string which will
> _never_ repeat, simply because you will run out of combinations
> eventually. Using 'random' values just means that you can't take the
> previous number and add one to determine the next unused number.
>
> If you're trying to generate relatively short pseudo-random IDs to
> thwart guesses, then the simplest method is going to be the
> previously-recommended approach of generating a random number and
> checking if it has already been used, rinse and repeat until you find
> one that has not. This will get progressively slower as the number of
> IDs generated grows, but is the only way you can be 100% sure of a truly
> random ID which will never conflict.
>
> If you go with a scheme like uuids then you can be sufficiently
> confident (but never 100% sure) that they are not going to repeat.
> However, if you try using some scheme to hash them down to 8 digits all
> bets are off as you're going from 16^32 down to 10^8 combinations.
>
> Dan
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php
>
--
IM/iChat: ejpusa
Links: http://del.icio.us/ejpusa
Blog: http://www.preceptress.com/blog
Follow me: http://www.twitter.com/ejpusa
Karma: http://www.coderswithconscience.com
Projects: http://flickr.com/photos/86842405@N00/
Store: http://astore.amazon.com/httpwwwutopic-20
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20090310/d4904379/attachment.html>
More information about the talk
mailing list