Last active
November 2, 2016 12:30
-
-
Save nullhack/7b0e06c5d1871b130fe23a1d579dc347 to your computer and use it in GitHub Desktop.
Best reward from 3 rolls using Monte Carlo Experiment
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
| """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