Skip to content

Instantly share code, notes, and snippets.

@libswan
Created May 11, 2013 13:04
Show Gist options
  • Save libswan/5559912 to your computer and use it in GitHub Desktop.
Save libswan/5559912 to your computer and use it in GitHub Desktop.
import pylab
import random
def MC(numBalls, numTrials):
ballsStudiedList = []
for i in range(numTrials):
ballList = []
ballListCopy = []
steps = 0
ballsStudied = 0
for i in range(numBalls):
i = random.choice(['white', 'black'])
ballList.append(i)
ballListCopy = ballList
randomLoc = random.choice(range(len(ballList)))
while randomLoc < len(ballList):
#print 'randomLoc'
#print randomLoc
ballAtLoc = ballList[randomLoc]
#print 'ballAtLoc'
#print ballAtLoc
if ballAtLoc == 'white':
steps += 1
ballsStudied += 1
break
elif ballAtLoc == 'black':
ballsStudied += 1
randomLoc += 1
#print 'randomLoc after black'
#print randomLoc
randomLoc = randomLoc
#print 'randomLoc at end of while'
#print randomLoc
ballsStudiedList.append(ballsStudied)
#print 'ballsStudied List'
#print ballsStudiedList
pylab.hist(ballsStudiedList, bins = 11)
#xmin, xmax = pylab.xlim()
#ymin, ymax = pylab.ylim()
#print 'x-range =', xmin, '-', xmax
#print 'y-range =', ymin, '-', ymax
#pylab.xlim(-1.0, 2.0)
pylab.figure(1)
pylab.savefig('MC')
pylab.show()
MC(1000,1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment