I tried two different ways to get a coin flip result, seeding the RNG first in order to get reproducible results.
First, I tried using random.randint
:
import random
random.seed(23412)
flip = random.randint(0,1)
if flip == 0:
print("Tails")
else:
print("Heads")
For this seed, I get a Heads result; the flip
result is 1
.
Then, I tried round
ing the result of random.random
:
import random
random.seed(23412)
flip = random.random()
print(flip) # for testing purposes
new_flip = round(flip)
if new_flip == 0:
print("Tails")
else:
print("Heads")
This time, I get a value for flip
of 0.27484468113952387
, which rounds to 0
, i.e. a Tails result.
Why does the result differ? Shouldn't random.randint
pull the same random value (since the seed was the same), and flip the coin the same way?