Skip to content

Instantly share code, notes, and snippets.

@gkarthik
Created November 28, 2014 02:46
Show Gist options
  • Save gkarthik/1b0d7e701f33bf4b8ced to your computer and use it in GitHub Desktop.
Save gkarthik/1b0d7e701f33bf4b8ced to your computer and use it in GitHub Desktop.
Biased vs UnBiased Dice
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.'
@gkarthik
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment