Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created August 9, 2016 00:16
Show Gist options
  • Save jakekara/86723714b092f2a08eb567cc2c91488d to your computer and use it in GitHub Desktop.
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.
# 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