7

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()?

2 Answers 2

10

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.

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

Comments

5

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.

1 Comment

I think you mean random.seed() instead of random.random.seed() ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.