Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy random seed produces different random numbers

I run the following code:

 np.random.RandomState(3)
 idx1 = np.random.choice(range(20),(5,))
 idx2 = np.random.choice(range(20),(5,))  
 np.random.RandomState(3)
 idx1S = np.random.choice(range(20),(5,))
 idx2S = np.random.choice(range(20),(5,))       

The output I get is the following:

idx1:  array([ 2, 19, 19,  9,  4])  
idx1S: array([ 2, 19, 19,  9,  4])  

idx2:  array([ 9,  2,  7, 10,  6]) 
idx2S: array([ 5, 16,  9, 11, 15]) 

idx1 and idx1S match, but idx2 and idx2S do not match. I expect that once I seed the random number generator and repeat the same sequence of commands - it should produce the same sequence of random numbers. Is this not true? Or is there something else that I am missing?

like image 541
pulkitag Avatar asked Mar 29 '15 01:03

pulkitag


People also ask

Does random seed work for NumPy?

The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.

How are random numbers generated from a seed?

A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001). A computer counts seconds from January 1, 1970 — a system called Unix time.

How does NumPy generate random numbers?

Numpy's random number routines produce pseudo random numbers using combinations of a BitGenerator to create sequences and a Generator to use those sequences to sample from different statistical distributions: BitGenerators: Objects that generate random numbers.

Why is NumPy's random module not considered completely random?

The “random” numbers generated by NumPy are not exactly random. They are pseudo-random … they approximate random numbers, but are 100% determined by the input and the pseudo-random number algorithm. The np. random.


1 Answers

You're confusing RandomState with seed. Your first line constructs an object which you can then use as your random source. For example, we make

>>> rnd = np.random.RandomState(3)
>>> rnd
<mtrand.RandomState object at 0xb17e18cc>

and then

>>> rnd.choice(range(20), (5,))
array([10,  3,  8,  0, 19])
>>> rnd.choice(range(20), (5,))
array([10, 11,  9, 10,  6])
>>> rnd = np.random.RandomState(3)
>>> rnd.choice(range(20), (5,))
array([10,  3,  8,  0, 19])
>>> rnd.choice(range(20), (5,))
array([10, 11,  9, 10,  6])

[I don't understand why your idx1 and idx1S agree-- but you didn't actually post a self-contained transcript, so I suspect user error.]

If you want to affect the global state, use seed:

>>> np.random.seed(3)
>>> np.random.choice(range(20),(5,))
array([10,  3,  8,  0, 19])
>>> np.random.choice(range(20),(5,))
array([10, 11,  9, 10,  6])
>>> np.random.seed(3)
>>> np.random.choice(range(20),(5,))
array([10,  3,  8,  0, 19])
>>> np.random.choice(range(20),(5,))
array([10, 11,  9, 10,  6])

Using a specific RandomState object may seem less convenient at first, but it makes a lot of things easier when you want different entropy streams you can tune.

like image 95
DSM Avatar answered Sep 28 '22 18:09

DSM