Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the current seed of NumPy's random number generator?

The following imports NumPy and sets the seed.

import numpy as np
np.random.seed(42)

However, I'm not interested in setting the seed but more in reading it. random.get_state() does not seem to contain the seed. The documentation doesn't show an obvious answer.

How do I retrieve the current seed used by numpy.random, assuming I did not set it manually?

I want to use the current seed to carry over for the next iteration of a process.

like image 767
Mast Avatar asked Aug 23 '15 22:08

Mast


People also ask

What is rand () seed?

In Golang, the rand. Seed() function is used to set a seed value to generate pseudo-random numbers. If the same seed value is used in every execution, then the same set of pseudo-random numbers is generated. In order to get a different set of pseudo-random numbers, we need to update the seed value.

How do I find a random seed in Python?

Python Random seed() MethodThe seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

How do I fix a random seed in Excel?

To stop the RAND or RANDBETWEEN functions from recalculating in one cell, select that cell, switch to the formula bar and press F9 to replace the formula with its value. To prevent an Excel random function from recalculating, use the Paste Special > Values feature.

Why do we seed a random number generator?

The seed is a starting point for a sequence of pseudorandom numbers. If you start from the same seed, you get the very same sequence. This can be quite useful for debugging. If you want a different sequence of numbers each time, you can use the current time as a seed.


4 Answers

The short answer is that you simply can't (at least not in general).

The Mersenne Twister RNG used by numpy has 219937-1 possible internal states, whereas a single 64 bit integer has only 264 possible values. It's therefore impossible to map every RNG state to a unique integer seed.

You can get and set the internal state of the RNG directly using np.random.get_state and np.random.set_state. The output of get_state is a tuple whose second element is a (624,) array of 32 bit integers. This array has more than enough bits to represent every possible internal state of the RNG (2624 * 32 > 219937-1).

The tuple returned by get_state can be used much like a seed in order to create reproducible sequences of random numbers. For example:

import numpy as np

# randomly initialize the RNG from some platform-dependent source of entropy
np.random.seed(None)

# get the initial state of the RNG
st0 = np.random.get_state()

# draw some random numbers
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]

# set the state back to what it was originally
np.random.set_state(st0)

# draw again
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]
like image 181
ali_m Avatar answered Oct 19 '22 09:10

ali_m


This contribution is intended to serve as a clarification to the right answer from ali_m, and as an important correction to the suggestion from Dong Justin.


These are my findings:

  1. After setting the random seed using np.random.seed(X) you can find it again using np.random.get_state()[1][0].
  2. It will, however, be of little use to you.

The output from the following code sections will show you why both statements are correct.


Statement 1 - you can find the random seed using np.random.get_state()[1][0].

