Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Forked from jimblandy/heads.py
Created August 4, 2017 04:21
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 jorendorff/3a9b4c649f8e3fd72d7d2941852cdf44 to your computer and use it in GitHub Desktop.
Save jorendorff/3a9b4c649f8e3fd72d7d2941852cdf44 to your computer and use it in GitHub Desktop.
There must be a better way to compute this...
import random
random.seed()
def flip():
return random.choice([True, False])
def run():
b = flip()
n = 1
while True:
(a, b) = (b, flip())
n += 1
if a and b:
return n
runs = 100000
def count():
counts = [0] * 100
for i in xrange(0, runs):
n = run()
if n < len(counts):
counts[n] += 1
return counts
counts = count()
for n in range(2,10):
print round(float(counts[n]) / runs * 2**n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment