Created
February 29, 2012 00:40
-
-
Save clarle/1936558 to your computer and use it in GitHub Desktop.
Monte Carlo for COMP 424
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 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