-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty_hall_sim.py
61 lines (50 loc) · 1.25 KB
/
monty_hall_sim.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
import random
# Three doors on the stage
doors = [1, 2, 3]
wins = 0
losses = 0
# Run the simulation n times
n = 10000
# You keep your original door selection
for i in range(n):
door_with_the_car = random.choice(doors)
your_initial_choice = random.choice(doors)
door_host_reveals = random.choice(
[
door for door in doors
if door not in [door_with_the_car, your_initial_choice]
]
)
if your_initial_choice == door_with_the_car:
wins += 1
else:
losses += 1
win_pct = wins / (wins + losses)
print(
'After {} rounds, you won {}% of the time keeping your choice.'.format(
n, win_pct
)
)
# Reset the wins and losses
wins = 0
losses = 0
# You change your door selection
for i in range(n):
door_with_the_car = random.choice(doors)
your_initial_choice = random.choice(doors)
door_host_reveals = random.choice(
[
door for door in doors
if door not in [door_with_the_car, your_initial_choice]
]
)
if your_initial_choice != door_with_the_car:
wins += 1
else:
losses += 1
win_pct = wins / (wins + losses)
print(
'After {} rounds, you won {}% of the time switching your choice.'.format(
n, win_pct
)
)