11

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?

1

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.