forked from quantslob/monty_hall_simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
46 lines (26 loc) · 1.05 KB
/
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
############ Run the following code in Python3 session. ~Quantslob
import datetime
import random
n = 3 ### number of doors
r = 1 ### number of losing doors revealed by host, must be > 0 AND < n - 1
nn = 50000 #### total number of simulations
#############
os_wins = [0] * nn
sw_wins = [0] * nn
d_space = list( range(1, n+1) )
xxnow = datetime.datetime.now()
kk = 0
while kk < nn:
door_prize = random.sample(d_space, 1)
door_select = random.sample(d_space, 1)
doors_can_reveal = set(d_space) - set( (door_prize + door_select) )
doors_revealed = random.sample(doors_can_reveal, r)
doors_can_switch = set(d_space) - set( (doors_revealed + door_select) )
door_switch = random.sample(doors_can_switch, 1)
os_wins[kk] = door_prize == door_select
sw_wins[kk] = door_prize == door_switch
kk += 1
print("proportion original selection wins: ", sum(os_wins) / nn)
print("proportion switching wins: ", sum(sw_wins) / nn)
yynow = datetime.datetime.now()
print("run time: ", (yynow - xxnow).total_seconds(), "secs")