-
Notifications
You must be signed in to change notification settings - Fork 190
/
balls_in_hat.py
43 lines (37 loc) · 1.06 KB
/
balls_in_hat.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
import random
def draw_ball(hat):
"""Draw a ball using list index."""
index = random.randint(0, len(hat)-1)
color = hat.pop(index)
return color, hat
def draw_ball(hat):
"""Draw a ball using list index."""
index = random.randint(0, len(hat)-1)
color = hat[index]
del hat[index]
return color, hat
def draw_ball(hat):
"""Draw a ball using list element."""
color = random.choice(hat)
hat.remove(color)
return color, hat
def new_hat():
colors = 'black', 'red', 'blue' # (tuple of strings)
hat = []
for color in colors:
for i in range(4):
hat.append(color)
return hat
n = int(raw_input('How many balls are to be drawn? '))
N = int(raw_input('How many experiments? '))
# Run experiments
M = 0 # no of successes
for e in range(N):
hat = new_hat()
balls = [] # the n balls we draw
for i in range(n):
color, hat = draw_ball(hat)
balls.append(color)
if balls.count('black') >= 2: # at least two black balls?
M += 1
print 'Probability:', float(M)/N