Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 982
Bill Avatar asked Sep 27 '18 20:09

Bill


People also ask

How do you find the seed of a random number generator?

For example, “take a number x, add 900 +x, then subtract 52.” In order for the process to start, you have to specify a starting number, x (the seed). Let's take the starting number 77: Add 900 + 77 = 977. Subtract 52 = 925.

How do I reset a random number seed in MATLAB?

If you want to avoid repeating the same random number arrays when MATLAB restarts, then execute the command, rng('shuffle'); before calling rand , randn , randi , or randperm . This command ensures that you do not repeat a result from a previous MATLAB session.

How do I cancel random seed?

There is no such thing as cancelling a seed. It just amounts to setting a different seed, based on some random-ish conditions, like milliseconds from epoch.


2 Answers

You're going down the wrong path. Instead of trying to unseed the global RNG used by numpy.random, use a separate RNG for the parts that need to be repeatable. This RNG can have a completely independent state from the numpy.random default RNG:

def test():
    rng = numpy.random.RandomState(2)
    print("Repeatable test:", [rng.randint(10) for i in range(3)])

While it is technically possible to save and restore the state of the global numpy.random RNG, it is a very specialized operation and rarely a good idea. It may be useful, for example, if you're debugging a piece of code and you want to "rewind" the random state after jumping backward through the code, though you need to save the state in advance, and it won't rewind any other random number generators:

# Don't abuse this.
state = numpy.random.get_state()
do_stuff()
numpy.random.set_state(state)
like image 88
user2357112 supports Monica Avatar answered Oct 19 '22 16:10

user2357112 supports Monica


You can instantiate your own Random object.

myrandom = random.Random(myseed)

The random module manages its own instance of Random, which will be unaffected by changes made to myrandom.

like image 3
Artem Bernatskyi Avatar answered Oct 19 '22 16:10

Artem Bernatskyi