Andrei
Andrei

Reputation: 993

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?

Upvotes: 0

Views: 1525

Answers (2)

dadung
dadung

Reputation: 39

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.

Upvotes: 1

Hao Li
Hao Li

Reputation: 1762

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.

Upvotes: 3

Related Questions