-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmonth-hall-paradox.py
52 lines (42 loc) · 1.23 KB
/
month-hall-paradox.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
import random
class MontyHall:
def __init__(self):
self.prize_door = random.randint(1, 3)
self.selected_door = 0
self.opened_door = 0
def select_door(self):
self.selected_door = random.randint(1, 3)
def host_door(self):
# host door should not be the selected nor prize door
d = random.randint(1, 3)
while d in (self.selected_door, self.prize_door):
d = random.randint(1, 3)
self.opened_door = d
def switch_door(self):
# sum of door numbers = 1+2+3 = 6.
self.selected_door = 6 - self.selected_door - self.opened_door
def hit(self):
if self.selected_door == self.prize_door:
return True
def run(self, switch=True):
self.select_door()
self.host_door()
if switch:
self.switch_door()
return self.hit() # hit?
# Switch
RUNS = 10000
print("%s Monty runs" % (RUNS))
hits = 0
for i in range(RUNS):
monty = MontyHall()
if monty.run(switch=True):
hits += 1
print("Switch : %.1f%%" % (100.0 * hits / RUNS))
# Stay
hits = 0
for i in range(RUNS):
monty = MontyHall()
if monty.run(switch=False):
hits += 1
print("Stay : %.1f%%" % (100.0 * hits / RUNS))