最佳答案
The uuid4() function of Python's module uuid
generates a random UUID, and seems to generate a different one every time:
In [1]: import uuid
In [2]: uuid.uuid4()
Out[2]: UUID('f6c9ad6c-eea0-4049-a7c5-56253bc3e9c0')
In [3]: uuid.uuid4()
Out[3]: UUID('2fc1b6f9-9052-4564-9be0-777e790af58f')
I would like to be able to generate the same random UUID every time I run a script - that is, I'd like to seed the random generator in uuid4()
. Is there a way to do this? (Or achieve this by some other means)?
I've to generate a UUID using the uuid.UUID()
method with a random 128-bit integer (from a seeded instance of random.Random()
) as input:
import uuid
import random
rd = random.Random()
rd.seed(0)
uuid.UUID(rd.getrandbits(128))
However, UUID()
seems not to accept this as input:
Traceback (most recent call last):
File "uuid_gen_seed.py", line 6, in <module>
uuid.UUID(rd.getrandbits(128))
File "/usr/lib/python2.7/uuid.py", line 133, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'long' object has no attribute 'replace'
Any other suggestions?