Reputation: 1763
In the process of fixing the random seed for reproducibility I have found great SO answers with something along numpy.random.seed(123)
and then got confused by the last numpy.random.seed
documentation (from june 2020).
This is a convenience, legacy function. The best practice is to not reseed a BitGenerator, rather to recreate a new one.
What I understand is that it creates new random states. How to then use those for reproducibility ? Bellow is the sample code provided with the documentation and a ramdom test.
Can someone please tell me how to use rs
to fix the uniform sampling rand()
?
from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence, rand
rs = RandomState(MT19937(SeedSequence(123456789)))
rs = RandomState(MT19937(SeedSequence(987654321)))
rand()
Upvotes: 1
Views: 488
Reputation: 11285
rs
is set to a reproducible sequence of seemingly random (but obviously not) numbers. All of random number functions are available for your use.
>>> rs = RandomState(MT19937(SeedSequence(123456789)))
>>> rs.random(20)
array([0.16693771, 0.1963513 , 0.75563051, 0.72778686, 0.88007369,
0.15063958, 0.27400923, 0.88465501, 0.6783961 , 0.40250501,
0.58925838, 0.32977293, 0.42650604, 0.24460162, 0.7896271 ,
0.98224806, 0.25327893, 0.32868043, 0.06819438, 0.62491713])
// Show that the results are reproducible
>>> rs = RandomState(MT19937(SeedSequence(123456789)))
>>> rs.random(20)
array([0.16693771, 0.1963513 , 0.75563051, 0.72778686, 0.88007369,
0.15063958, 0.27400923, 0.88465501, 0.6783961 , 0.40250501,
0.58925838, 0.32977293, 0.42650604, 0.24460162, 0.7896271 ,
0.98224806, 0.25327893, 0.32868043, 0.06819438, 0.62491713])
Upvotes: 1