The Monty Hall Problem

Lately I’ve been brushing up my knowledge of probability and statistics and naturally came across the Monty Hall problem. According to Wikipedia, “no other statistical puzzle comes so close to fooling all the people all the time”. Even Paul ErdÅ‘s remained unconvinced until he was shown a computer simulation of the problem. This prompted me to try it myself, just so that I could marvel at the result:

import numpy as np

trials = 10000
count1 = 0
count2 = 0
doors = [0, 1, 2]

for i in range(trials):
    car_door = np.random.choice(doors)
    first_choice = np.random.choice(doors)
    if first_choice == car_door:
        count1 += 1

    other_doors_with_goat = [door for door in doors
                             if door != car_door
                             and door != first_choice]
    revealed_goat = np.random.choice(other_doors_with_goat)
    second_choice = [door for door in doors
                     if door not in [first_choice, revealed_goat]][0]
    if second_choice == car_door:
        count2 += 1

print('Chance of winning without switching: {:.2f}'.format(count1/trials*100))
print('Chance of winning with switching: {:.2f}'.format(count2/trials*100))