
Suppose you're on a game show and you're given the choice of three doors.

Behind one door is a car; behind the others, goats.

The car and the goats were placed randomly behind the doors before the show.



Rules of the game

After you have chosen a door, the door remains closed for the time being.

The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it.

If both remaining doors have goats behind them, he chooses one randomly.

After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door.

Imagine that you chose Door 1 and the host opens Door 3, which has a goat.

He then asks you "Do you want to switch to Door Number 2?"



The question

Is it to your advantage to change your choice?



Note

The player may initially choose any of the three doors (not just Door 1), that the host opens a different door revealing a goat (not necessarily Door 3), and that he gives the player a second choice between the two remaining unopened doors.



Task

Run random simulations of the Monty Hall game. Show the effects of a strategy of the contestant always keeping his first guess so it can be contrasted with the strategy of the contestant always switching his guess.

Simulate at least a thousand games using three doors for each strategy and show the results in such a way as to make it easy to compare the effects of each strategy.



References

 Stefan Krauss, X. T. Wang, "The psychology of the Monty Hall problem: Discovering psychological mechanisms for solving a tenacious brain teaser.", Journal of Experimental Psychology: General, Vol 132(1), Mar 2003, 3-22 DOI: 10.1037/0096-3445.132.1.3
 A YouTube video:   Monty Hall Problem - Numberphile. 


#!/bin/bash
# Simulates the "monty hall" probability paradox and shows results.
# http://en.wikipedia.org/wiki/Monty_Hall_problem
# (should rewrite this in C for faster calculating of huge number of rounds)
# (Hacked up by Éric Tremblay, 07.dec.2010)
 
num_rounds=10 #default number of rounds
num_doors=3 # default number of doors
[ "$1" = "" ] || num_rounds=$[$1+0]
[ "$2" = "" ] || num_doors=$[$2+0]
 
nbase=1 # or 0 if we want to see door numbers zero-based
num_win=0; num_lose=0
 
echo "Playing $num_rounds times, with $num_doors doors."
[ "$num_doors" -lt 3 ] && { 
  echo "Hey, there has to be at least 3 doors!!"
  exit 1
}
echo
 
function one_round() {
  winning_door=$[$RANDOM % $num_doors ]
  player_picks_door=$[$RANDOM % $num_doors ]
 
  # Host leaves this door AND the player's first choice closed, opens all others
  # (this WILL loop forever if there is only 1 door)
  host_skips_door=$winning_door
  while [ "$host_skips_door" = "$player_picks_door" ]; do 
    #echo -n "(Host looks at door $host_skips_door...) "
    host_skips_door=$[$RANDOM % $num_doors]
  done
 
  # Output the result of this round
  #echo "Round $[$nbase+current_round]: "
  echo -n "Player chooses #$[$nbase+$player_picks_door]. "
  [ "$num_doors" -ge 10 ] &&               
    # listing too many door numbers (10 or more) will just clutter the output
    echo -n "Host opens all except #$[$nbase+$host_skips_door] and #$[$nbase+$player_picks_door]. " \
  || {
    # less than 10 doors, we list them one by one instead of "all except ?? and ??"
    echo -n "Host opens"
    host_opens=0
    while [ "$host_opens" -lt "$num_doors" ]; do
      [ "$host_opens" != "$host_skips_door" ] && [ "$host_opens" != "$player_picks_door" ] && \
      echo -n " #$[$nbase+$host_opens]"
      host_opens=$[$host_opens+1]
    done
    echo -n " "
  }
  echo -n "(prize is behind #$[$nbase+$winning_door]) "
  echo -n "Switch from $[$nbase+$player_picks_door] to $[$nbase+$host_skips_door]: "
  [ "$winning_door" = "$host_skips_door" ] && {
    echo "WIN."
    num_win=$[num_win+1]
  } || {
    echo "LOSE."
    num_lose=$[num_lose+1]
  }
} # end of function one_round
 
# ok, let's go
current_round=0
while [ "$num_rounds" -gt "$current_round" ]; do
  one_round
  current_round=$[$current_round+1]
done
 
echo
echo "Wins (switch to remaining door):  $num_win"
echo "Losses (first guess was correct): $num_lose"
exit 0