Skip to content

Instantly share code, notes, and snippets.

@pjz
Created March 14, 2016 16:51
Show Gist options
  • Save pjz/21fc36347f7715c8a8f3 to your computer and use it in GitHub Desktop.
Save pjz/21fc36347f7715c8a8f3 to your computer and use it in GitHub Desktop.
import random
def tosses_until(s):
tosses = ''
while not tosses.endswith(s):
tosses += random.choice('HT')
return len(tosses)
TRIALS = 10000
alice = sum(tosses_until('HT') for i in range(TRIALS))
bob = sum(tosses_until('HH') for i in range(TRIALS))
print("Alice: %d / %d " % (alice, TRIALS))
print(" Bob: %d / %d " % (bob, TRIALS))
"""
$ python headtails.py
Alice: 40062 / 10000
Bob: 59274 / 10000
$ python headtails.py
Alice: 39892 / 10000
Bob: 60868 / 10000
$ python headtails.py
Alice: 40259 / 10000
Bob: 60975 / 10000
$ python headtails.py
Alice: 39743 / 10000
Bob: 59394 / 10000
$ python headtails.py
Alice: 40209 / 10000
Bob: 60172 / 10000
$ python headtails.py
Alice: 39914 / 10000
Bob: 59943 / 10000
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment