Skip to content

Instantly share code, notes, and snippets.

@rvcx
Last active July 14, 2017 17:58
Show Gist options
  • Save rvcx/410a6b4645e8ba34544a8847d05cd21e to your computer and use it in GitHub Desktop.
Save rvcx/410a6b4645e8ba34544a8847d05cd21e to your computer and use it in GitHub Desktop.
import random
def simulate(needed, win_pct=0.6):
losses, wins = 0, 0
while True:
if random.random() < win_pct:
wins += 1
else:
losses +=1
if wins > needed:
return True
if losses > needed:
return False
simulations = 1000000
for needed in range(1, 51):
prize = 1000 * 1000 - 10000 * needed
expected = sum(prize if simulate(needed) else 0
for i in range(simulations)) / float(simulations)
print('{}: {}'.format(needed, expected))
1: 641290.32
2: 668967.6
3: 688765.96
4: 704352.96
5: 714944.35
6: 725429.02
7: 731271.09
8: 736971.52
9: 740608.05
10: 742789.8
11: 744607.82
12: 744960.48
13: 744307.62
14: 742284.06
15: 740899.1
16: 738575.04
17: 734866.23
18: 731127.58
19: 727316.01
20: 722102.4
21: 717539.62
22: 712494.12
23: 707147.98
24: 701172.96
25: 694894.5
26: 688587.02
27: 681726.56
28: 674743.68
29: 667434.79
30: 660160.2
31: 653117.43
32: 645457.36
33: 637371.0
34: 629438.7
35: 621459.8
36: 613440.0
37: 605092.95
38: 596684.28
39: 588175.42
40: 579627.0
41: 570927.66
42: 562130.78
43: 553243.71
44: 544471.2
45: 535396.95
46: 526271.58
47: 517121.53
48: 508137.24
49: 498827.94
50: 489563.0
real 7m44.345s
user 7m42.271s
sys 0m0.952s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment