-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdraw_two.py
More file actions
45 lines (34 loc) · 1.01 KB
/
draw_two.py
File metadata and controls
45 lines (34 loc) · 1.01 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
45
# Draw Two
# Two numbers are drawn from the integers 1-10.
# What's the expected value of their sum?
# (With and without replacement.)
# Based on https://twitter.com/MrHonner/status/917546796322377728
import random
def with_replacement(samples):
total = 0
for i in range(samples):
a = random.randint(1, 10)
b = random.randint(1, 10)
c = a + b
total += c
avg = total / samples
return avg
def without_replacement(samples):
total = 0
for i in range(samples):
values = list(range(1, 11)) # range stop value is not inclusive
random.shuffle(values)
a = values.pop()
b = values.pop()
c = a + b
total += c
avg = total / samples
return avg
def draw_two():
samples = 10000
with_rep = with_replacement(samples)
without_rep = without_replacement(samples)
print("Expected value with replacement:", with_rep)
print("Expected value without replacement:", without_rep)
if __name__ == '__main__':
draw_two()