-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarlo
40 lines (37 loc) · 1.23 KB
/
MonteCarlo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def ReplacementSimulation(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 replaced once
drawn. Returns the a decimal - the fraction of times 3
balls of the same color were drawn.
'''
# Your code here
success = 0
for i in range(numTrials):
exp = []
for j in range(3):
exp.append(random.choice([0,1]))
if sum(exp) == 0 or sum(exp)==3:
success += 1
return float(success)/numTrials
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.
'''
# Your code here
success = 0
for i in range(numTrials):
bucket = [0, 0, 0, 1, 1, 1]
exp = []
for j in range(3):
exp.append(random.choice(bucket))
bucket.remove(exp[j])
if sum(exp) == 0 or sum(exp)==3:
success += 1
return float(success)/numTrials
print noReplacementSimulation(1000)