0

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?

Andrei
  • 961
  • 1
  • 9
  • 29

2 Answers2

2

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.

Hao Li
  • 1,593
  • 15
  • 27
  • You are right, I misunderstood what seed does (or more accurately, how choice works). Such simple function, you'd think it doesn't need much explanations, yet here I am. I was worried that ```p=[0.5,0.5]``` influences the result, and that would not be good for what I'm working on. – Andrei Jun 23 '20 at 11:40
  • I thought I got it, but I realized I am still getting odd results that were not obvious on the above example. I asked a [new, more specific question](https://stackoverflow.com/questions/62536092/why-does-numpy-random-generator-choice-provides-different-results-seeded-with), if you'd be so kind to have a look? – Andrei Jun 23 '20 at 13:40
1

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.

dadung
  • 39
  • 4