samedi 14 mars 2020

Anyway to set a seed for the "random" module in an object instead of globally? [duplicate]

For a project I use the random module. I'm aware of how the seed works. However for a certain function, I have it choosing a random int that requires a seed to be set. I can set the seed and get my desired result but the issue is when I set the seed with random.seed(seed) it changes the random seed globally for everything. Not just the specific thing I'm doing. I have other things that also use random but I don't want them using a seed. I want them with the default seed. I could just run random.seed(None) to reset it to default but that seems barbaric. Is it somehow possible to make an object or session of random? Here's an example of what I imagined.

import random
rand_obj = random.Session()
rand_obj.seed("my seed")
print(rand_obj.randint(0, 1))

# This is not affected by the seed set earlier since its just using random globally, 
# when the seed set earlier is on the object
print(random.randint(0, 1))



Aucun commentaire:

Enregistrer un commentaire