How can I generate a unique ID in Python? [duplicate] How can I generate a unique ID in Python? [duplicate] python python

How can I generate a unique ID in Python? [duplicate]


Perhaps uuid.uuid4() might do the job. See uuid for more information.


You might want Python's UUID functions:

21.15. uuid — UUID objects according to RFC 4122

eg:

import uuidprint uuid.uuid4()

7d529dd4-548b-4258-aa8e-23e34dc8d43d


unique and random are mutually exclusive. perhaps you want this?

import randomdef uniqueid():    seed = random.getrandbits(32)    while True:       yield seed       seed += 1

Usage:

unique_sequence = uniqueid()id1 = next(unique_sequence)id2 = next(unique_sequence)id3 = next(unique_sequence)ids = list(itertools.islice(unique_sequence, 1000))

no two returned id is the same (Unique) and this is based on a randomized seed value