Skip to content

Instantly share code, notes, and snippets.

@dgrant
Last active December 16, 2015 05:49
Show Gist options
  • Select an option

  • Save dgrant/5386720 to your computer and use it in GitHub Desktop.

Select an option

Save dgrant/5386720 to your computer and use it in GitHub Desktop.
Just a simulator for the problem posed by Geordie Rose here: http://dwave.wordpress.com/2008/03/03/i-am-gamblor/
from __future__ import division
import random
totalPaid=0
numRuns=0
maxRuns=100000
maxRuns=10000
amountCharged=1
def flipCoins():
result=0
num=-1
while result < 0.5:
result=random.random()
num+=1
# if num >=3:
# return 3
else:
return num
dist={}
for i in xrange(maxRuns):
numToGetTail = flipCoins()
# print "numtails=", numToGetTail
if dist.has_key(numToGetTail) == 0: dist[numToGetTail] = 1
else: dist[numToGetTail] += 1
if numToGetTail > 0:
payout = 2**(numToGetTail-1)
else:
payout = 0
# print "payout=", payout
print
totalPaid += payout - amountCharged
numRuns += 1
print "average paid out=", totalPaid/numRuns
print "average paid out div by 4=", totalPaid/numRuns/4
print "total paid out=", totalPaid
for i in sorted(dist.keys()):
print i,"=",dist[i]/maxRuns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment