dimanche 25 avril 2021

how to restore numpy seed back to its former state?

I have a function which do some random things..

And I want to input seed to it, so for same seeds the output will be the same..

But initiating the seed at the beggining like this:

def generate_random_number(seed):

    np.random.seed(seed)
    return np.random.random(1)

Will result that after calling this method, the seed is ruined.

Is there a way to "save" the current seed state and restore it later?

So it will look something like this:

def generate_random_number2(seed):
    old_seed = np.random.get_seed()
    np.random.seed(seed)
    random_number = np.random.random(1)
    np.random.seed(old_seed)
    return random_number

Of course, the command: np.random.get_seed() is a made-up function.. is there something like this? Or alternative approach to generate "random" (up to seed input) output without destroying everyone's randomness state?

(The actual random process is much more complicated in practice, it generates matrices in loop so I would like to stick with the format of initiating seed at the beggining for simplicity)




Aucun commentaire:

Enregistrer un commentaire