Created
November 28, 2014 02:46
-
-
Save gkarthik/1b0d7e701f33bf4b8ced to your computer and use it in GitHub Desktop.
Biased vs UnBiased Dice
This file contains 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
from random import randrange | |
import matplotlib.pyplot as PLT | |
biasedDiceDist = [1,2,3,4,4,4,5,6] | |
unbiasedDiceDist= [1,2,3,4,5,6] | |
board = [0] * 40 | |
rollSum = 0 | |
fig = PLT.figure() | |
def roll(dist): | |
return dist[randrange(len(dist))] | |
def simulateGame(no, dist): | |
global rollSum | |
global board | |
rollSum = 0 | |
board = [0] * 40 | |
for x in range(0, no): | |
rollSum += roll(dist) | |
if rollSum >= 40: | |
rollSum -= 40 | |
board[rollSum] += 1 | |
def runSimulation(no): | |
global fig | |
fig = PLT.figure() | |
global xaxis | |
for x in range(0, no): | |
simulateGame(10000, biasedDiceDist) | |
biased = fig.add_subplot(211) | |
biased.fill(board, 'r', alpha=0.2) | |
biased.grid(True) | |
simulateGame(10000, unbiasedDiceDist) | |
unbiased = fig.add_subplot(212) | |
unbiased.fill(board, 'g', alpha = 0.2) | |
unbiased.grid(True) | |
for k in range(0,10): | |
print 'Running Simulation ... '+str(k) | |
runSimulation(1000) | |
PLT.savefig('./plot'+str(k)+'.png') | |
print 'Rendering Complete.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plots at https://plus.google.com/photos/+KarthikG92/albums/6086582387871890641