samedi 16 novembre 2019

How to "stash" random state generator state

I'm seeding a random number generator for reproducible results with:

import random

SEED = 32412542
random.seed(SEED)

I'd like to make it return "non-reproducible" random values for only one part of a program, as in:

import random

SEED = 32412542
random.seed(SEED)

my_list = [1, 2, 3, 4, 5]

res = random.sample(my_list, len(my_list))  # I would like result of this to be the same between runs of the program.

# Do some reproducible calculations, such as training neural network.
print(res)  # E.g. prints [3, 2, 4, 1, 5]

# What to do here?
res = random.sample(my_list, len(my_list))  # I would like result of this to be different between runs.

# Do some non-reproducible calculations, such as picking neural network parameters randomly.
print(res)  # Prints some random order.

res = random.sample(my_list, len(my_list))  # I would like result of this to be the same between runs of the program.

# Do some reproducible calculations, such as training neural network.
print(res)  # E.g. prints [2, 3, 1, 4, 5]

What I came up with so far is to seed with no parameter right before I'd like it to become non-reproducible and then re-seed with the SEED value afterwards:

import random

SEED = 32412542
random.seed(SEED)

my_list = [1, 2, 3, 4, 5]

res = random.sample(my_list, len(my_list))
print(res)  # Prints: [3, 2, 4, 1, 5]

random.seed()
res = random.sample(my_list, len(my_list))
print(res)  # Prints some random order.

random.seed(SEED)
res = random.sample(my_list, len(my_list))
print(res)  # Prints: [3, 2, 4, 1, 5], so exactly what has been printed before.

The problem is that after re-seeding, exactly the same set of random values is produced (obviously - in the end that's the purpose of seeding with a specific value), which I don't want to happen. I'd like to somehow restore the previous state of the random generator. Is that possible?




Aucun commentaire:

Enregistrer un commentaire