Created
August 20, 2013 02:05
-
-
Save clarle/6276344 to your computer and use it in GitHub Desktop.
Flippin'
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
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