lundi 28 septembre 2020

How do I set a random seed, randomly?

I am running a process which is massively parallelized that depends on randomness. The trouble is that each of the workers in the parallel process shares the same random seed as the parent process, thereby ruining my attempts to run distinct random simulations.

Thus, I have the problem of how to set a distinct random seed on each worker at inception. My current solution is to take the microseconds part off of time.time() and convert it to an integer before passing it to random.seed(). It looks like the following. Is there a better way?

import re
import random
import time
def set_random_seed():
    seed = int(re.sub(r'[^0-9]', '', str(time.time())[-7:]))
    random.seed(seed)



Aucun commentaire:

Enregistrer un commentaire