Skip to content

Instantly share code, notes, and snippets.

@clarle
Created February 29, 2012 00:40
Show Gist options
  • Save clarle/1936558 to your computer and use it in GitHub Desktop.
Save clarle/1936558 to your computer and use it in GitHub Desktop.
Monte Carlo for COMP 424
from random import random, shuffle
def monte_carlo(draws, reds, blues):
cases = []
for i in xrange(draws):
red_draws = [1] * reds
blue_draws = [0] * blues
total = red_draws + blue_draws
shuffle(total)
utility = total.pop() + total.pop() # pop two off
cases.append(utility)
return sum(cases)/float(draws)
print "3 red and 2 blue: " + str(monte_carlo(10000, 3, 2))
print "30 red and 20 blue: " + str(monte_carlo(10000, 30, 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment