-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProbability Calculator.py
More file actions
44 lines (33 loc) · 1.12 KB
/
Probability Calculator.py
File metadata and controls
44 lines (33 loc) · 1.12 KB
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
import random
import copy
class Hat:
def __init__(self, **kwargs) -> None:
self.contents = [key for key, value in kwargs.items() for _ in range(value)]
def draw(self, number):
if number > len(self.contents):
return self.contents
balls = [
self.contents.pop(random.randrange(len(self.contents)))
for _ in range(number)
]
return balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
expected_no_of_balls = [expected_balls[key] for key in expected_balls]
successes = 0
for _ in range(num_experiments):
new_hat = copy.deepcopy(hat)
balls = new_hat.draw(num_balls_drawn)
no_of_balls = [balls.count(key) for key in expected_balls]
if no_of_balls >= expected_no_of_balls:
successes += 1
return successes / num_experiments
hat = Hat(blue=4, red=2, green=6)
hat_n = copy.deepcopy(hat)
print(hat_n.draw(10))
probability = experiment(
hat=hat,
expected_balls={"blue": 2, "red": 1},
num_balls_drawn=4,
num_experiments=3000,
)
print("Probability:", probability)