mercredi 16 septembre 2020

Python: is there a way to continue a random sequence where you left off?

My program uses random numbers many times, and setting the seed at the start makes this reproducible. If the program stops, it saves variables in a pickle file so it can start off where it left off, but the random numbers will be different so the restarted version will produce different results than if the original version carried on. I would like to include the current random number generator state in the pickle file so the two runs are comparable. Here's a toy example:

>>> np.random.seed(0)
>>> np.random.randint(0,10,5)
array([5, 0, 3, 3, 7])
(save to pickle file)
>>> np.random.randint(0,10,5)
array([9, 3, 5, 2, 4])

(exit and restart python, load pickle file)
>>> np.random.randint(0,10,5)
array([9, 3, 5, 2, 4])

RandomState looked promising, but on inspection doesn't seem to do what I want.




Aucun commentaire:

Enregistrer un commentaire