Skip to content

Instantly share code, notes, and snippets.

@deanlxvii
Created March 18, 2015 12:07
Show Gist options
  • Save deanlxvii/edb11250ed3a9b42cfca to your computer and use it in GitHub Desktop.
Save deanlxvii/edb11250ed3a9b42cfca to your computer and use it in GitHub Desktop.
noReplacementSimulation(numTrials)
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