Home > Software design >  Setting a seed for numpy uniform doesn't result in the same probabilities
Setting a seed for numpy uniform doesn't result in the same probabilities

Time:02-18

Why doesn't the below shown code result in 3 arrays with the same probabilities? How can I generate reproducible probabilities?

import numpy as np
np.random.seed(42)
for i in range(3):
    print(np.random.uniform(size = 10))

[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452
 0.05808361 0.86617615 0.60111501 0.70807258]

[0.02058449 0.96990985 0.83244264 0.21233911 0.18182497 0.18340451
     0.30424224 0.52475643 0.43194502 0.29122914]

[0.61185289 0.13949386 0.29214465 0.36636184 0.45606998 0.78517596
     0.19967378 0.51423444 0.59241457 0.04645041]

CodePudding user response:

You should reset the seed at the beginning of every loop.

import numpy as np
for i in range(3):
    np.random.seed(42)
    print(np.random.uniform(size = 10))
[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452
 0.05808361 0.86617615 0.60111501 0.70807258]
[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452
 0.05808361 0.86617615 0.60111501 0.70807258]
[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452
 0.05808361 0.86617615 0.60111501 0.70807258]
  • Related