Generating a unique identifier

Paul Rubin http
Sat Sep 8 03:32:31 CEST 2007


Ben Finney <bignose+hates-spam at benfinney.id.au> writes:
> >     http://docs.python.org/lib/module-uuid.html
> I second this recommendation. If you want arbitrary unique IDs that
> are not a function of the data they identify, the simplest solution is
> a monotonically-increasing serial number. If you want more than that,
> you might as well go straight to the standard UUIDs.

The standard UUID's as described in that url don't make any serious
attempt to be unguessable.  It's better to use a string from os.urandom().

To Stephen: double oops, I was thinking of hex digits rather than bytes
when I suggested reading 32 (length of an md5 hash) or 40 (length of
an sha1 hash) from os.urandom.  That should have said 16 or 20 bytes.

If k is the number of unique id's you'll actually use, and N is the
number of possible random uid's (so for 16 bytes, N=2**128 since
16 bytes is 128 bits), the probability of a collision occuring is
approximately exp(-k**2 / (2*N)), assuming k is small compared
with sqrt(N).



More information about the Python-list mailing list