Generating unique numbers?

Erik Max Francis max at alcyone.com
Fri May 31 19:16:27 EDT 2002


VanL wrote:

> Is there a good algorithm for generating unique numbers
> (ints, specifically) that can be used as object identifiers?
> 
> I am discounding the obvious newid += 1 because I will need
> to merge different sets, and while I may have some number
> overlap, using a simple count for each object would
> guarantee that I have the maximum number of clashes.
> 
> Specifically, I will be using these numbers as dictionary keys.

It really depends on what you need them for and how devastating a
collision is.  If they're, say, being generated by different contexts
(say, servers) the you can assign each context a number and have each
server increment sequentially when creating IDs, and combine them in
some reasonable way (e.g., (contextID << 24) | localID).

If there are an arbitrary number of contexts, then you're probably
better off with an ID being, say, a string, or at least a 2-tuple of the
context name and the local ID.  If you're interested in obscurity, then
you can run it through an md5 hash.  If you just need to spout off
unique IDs at random, then something like an md5 hash of the machine
name, process ID, time, and anything else you can think of might be in
order.

In the general case with no further clarifications and only 32 bits, you
ultimately will have collisions, so the question really boils down to
how disastrous a collision would be and what you need to do in that
case.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Who'd ever think it / Such a squalid little ending
\__/ The American and Florence, _Chess_
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list