import random
parameters = [(7, 3),
(7, 4),
(7, 5),
(8, 3),
(8, 4),
(8, 5),
]
num_trials = 100000
for N, n in parameters:
stay_wins = 0
switch_wins = 0
for t in range(num_trials):
doors = set(range(N))
prize = random.sample(doors, 1)
first_pick = random.sample(doors, 1)
openable_doors = doors - set(prize + first_pick)
opened = random.sample(openable_doors, n)
switchable_doors = doors - set(opened + first_pick)
switched_pick = random.sample(switchable_doors, 1)
if prize == first_pick:
stay_wins += 1
if prize == switched_pick:
switch_wins += 1
ratio = float(switch_wins) / stay_wins
theoretical_ratio = float(N - 1) / (N - n - 1)
print 'N={0}, n={1}\t {2} {3} {4:.3f} {5:.3f}'.format(N,
n,
switch_wins,
stay_wins,
ratio,
theoretical_ratio,
)
N=7, n=3 28631 14403 1.988 2.000 N=7, n=4 42844 14208 3.015 3.000 N=7, n=5 86043 13957 6.165 6.000 N=8, n=3 21812 12432 1.755 1.750 N=8, n=4 28901 12493 2.313 2.333 N=8, n=5 43843 12475 3.514 3.500