If you set the random seed using np.random.seed(123), you can retrieve the random state as a tuple using state = np.random.get_state(). Below is a closer look at state (I'm using the Variable explorer in Spyder). I'm using a screenshot since using print(state) will flood your console because of the size of the array in the second element of the tuple.

enter image description here

You can easily see 123 as the first number in the array contained in the second element. And using seed = np.random.get_state()[1][0] will give you 123. Perfect? Not quite, because:

Statement 2 - It will, however, be of little use to you:

It may not seem so at first though, because you could use np.random.seed(123), retrieve the same number with seed = np.random.get_state()[1][0], reset the seed with np.random.seed(444), and then (seemingly) set it back to the 123 scenario with np.random.seed(seed). But then you'd already know what your random seed was before, so you wouldn't need to do it that way. The next code section will also show that you can not take the first number of any random state using np.random.get_state()[1][0] and expect to recreate that exact scenario. Note that you'll most likely have to shut down and restart your kernel completely (or call np.random.seed(None)) in order to be able to see this.

The following snippet uses np.random.randint() to generate 5 random integers between -10 and 10, as well as storing some info about the process:

Snippet 1

# 1. Imports
import pandas as pd
import numpy as np

# 2. set random seed
#seedSet = None
seedSet = 123
np.random.seed(seedSet)

# 3. describe random state
state = np.random.get_state()
state5 = np.random.get_state()[1][:5]
seedState = np.random.get_state()[1][0]

# 4. generate random numbers
random = np.random.randint(-10, 10, size = 5)

# 5. organize and present findings
df = pd.DataFrame.from_dict({'seedSet':seedSet, 'seedState':seedState, 'state':state, 'random':random})
print(df)

Notice that the column named seedState is the same as the first number under state. I could have printed it as a stand-alone number, but I wanted to keep it all in the same place. Also notice that, seedSet = 123, and np.random.seed(seedSet) so far have been commented out. And because no random seed has been set, your numbers will differ from mine. But that is not what is important here, but rather the internal consisteny of your results:

Output 1:

   random seedSet   seedState       state
0       2    None  1558056443  1558056443
1      -1    None  1558056443  1808451632
2       4    None  1558056443   730968006
3      -4    None  1558056443  3568749506
4      -6    None  1558056443  3809593045

In this particular case seed = np.random.get_state()[1][0] equals 1558056443. And following the logic from Dong Justins answer (as well as my own answer prior to this edit), you could set the random seed with np.random.seed(1558056443) and obtain the same random state. The next snippet will show that you can not:

Snippet 2

# 1. Imports
import pandas as pd
import numpy as np

# 2. set random seed
#seedSet = None
seedSet = 1558056443
np.random.seed(seedSet)

# 3. describe random state
#state = np.random.get_state()
state = np.random.get_state()[1][:5]
seedState = np.random.get_state()[1][0]

# 4. generate random numbers
random = np.random.randint(-10, 10, size = 5)

# 5. organize and present findings
df = pd.DataFrame.from_dict({'seedSet':seedSet, 'seedState':seedState, 'state':state, 'random':random})
print(df)

Output 2:

   random     seedSet   seedState       state
0       8  1558056443  1558056443  1558056443
1       3  1558056443  1558056443  1391218083
2       7  1558056443  1558056443  2754892524
3      -8  1558056443  1558056443  1971852777
4       4  1558056443  1558056443  2881604748

See the difference? np.random.get_state()[1][0] is identical for Output 1 and Output 2, but the rest of the output is not (most importantly the random numbers are not the same). So, as ali_m already has clearly stated:

It's therefore impossible to map every RNG state to a unique integer seed.

like image 25
vestland Avatar answered Oct 19 '22 09:10

vestland


Check the first element of the array returned by np.random.get_state(), it seems exactly the random seed to me.

like image 2
Dong Justin Avatar answered Oct 19 '22 09:10

Dong Justin


This answer complements important details others missed. First, to rephrase the conclusion:

Original random seeds (set via np.random.seed) cannot be retrieved after generating numbers, but intermediates (current state) can.

Refer to @vestland's answer; it may, however, mislead: the generated numbers differ not due to inability to map states, but that an incomplete encoding is used: get_state()[1]. The complete representation includes pos = get_state()[2]. To illustrate:

import numpy as np

state0 = np.random.get_state()
rand0  = np.random.randint(0, 10, 1)
state1 = np.random.get_state()
rand1  = np.random.randint(0, 10, 1)

assert all(s0 == s1 for s0, s1 in zip(state0[1], state1[1]))

We generated a number, yet get_state()[1] remained identical. However:

np.random.set_state(state0)
assert np.random.randint(0, 10, 1) == rand0

and likewise for state1 & rand1. Hence, @vestland's numbers differ because when not setting a seed, pos = 623 - whereas if we use np.random.seed, pos = 624. Why the inconvenient discrepancy? No clue.


In summary on np.random.seed(s):

  • get_state()[1][0] immediately after setting: retrieves s that exactly recreates the state
  • get_state()[1][0] after generating numbers: may or may not retrieve s, but it will not recreate the current state (at get_state())
  • get_state()[1][0] after generating many numbers: will not retrieve s. This is because pos exhausted its representation.
  • get_state() at any point: will exactly recreate that point.

Lastly, behavior may also differ due to get_state()[3:] (and of course [0]).

like image 2
OverLordGoldDragon Avatar answered Oct 19 '22 08:10

OverLordGoldDragon