(TIL) Python: Generate random but reproducible UUID with seed

less than 1 minute read

I often generate UUIDs ( Universally Unique Identifiers ), but when I use these in testing, I want to do so reproducibly. Turns out you can do this using a seed from the random module:

import uuid
import random

seed = 0

rd = random.Random()
rd.seed(seed)

reproducible_seed = uuid.UUID(int=rd.getrandbits(128))

Now reproducible_seed should be the same for each run of this script.

Via StackOverflow.

Comments