So i'm trying to generate a list of numbers with desired probability; the problem is that random.seed() does not work in this case.
M_NumDependent = []
for i in range(61729):
random.seed(2020)
n = np.random.choice(np.arange(0, 4), p=[0.44, 0.21, 0.23, 0.12])
M_NumDependent.append(n)
print(M_NumDependent)
the desired output should be the same if the random.seed() works, but the output is different everytime i run it. Does anyone know if there's a function does the similar job of seed() for np.random.choice()?
numpy uses its own pseudo random generator. You can seed the Numpy random generator with np.random.seed(…) [numpy-doc]:
np.random.seed(2020)
For example:
>>> np.random.seed(2020)
>>> np.random.choice(np.arange(0, 4), p=[0.44, 0.21, 0.23, 0.12])
3
>>> np.random.seed(2020)
>>> np.random.choice(np.arange(0, 4), p=[0.44, 0.21, 0.23, 0.12])
3
>>> np.random.seed(2020)
>>> np.random.choice(np.arange(0, 4), p=[0.44, 0.21, 0.23, 0.12])
3
>>> np.random.choice(np.arange(0, 4), p=[0.44, 0.21, 0.23, 0.12])
2
As you can see we each time pick 3 whereas if we do not seed the random generator, 2 is the next item after 3.
You are accidentally setting random.random.seed() instead of numpy.random.seed().
Instead of
random.seed(2020)
use
import numpy as np
np.random.seed(2020)
and your results will always be reproducible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With