[Q]:Generate Unique ID's

Paul Rubin http
Fri May 23 17:12:12 EDT 2003


achrist at easystreet.com writes:
> Is there anything in the standard Python libs to generate an ID
> like those that Windows uses, which are guaranteed to be unique
> over all instances of everything, forever, no exceptions?  How 
> about an easy call to Windows (if I don't care about cross-platform)
> to have it generate one for me?  As this might happen every time
> a user presses enter, maybe even several times on some updates, 
> it would be nice if it never took more than (e.g.) 0.1 secs or so.

The best way to generate a unique ID is to use a high-entropy random
number that's long enough to make collisions highly unlikely, i.e. 128
bits or longer.  Any attempts to use stuff like increasing sequences,
the date, the ethernet MAC address etc. are much more failure prone.
MAC's themselves are supposed to be examples of unique ID's generated
by handing out blocks of them to ethernet vendors and asking them to
assign MAC's uniquely (e.g. in increasing sequence) but occasionally
in practice, you'll find two ethernet cards with the same MAC.

Unfortunately there's no good way to get good random numbers with
windows and just the standard python libs.  There's a nonstandard lib
that reads a random number from the Windows crypto API random number
generator.  That's what I'd use.




More information about the Python-list mailing list