Search code examples
pythonnumpyrandom-seednumpy-random

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?


Solution

  • 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.