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.
Check the first element of the array returned by
np.random.get_state()
, it seems exactly the random seed to me.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
andnp.random.set_state
. The output ofget_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:I have edited this answer heavily after I found out some more details about the problem at hand. As it now stands, I'm hoping that it serves well as a clarification to the answer from ali_m, and as an important correction to the answer from Dong Justin.
These are my findings:
np.random.seed(X)
you can find it again usingnp.random.get_state()[1][0]
.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 usingstate = np.random.get_state()
. Below is a closer look atstate
(I'm using the Variable explorer in Spyder). I'm using a screenshot since usingprint(state)
will flood your console because of the size of the array in the second element of the tuple.You can easily see
123
as the first number in the array contained in the second element. And usingseed = np.random.get_state()[1][0]
will give you123
. Perfect? Not quite, because:Statement 2 - It will, however, be of little use to you:
It may seem so at first though, because you could use
np.random.seed(123)
, retrieve the same number withseed = np.random.get_state()[1][0]
, reset the seed withnp.random.seed(444)
, and then (seemingly) set it back to the 123 scenario withnp.random.seed(seed)
. But then you'll 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 usingnp.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 callnp.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
Notice that the column named
seedState
is the same as the first number understate
. 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
, andnp.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:
In this particular case
seed = np.random.get_state()[1][0]
equals1558056443
. And following the logic from Dong Justins answer (as wll as my own answer prior to this edit), you could get set the random seed withnp.random.seed(1558056443)
and obtain the same random state. The next snippet will show that you can not:Snippet 2
Output 2:
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: