Created
September 20, 2017 20:46
-
-
Save MikulasZelinka/fa7d299f934b2f14f8a371e3bb0b5e3c to your computer and use it in GitHub Desktop.
Infinitely many people gather to wrap their heads around a coin-flipping game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Game: n people, each person flips a coin until they get heads | |
# Question: what is the ratio of heads after the game ends? | |
n = 1024 | |
heads = 0 | |
tails = 0 | |
# how many rounds does each game last on average (just for fun): | |
iterations = 0 | |
# number of game simulations to run: | |
runs = 1024 | |
for run in range(runs): | |
m = n | |
while m > 0: | |
for i in range(m): | |
if random.random() < 0.5: | |
heads += 1 | |
else: | |
tails += 1 | |
m -= 1 | |
iterations += 1 | |
# Answer: there is an equal amount of heads and tails | |
print('avg. iterations: ', iterations / runs, ', % heads', (100 * heads / (heads + tails))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment