Skip to content

Instantly share code, notes, and snippets.

@clarle
Created August 20, 2013 02:05
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 clarle/6276344 to your computer and use it in GitHub Desktop.
Save clarle/6276344 to your computer and use it in GitHub Desktop.
Flippin'
import random
import collections
COIN = ["H", "T"]
def average(array):
return sum(array) / float(len(array))
def flip_coins(result):
flips = collections.deque(maxlen=3)
num_flips = 0
while (list(flips) != result):
flips.append(random.choice(COIN))
num_flips += 1
return num_flips
THT = []
HTT = []
for i in xrange(100000):
THT.append(flip_coins(["T", "H", "T"]))
HTT.append(flip_coins(["H", "T", "T"]))
print("THT average: " + str(average(THT)))
print("HTT average: " + str(average(HTT)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment