Python coin-toss
- by Andy
i am new to Python, and i can't wrap my head around this. I have following function defined:
def FlipCoins(num_flips):
heads_rounds_won = 0
for i in range(10000):
heads = 0
tails = 0
for j in range(num_flips):
dice = random.randint(0,1)
if dice==1: heads += 1
else: tails += 1
if heads > tails: heads_rounds_won += 1
return heads_rounds_won
Here is what it should do (but apparently doesn't): flip a coin num_flip times, count heads and tails, and see if there are more heads than tails. If yes, increment head_rounds_won by 1. Repeat 10000 times.
I would assume that head_rounds_won will approximate 5000 (50%). And it does that for odd numbers as input. For example, 3, 5 or 7 will produce about 50%. However, even numbers will produce much lower results, more like 34%. Small numbers especially, with higher even numbers, like for example 800, the difference to 50% is much narrower.
Why is this the case? Shouldn't any input produce about 50% heads/tails?