Last active
August 2, 2016 11:32
-
-
Save vegarsti/ccb74508664bab94aafa to your computer and use it in GitHub Desktop.
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
""" | |
A program for investigating this property of coins: | |
If Alice tosses a coin until she sees a head followed by a tail, and Bob tosses a coin until he sees two heads in a row, | |
then on average, Alice will require four tosses while Bob will require six tosses (try this at home!), | |
even though head-tail and head-head have an equal chance of appearing after two coin tosses. | |
From https://www.quantamagazine.org/20160313-mathematicians-discover-prime-conspiracy/ | |
""" | |
from numpy.random import randint | |
from sys import argv | |
HEAD = 0 | |
TAIL = 1 | |
# Give N on command line. | |
N = int(argv[1]) | |
""" | |
Flip a coin. 0 is heads, 1 is tails. | |
""" | |
def throw_die(): | |
return randint(2) | |
""" | |
Returns the total number of coin tosses needed | |
to satisfy the conditions given. It is possible to give any | |
combination for two consecutive throws: | |
Head and then tails, head and then head, tails and then head, | |
or tails and then tails. | |
""" | |
def experiment(N, first_throw, second_throw): | |
total = 0 | |
for i in range(N): | |
old = throw_die() | |
throws = 1 | |
while True: | |
new = throw_die() | |
throws += 1 | |
if old == first_throw and new == second_throw: | |
break | |
old = new | |
total += throws | |
return total | |
total_Alice = experiment(N, HEAD, TAIL) | |
total_Bob = experiment(N, HEAD, HEAD) | |
print "With %i experiments," % N | |
print "%10s %5.2f" % ("Alice:", total_Alice/float(N)) | |
print "%10s %5.2f" % ("Bob:", total_Bob/float(N)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment