Created
April 1, 2017 19:21
-
-
Save igortg/de9355338a97235b6b15eeddb952ce15 to your computer and use it in GitHub Desktop.
Show the best strategy to win the "Pig" dice game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy | |
def play(): | |
strategies = {} | |
for i in range(1, 20): | |
game_result, count = 0, 0 | |
while game_result < 100: | |
round_result = 0 | |
for j in range(i): | |
dice = random.randint(1, 6) | |
if dice == 1: | |
round_result = 0 | |
break | |
else: | |
round_result += dice | |
game_result += round_result | |
count += 1 | |
strategies[i] = count | |
return strategies | |
num_runs = 10000 | |
winners = numpy.zeros(num_runs) | |
for i in xrange(num_runs): | |
strategies = play() | |
# for count, i in strategies.items(): | |
# print "Win with {0} plays - {1} per round".format(count, i) | |
winner = min(strategies.values()) | |
winners[i] = winner | |
print winners | |
from matplotlib import pyplot | |
pyplot.hist(winners) | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment