Generating Unique Keys

Skip Montanaro skip at pobox.com
Sat Jan 25 11:48:22 EST 2003


    Mongryong> Is there python library for generating unique keys on Linux?
    Mongryong> Something sort of like Window's HKEY generator.  Thanks.

Unique over what domain?  For all time?  For just this run?  For just this
computer?  Int, float, string?

I'll just toss something simple out there to stimulate your creative juices.
What about:

    "%s:%f:%f" % (socket.gethostname(), time.time(), random.random())

I realize it's not guaranteed to be completely unique, but for most uses it
will probably be "good enough".  In fact

    "%s:%f" % (socket.gethostname(), time.time())

will probably be "good enough" on systems with decent clocks:

    >>> time.time() == time.time()
    False

Skip





More information about the Python-list mailing list