Of Goats and Porsches: A Monty Hall Paradox Simulator

The Monty Hall Paradox can’t be right, or so I thought.

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. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?

So I wrote the below simulator to find out. It uses the Monte Carlo Technique, which is a nice way of solving math problems numerically.
The results from my run are pretty conclusive:

Twitchy winners : 653
Twitchy losers : 347
Stubborn winners : 318
Stubborn losers : 682

So it really does pay to switch doors when the host gives you the choice. Which is not what I had expected – and means it’s time for me to go eat some humble pie 🙁 .

Code listing below:

#!/bin/ksh
#######################################################
# monty_hall.sh
# Andrew Fraser ( http://andrewfraserdba.com )
# January 2007
# Simulator for the Monty Hall 'paradox'. See http://en.wikipedia.org/wiki/Monty_Hall_problem for background info
# Usage:
#  monty_hall.sh <no_iterations> <twitchy|stubborn>
#  e.g.: monty_hall.sh 1000 twitchy
# Parameters:
#   1 - NO_ITERATIONS - number of times to run simulation (a few hundred is reasonably fast on most machines)
#   2 - CONTESTANT_TYPE - twitchty = moves doors when given chance to move : stubborn = stays put when given chance to move
# Notes:
#   ksh RANDOM built in function is not the best random function in existence, although there are patches that resolve that.
#   RANDOM returns a number between 0 and 32768. The three doors here are named "0", "1", and "2", achieved by dividing by 10924
#
#   A good idea is to redirect the output for larger runs, and grep out the results. E.g.:
#     monty_hall.sh 1000 twitchy > twitchy.out
#     monty_hall.sh 1000 stubborn > stubborn.out
#     echo Twitchy winners : `grep Congratulations twitchy.out|wc -l`
#     echo Twitchy losers : `grep dear twitchy.out|wc -l`
#     echo Stubborn winners : `grep Congratulations stubborn.out|wc -l`
#     echo Stubborn losers : `grep dear stubborn.out|wc -l`
#######################################################

i=0

NO_ITERATIONS=$1
CONTESTANT_TYPE=$2

while [ $i -lt $NO_ITERATIONS ]
do

CONTESTANT_DOOR1=`echo $(( RANDOM / 10924 ))`
echo CONTESTANT_DOOR1 is $CONTESTANT_DOOR1
PORSCHE_DOOR=`echo $(( RANDOM / 10924 ))`
echo PORSCHE_DOOR is $PORSCHE_DOOR

if [ $CONTESTANT_DOOR1 -eq $PORSCHE_DOOR ]
then
RIGHT_FIRST_TIME=yes
#host now uncovers one (at random) of the two remaining doors to show a goat...
ZERO_OR_ONE=`echo $(( RANDOM / 16384 ))`
if [ $PORSCHE_DOOR = 0 ]
then
let UNCOVERED_GOAT_DOOR=1+ZERO_OR_ONE
elif [ $PORSCHE_DOOR = 1 ]
then
if [ $ZERO_OR_ONE = 0 ]
then
UNCOVERED_GOAT_DOOR=0
else
UNCOVERED_GOAT_DOOR=2
fi
elif [ $PORSCHE_DOOR = 2 ]
then
UNCOVERED_GOAT_DOOR=$ZERO_OR_ONE
fi
echo UNCOVERED_GOAT_DOOR is $UNCOVERED_GOAT_DOOR
else
RIGHT_FIRST_TIME=no
#host now uncovers the only other goat door to show a goat...
let UNCOVERED_GOAT_DOOR=3-CONTESTANT_DOOR1-PORSCHE_DOOR
echo UNCOVERED_GOAT_DOOR is $UNCOVERED_GOAT_DOOR
fi

if [ $CONTESTANT_TYPE = twitchy ]
then
# contestant moves to the other uncovered door, maybe cos she read something about that being good strategy on wikipedia...
if [ $RIGHT_FIRST_TIME = yes ]
then
echo ":( Oh dear, you won a goat, what a shame, you were right first time, if only you had stayed where you were! :("
else
echo ":) Congratulations! You've won a porsche :)"
fi
else
# contestant stubbornly stays where she is...
if [ $RIGHT_FIRST_TIME = yes ]
then
echo ":) Congratulations! You've won a porsche :)"
else
echo ":( Oh dear, you won a goat, what a shame, you should have moved door. :("
fi
fi

echo

let i=i+1

done

For the actual run I did, with results pasted above, I ran:

monty_hall.sh 1000 twitchy > twitchy.out
monty_hall.sh 1000 stubborn > stubborn.out
echo Twitchy winners : `grep Congratulations twitchy.out|wc -l`
echo Twitchy losers : `grep dear twitchy.out|wc -l`
echo Stubborn winners : `grep Congratulations stubborn.out|wc -l`
echo Stubborn losers : `grep dear stubborn.out|wc -l`

January 17, 2007

  • Funny, I was only discussing this with someone the other day. The way I pursuaded them that switching was better was to scale the whole thing up. If you had a million doors and you pick one, and 999,998 others are revealed to be “goats”, leaving only yours and one other, then clearly you would switch. Nice shell script though, you really must have too much time on your hands (although I did write a korn shell script to solve those tricky sudoku puzzles – OK, I admit it, I’m a geek at heart.)

  • We learned this in my statistics class. The rationale is that whether or not your first choice is actually a goat, Monte will always reveal another goat. If he didn’t know, you would have a 1/3 chance of getting it right. But because he does know, if you switch you now have a 1/2 chance. If you don’t switch you still have a 1/3 chance.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *