unique number generator

Jeff Epler jepler at unpythonic.net
Wed May 19 12:28:30 EDT 2004


This is probably not easy to do, without more requirements.

UUIDs can be generated randomly, in which case about 120 of the 128 bits
may vary.  This means that you wouldn't expect to generate two that are
identical before about 2^60 are generated, which is enough to make most
people comfortable.

8-digit numbers, well, they're shorter.  You'd expect to have a
collision after about 10,000 "unique" numbers are generated randomly.
This isn't very many!

UUIDs can be generated using partly a number which should be unique to
each machine, plus some other factors.  You could consider doing this,
giving each machine a unique prefix and generating the suffixes randomly
or sequentially.  For example, if you have 100 of fewer hosts, you give
them the 2-digit prefixes 00 through 99, and let them generate IDs by
choosing the final 6 digits.  If the machine does so randomly, you'll
expect a collision after about sqrt(10e6) ~ 3000 IDs per machine, and
if it is done sequentially on each machine then you can use all 10e6 IDs
on each machine.  3000 and 10e6 are both pretty small, though.

There's a reason that UUIDs are large numbers, unless your system is
guaranteed to be very small, narrow "unique numbers" will fail, and if
your system is small you might as well allocate them manually, or
automatically but sequentially from some "master" source.

Jeff




More information about the Python-list mailing list