import numpy as np
import numpy.random as rand
import matplotlib.pyplot as plt
set_Doors = [1,2,3] # for reference
# let 1 be our first choice
# if thats correct
# Then monty will reveal 2 or 3
# if not
# he will reveal 1 or the door without the prize (1 or 2 || 1 or 3)
# we arbitrarily labeled the doors 1,2,3 so we can solve for an initial guess of 1 and the odds will be the same no matter which door we actualy open first
def montyHallSim(N=10,change = False):
'''
Input number of runs (N) and a boolean representing if we writs doors (change).
Output numpy array of wins (1), and losses (0).
'''
resultArray = [] # 1 for win, 0 for lose
for _ in range(N):
choice = 1
truePrize = rand.randint(1,4)
if truePrize == choice:
# if prize behind first choice, randomly reveal one of the other 2 doors
reveal = rand.randint(2,4)
else:
# if prize not behind chosen door, reveal the remaining door
reveal = 6/(choice*truePrize)
if change:
# only change the choice to non-revealed door if strat set to True
choice = 6/(choice*reveal)
else:
pass
if choice == truePrize:
# Check if we won!
resultArray.append(1)
else:
# Check if we lost ):
resultArray.append(0)
return np.array(resultArray)
runs = 10000
# Run simulatiom
results_strat1 = montyHallSim(runs)
results_strat2 = montyHallSim(runs,change = True)
# Calculate the odds we experienced
percent_correct_strat1 = np.count_nonzero(results_strat1)/len(results_strat1)
percent_correct_strat2 = np.count_nonzero(results_strat2)/len(results_strat2)
# Display results
print("After",runs,"games we observe")
print("Strat 1 odds:",np.around(percent_correct_strat1*100,0),"%")
print("Strat 2 odds:",np.around(percent_correct_strat2*100,0),"%")
After 10000 games we observe
Strat 1 odds: 33.0 %
Strat 2 odds: 67.0 %