import time
def new_id():
time.sleep(0.000001)
return time.time()
On my system, time.time() seems to offer 6 significant figures after the decimal point. With a brief sleep it should be guaranteed unique with at least a moderate amount of randomness down in the last two or three digits.
import time
import random
import socket
import hashlib
def guid( *args ):
"""
Generates a universally unique ID.
Any arguments only create more randomness.
"""
t = long( time.time() * 1000 )
r = long( random.random()*100000000000000000L )
try:
a = socket.gethostbyname( socket.gethostname() )
except:
# if we can't get a network address, just imagine one
a = random.random()*100000000000000000L
data = str(t)+' '+str(r)+' '+str(a)+' '+str(args)
data = hashlib.md5(data).hexdigest()
return data
On my system, time.time() seems to offer 6 significant figures after the decimal point. With a brief sleep it should be guaranteed unique with at least a moderate amount of randomness down in the last two or three digits.
You could hash it as well if you're worried.
You might want Python's UUID functions:
21.15. uuid — UUID objects according to RFC 4122
eg:
Perhaps
uuid.uuid4()
might do the job. See uuid for more information.unique and random are mutually exclusive. perhaps you want this?
Usage:
no two returned id is the same (Unique) and this is based on a randomized seed value
Maybe the uuid module?