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?
numpy.random.seed(0)
resets the state of the existing globalRandomState
instance that underlies the functions in thenumpy.random
namespace.numpy.random.RandomState(0)
returns a new seededRandomState
instance but otherwise does not change anything. You have to use the returnedRandomState
instance to get consistent pseudorandom numbers. If you use the functions in thenumpy.random
namespace, you will not get consistent pseudorandom numbers because they are pulling from a differentRandomState
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