Created
August 9, 2016 00:16
-
-
Save jakekara/86723714b092f2a08eb567cc2c91488d to your computer and use it in GitHub Desktop.
Coin flip simulator to find how many flips are required to produce x consecutive "heads" flips.
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
# Example of a coin flip simulator | |
# with a statistical summary | |
import random, pandas as pd | |
def flip(): | |
if random.uniform(0.0,1.0) >= 0.5: | |
return "heads" | |
return "tails" | |
def x_heads(x): | |
log = [] | |
consec_heads = 0 | |
flip_count = 0 | |
while consec_heads < x: | |
outcome = flip() | |
if outcome == "heads": | |
consec_heads += 1 | |
else: | |
consec_heads = 0 | |
log.append(outcome) | |
flip_count += 1 | |
return flip_count | |
def trials(f, x, trials=1000): | |
result_log = [] | |
for i in range(trials): | |
result = f(x) | |
result_log.append(result) | |
series = pd.Series(result_log) | |
print "median\t" + str(series.median()) | |
print series.describe() | |
trials(x_heads, 10, trials=1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment