简体   繁体   中英

Why does numpy.random.Generator.choice gives different results even if given fixed seed?

The code is simple:

import numpy
rng = numpy.random.default_rng(0)
control = rng.choice([0,1],p=[0.5,0.5])
for i in range(100):
    print(control == rng.choice([0,1],p=[0.5,0.5]))
# Not only True gets printed

Probably I am missing something, but the way I understand this is that rng.choice, run with the exact same parameters, should always return the same thing if it was seeded. What am I missing?

I think you might misunderstand the usage of the seed. The following code should always output True :

import numpy
rng = numpy.random.default_rng(0)
control = rng.choice([0,1],p=[0.5,0.5])
for i in range(100):
    rng = numpy.random.default_rng(0)
    print(control == rng.choice([0,1],p=[0.5,0.5]))
# Always True

When we used the same seed, we could get the same sequence of random numbers. Which means:

import numpy
rng = numpy.random.default_rng(0)
out = [rng.choice([0, 1], p=[0.5, 0.5]) for _ in range(10)] 

the out should be the same whenever you run it, but the values in out are different.

As far as I understand random number generation, you should get the same 100 outputs every time you run the program with the same seed, but since you sample from a normal distribution your results for each sample should be different.

So you should always get the same sequence of true and false, each time you run this program.

I have tried and it seems like it does at least that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM