Skip to content

Instantly share code, notes, and snippets.

@tanutarou
Created December 25, 2015 05:00
Show Gist options
  • Save tanutarou/910e41991f22b77b55b4 to your computer and use it in GitHub Desktop.
Save tanutarou/910e41991f22b77b55b4 to your computer and use it in GitHub Desktop.
Penney's game simulation
import numpy as np
N = 10000
winfreq = np.array([0, 0])
playerA = "111"
playerB = "011"
for i in xrange(N):
res = [2, 2, 2]
while True:
res.pop(0)
coin = np.random.randint(2)
res.append(coin)
bin_res = "".join(map(str, res))
if bin_res == playerA:
winfreq[0] += 1
break
elif bin_res == playerB:
winfreq[1] += 1
break
winrate = winfreq * 1.0 / N
print "win rate:"
print winrate
print "playerB / playerA = ", winrate[1] / winrate[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment