-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathnoReplacementSimulation-monte-carlo-simulation.py
65 lines (53 loc) · 1.8 KB
/
noReplacementSimulation-monte-carlo-simulation.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
@author: salimt
"""
import random
def noReplacementSimulation(numTrials):
'''(int) -> float
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.
Red = 1
Green = -1 or 0
'''
allSame = 0
# for i in num of trials:
for ball in range(numTrials):
bucket = ["red", "red", "red", "green", "green", "green"]
firstBall = random.choice(bucket)
bucket.remove(firstBall)
secondBall = random.choice(bucket)
bucket.remove(secondBall)
thirdBall = random.choice(bucket)
bucket.remove(thirdBall)
if firstBall == secondBall == thirdBall:
allSame += 1
return allSame / numTrials
############ SECOND WAY ##############
# allSameColour = 0
# for ball in range(1, numTrials + 1):
# bucket = [1, 1, 1, -1, -1, -1]
# nums = 0
# for i in range(3):
# chosenBall = random.choice(bucket)
# bucket.remove(chosenBall)
# nums += chosenBall
# if nums == 3 or nums == -3:
# allSameColour += 1
#
# return allSameColour / numTrials
############ THIRD WAY ##############
# import numpy as np
#
# allSame = 0
# bucket = [1, 1, 1, 0, 0, 0]
# for ball in range(1, numTrials + 1):
# summed = sum(np.random.choice(bucket, 3, replace=False))
# if summed == 3 or summed == 0:
# allSame += 1
#
# return allSame / float(numTrials)
print(noReplacementSimulation(5000))