Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created January 14, 2018 03:29
Show Gist options
  • Save jrjames83/2b922d36e81a9057afe71ea21dba86cb to your computer and use it in GitHub Desktop.
Save jrjames83/2b922d36e81a9057afe71ea21dba86cb to your computer and use it in GitHub Desktop.
Python Coin Toss Simulation of Binomial Theorem
# python 3.6
### 10 heads in a row, 10 tails in a row
from collections import Counter
from itertools import groupby
print(1 / (.50 ** 11))
# 1024 tosses to get 1 string of heads or tails consecutive
outcomes = ''
for toss in range(1024):
toss_outcome = np.random.random()
if toss_outcome < .50:
outcomes += 'H'
if toss_outcome > .50:
outcomes += 'T'
Counter(outcomes)
outcome_tracker = []
for key, group in groupby(outcomes):
outcome_tracker.append( (key, len(list(group))) )
sorted(outcome_tracker, key=lambda x: x[1], reverse=True)[:10]
all_tails = [x[1] for x in
sorted(outcome_tracker, key=lambda x: x[1], reverse=True) if x[0] == 'H']
Counter(all_tails).most_common(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment