Skip to content

Instantly share code, notes, and snippets.

@beltiras
Last active September 29, 2015 15:37
Show Gist options
  • Save beltiras/228c4e77b1e2aa6d33f1 to your computer and use it in GitHub Desktop.
Save beltiras/228c4e77b1e2aa6d33f1 to your computer and use it in GitHub Desktop.
from random import random
from collections import Counter
def randbits(n, p):
i = 0
while i<n:
yield random() < p
i += 1
def mc_hothand(trials, p):
d={1:{0:0,1:0},0:{0:0,1:0}}
lastbit=0
for nextbit in randbits(trials, p):
d[lastbit][nextbit] += 1
lastbit = nextbit
if d[1][0]+d[1][1]:
return float(d[1][1])/float(d[1][0]+d[1][1])
return 0.0 # short runs will sometimes yield a division by zero.
def hothand_asymptote():
return [sum([mc_hothand(n,0.5) for i in xrange(1000)])/1000.0 for n in xrange(4,100)]
def hothand(p=0.5, trials=3):
lastbit = random() < p
wins = 0
for i in xrange(trials):
nextbit = random() < p
if nextbit != lastbit:
wins += 1
lastbit = nextbit
return wins
def test_hothand(p,n):
a = Counter([hothand(p,n) for i in xrange(1000)])
return (a[0]*-3)+(a[1]*-1)+a[2]+(3*a[3])
@beltiras
Copy link
Author

asymptote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment