Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between RandomState and seed in numpy

I have read the documentation but I still find difficult to understand the difference between the use of

numpy.random.RandomState(0) 

or

numpy.random.seed(0)

Don't they both ensure that the process through which the random values are selected is the same and consistent across runs?

like image 384
user Avatar asked May 14 '16 07:05

user


1 Answers

numpy.random.seed(0) resets the state of the existing global RandomState instance that underlies the functions in the numpy.random namespace.

numpy.random.RandomState(0) returns a new seeded RandomState instance but otherwise does not change anything. You have to use the returned RandomState instance to get consistent pseudorandom numbers. If you use the functions in the numpy.random namespace, you will not get consistent pseudorandom numbers because they are pulling from a different RandomState instance than the one you just created.

If you care about reproducibility, it is highly preferable to structure your code to pass around RandomState instances. Global state sucks. C.f. Consistenly create same random numpy array

like image 61
Robert Kern Avatar answered Sep 27 '22 20:09

Robert Kern