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

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


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 entropynp.random.seed(None)# get the initial state of the RNGst0 = np.random.get_state()# draw some random numbersprint(np.random.randint(0, 100, 10))# [ 8 76 76 33 77 26  3  1 68 21]# set the state back to what it was originallynp.random.set_state(st0)# draw againprint(np.random.randint(0, 100, 10))# [ 8 76 76 33 77 26  3  1 68 21]


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. Importsimport pandas as pdimport numpy as np# 2. set random seed#seedSet = NoneseedSet = 123np.random.seed(seedSet)# 3. describe random statestate = np.random.get_state()state5 = np.random.get_state()[1][:5]seedState = np.random.get_state()[1][0]# 4. generate random numbersrandom = np.random.randint(-10, 10, size = 5)# 5. organize and present findingsdf = 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       state0       2    None  1558056443  15580564431      -1    None  1558056443  18084516322       4    None  1558056443   7309680063      -4    None  1558056443  35687495064      -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. Importsimport pandas as pdimport numpy as np# 2. set random seed#seedSet = NoneseedSet = 1558056443np.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 numbersrandom = np.random.randint(-10, 10, size = 5)# 5. organize and present findingsdf = pd.DataFrame.from_dict({'seedSet':seedSet, 'seedState':seedState, 'state':state, 'random':random})print(df)

Output 2:

   random     seedSet   seedState       state0       8  1558056443  1558056443  15580564431       3  1558056443  1558056443  13912180832       7  1558056443  1558056443  27548925243      -8  1558056443  1558056443  19718527774       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.


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