The Monty Hall puzzle (named after the original host of Lets Make a Deal) offers contestants three identical doors. One of these doors leads to the contestant winning a new car, while the other two lead to rooms containing goats. The contestant selects a door, and the host immediately opens up one of the two unselected doors, revealing a goat. The contestant is then asked if they wish to change their selection.
It turns out that the contestant should always change doors; that there is a 2/3 chance of winning the car if they do so. However, I was unconvinced of this fact at first (because it is so counterintuitive), so I programmed up the following simulation to demonstrate the phenomenon:
import random # set the random number seed, for repeatability random.seed(1) # set number of simulations, and define the set of outcomes for each door n = 100000 set = ['car', 'goat', 'goat'] # # run the simulation # switch_list_success =  not_switch_list_success =  for n in range(n): # randomly assign the car and goats to the three doors doors = random.sample(set, 3) # simulate the picking of the first door first_pick = random.sample([0, 1, 2], 1) # determine which door gets opened location_of_goats = [item for item in range(len(doors)) if doors[item] == 'goat'] if first_pick == location_of_goats: opened_door = location_of_goats if first_pick == location_of_goats: opened_door = location_of_goats if not first_pick in location_of_goats: opened_door = random.sample(location_of_goats, 1) # determine which doors remain remaining_doors = [x for x in range(len(doors)) if x != opened_door] # figure out which of the remaining doors was not already selected non_first_pick_remaining_door = [x for x in remaining_doors if x != first_pick] # consequences of switching doors if doors[non_first_pick_remaining_door] == 'car': switch_list_success.append('car') # consequences of not switching doors if doors[first_pick] == 'car': not_switch_list_success.append('car') print print 'Probability of winning due to switching: ', float(len(switch_list_success)) / float(n) print 'Probability of winning by not switching: ', float(len(not_switch_list_success)) / float(n) print
The program works by repeatedly randomly arranging a car and two goats behind three virtual doors. Then, for each repetition, a door is selected at random and a new door containing a goat is opened, Finally, the remaining doors are tested for presence of the car, and notation is made whether the first selection produced the car or whether switching doors would produce the car. By repeating this process 100,000 times we estimate the long run probability of winning the car based on the strategies of switching or not switching door selection.
The simulation results verify that the probability of winning the car is 2/3 if the selected door is switched:
The image of three doors with the goat in the third door comes from http://en.wikipedia.org/wiki/Monty_Hall_problem.