Ad

How To Use Random.seed With Random.randn?

- 1 answer

I can seed an intenteger random number generator by doing:

import random    
random.seed(9002)   
random.randint(1, 10)  

and the same integer number is generated every time.

On the other hand, when I try to do the same to generate real numbers, the seed is not fixing the generated number to the same value every time.

import random   
random.seed(9001)
np.random.randn(1)

How can I seed np.random.randn ?

Ad

Answer

Seed numpy's random number generator.

np.random.seed(0)
np.random.randn(1)

That should always produce the same array.

Ad
source: stackoverflow.com
Ad