Created
March 18, 2015 12:07
-
-
Save deanlxvii/edb11250ed3a9b42cfca to your computer and use it in GitHub Desktop.
noReplacementSimulation(numTrials)
This file contains hidden or 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
import random | |
def noReplacementSimulation(numTrials): | |
''' | |
Runs numTrials trials of a Monte Carlo simulation | |
of drawing 3 balls out of a bucket containing | |
3 red and 3 green balls. Balls are not replaced once | |
drawn. Returns the a decimal - the fraction of times 3 | |
balls of the same color were drawn. | |
''' | |
same_color_count = 0 | |
for trial in xrange(numTrials): | |
balls = [0,0,0,1,1,1] | |
random.shuffle(balls) | |
sum_balls = sum(balls[:3]) | |
if sum_balls == 0 or sum_balls == 3: | |
same_color_count += 1 | |
return float(same_color_count)/numTrials | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment