Skip to content

Instantly share code, notes, and snippets.

@MikulasZelinka
Created September 20, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikulasZelinka/fa7d299f934b2f14f8a371e3bb0b5e3c to your computer and use it in GitHub Desktop.
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
# 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