jeudi 27 septembre 2018

Best way to revert to a random seed after temporarily fixing it?

Is this the only way to 'unseed' the random number generator:

np.random.seed(int(time.time()))

If you have some code that you want to be repeatable (e.g. a test) in a loop with other code that you want to be random each loop, how do you 'reset' the seed to random number generator after setting it?

The following code illustrates the issue:

import numpy as np

def test():
    np.random.seed(2)
    print("Repeatable test:", [np.random.randint(10) for i in range(3)])

for i in range(4):
    print("Random number:", np.random.randint(10))
    test()

Random number: 8
Repeatable test: [8, 8, 6]
Random number: 2
Repeatable test: [8, 8, 6]
Random number: 2
Repeatable test: [8, 8, 6]
Random number: 2
Repeatable test: [8, 8, 6]

Desired result: I want random number to be random each loop.

I am happy to import the time module if this is the only way to do it but I thought there might be a simpler, more robust way.

(You can't get the current seed according to this post)




Aucun commentaire:

Enregistrer un commentaire