Skip to content

Instantly share code, notes, and snippets.

@nullhack
Last active November 2, 2016 12:30
Show Gist options
  • Select an option

  • Save nullhack/7b0e06c5d1871b130fe23a1d579dc347 to your computer and use it in GitHub Desktop.

Select an option

Save nullhack/7b0e06c5d1871b130fe23a1d579dc347 to your computer and use it in GitHub Desktop.
Best reward from 3 rolls using Monte Carlo Experiment
"""Licensed under GPLv3 <http://www.gnu.org/licenses/>.
Authors:
Eric Lopes
"""
import random
def roll(x=[6,6]):
for i in range(2):
v=random.randint(1,6)
if v>=x[i]: return v
v=random.randint(1,6)
return v
def monteCarlo(n=1000, x=[6,6]):
c=0
for i in range(n):
c+=roll(x)
return c/n
def testAll(n=10**6):
c=0
mt = [[0 for a in range(6)] for b in range(6)]
for i in range(1,7):
for j in range(1,7):
v=monteCarlo(n,[i,j])
mt[i-1][j-1]=v
if v>c:
c=v
print(i,j,v)
return mt
testAll(10**6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment