The Monty Hall Problem

Statement of the Problem

The Monty Hall problem involves a classical game show situation and is named, of course, after Monty Hall, the long-time host of the show Let's Make a Deal. There are three doors labeled 1, 2, and 3. A pot of gold is behind one of the doors, while goats are behind the other two:

 

The rules are as follows:

  1. The player selects a door.
  2. The host selects a different door and opens it.
  3. The host gives the player the option of switching from her original choice to the remaining closed door.
  4. The door finally selected by the player is opened and she either wins or loses.

The Monty Hall problem became the subject of intense controversy because of several articles by Marilyn Vos Savant in the Ask Marilyn column of Parade magazine, a popular Sunday newspaper supplement. The controversy began when a reader posed the problem in the following way:

Suppose you're on a game show, and you're given a choice of three doors. Behind one door is a (pot of gold); 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?

Marilyn's response was that the contestant should switch doors, claiming that there is a 1/3 chance that the gold is behind door 1, while there is a 2/3 chance that the car is behind door 2. In two follow-up columns, Marilyn printed a number of responses, some from academics, most of whom claimed in angry or sarcastic tones that she was wrong and that there are equal chances that the car is behind doors 1 or 2. Marilyn stood by her original answer and offered additional, but non-mathematical, arguments.

Here is a version of the Monty Hall problem  Play with it and see if you can answer the question.

1. Setting up

a. Monty picks the winning door
b. Pictures initialize
c. counters initialize
d. Monty asks you to “Pick a Door”
 

2. You pick a door

3. Monty shows you what’s behind a door (can’t be winning door or one you picked!)

4. Monty offer you the chance to switch doors

5. You decide if you want to switch

6. Monty shows the winning door and tells you if you win

7. If you win, then win counter is incremented, else lose counters initialize

Interface and Properties

For the interface, you will need to download 5 pictures. You need one copy of the donkey and one of the pot of gold (above) and one each of the numbered doors (picNum1, picNum2 and picNum3) below. Just right-click and select Save Picture As from the context menu. Save them to your desktop or somewhere you can find them!

  picNum1 PicNum2 PicNum3

Before we start adding controls, we will add the downloaded pictures into the project resource file. If you don't know how to do this, read these instructions.

Now design an interface with three picture boxes, 5 labels, 3 picture boxes and 5 buttons. Set the properties as follows:

Control

Property

Value

frmForm1

Text

The Monty Hall Problem

Picture box

Name

picDoor1

 

Image

My.Resources.picNum1

Picture box

Name

picDoor2

 

Image

My.Resources.picNum2

Picture box

Name

picDoor3

 

Image

My.Resources.picNum3

Label

Name

lblMontySex

 

Text

blank

Button

Name

btnReset

 

Text

Reset Counters

Label

Name

not needed

 

Text

Win

Label

Name

lblWin

 

Text

blank

Label

Name

not needed

 

Text

Lose

Label

Name

lblLose

 

Text

blank

Button

Name

btnPlay

 

Text

Play

Button

Name

btnSwitch

 

Text

Switch

Button

Name

bthKeep

 

Text

Keep

Button

Name

btnExit

 

Text

Exit

The final interface looks like this:

Writing the Code

The following code goes behind the Door 1 picture box.

Private Sub picDoor1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picDoor1.Click

If WinningDoor = 1 Then

picDoor2.Image = My.Resources.donkey
ShowDoor = 2
SwitchDoor = 3

ElseIf WinningDoor = 2 Then

picDoor3.Image = My.Resources.donkey
ShowDoor = 3
SwitchDoor = 2

Else

picDoor2.Image = My.Resources.donkey
ShowDoor = 2
SwitchDoor = 3

End If

lblMontySez.Text = "You picked Door #1" & vbCrLf & "I will show you door " & ShowDoor & vbCrLf & "If you want to switch, press the button. Otherwise click Keep"

End Sub

The code is similar for picDoor2 and picDoor3. This does not mean the coding is easy- you really have to think about how the program works!

The code for btnSwitch is:

Private Sub btnSwitch_Click

If WinningDoor = SwitchDoor Then

lblMontySez.Text = "The winning door was " & WinningDoor & " you picked " & PickedDoor & " you win"
lblWin.Text = Str(Val(lblWin.Text) + 1)

Else

lblMontySez.Text = "You switched to " & SwitchDoor & " you lose"
lblLose.Text = Str(Val(lblLose.Text) + 1)

End If

If WinningDoor = 1 Then

picDoor1.Image = picGold.Image

ElseIf WinningDoor = 2 Then

picDoor2.Image = picGold.Image

Else

picDoor3.Image = picGold.Image

End If

End Sub

The code for "Keep" is similar.

Run the program 20 or 30 times- see if you can determine the correct strategy. Upload your final project to Blackboard.

Problem 2

Write a program in Visual Basic to simulate the Monty Hall problem. To do this, the computer will be both "Monty" and the player. The program will randomly assign the correct door and the players choice.

To create a simulation, we must have an event repeated over and over. In traditional programming languages, this is usually done by creating a loop. However, VB has a control that is much easier to use, the Timer. Think of the timer as a stop watch. When it is turned on, it "ticks." These ticks are a programmable event in VB. This means that we can attach code to the timer and it will be executed each time the clock ticks. We control the timer (turn it on and off) by using a button. The button sends a message to the timer to turn on (become enabled) or turn off (become disabled).

To turn on the timer: Timer1.Enabled = True
To turn off the time:
Timer1.Enabled = False


Design the form


 

Set the Properties

Object Name Caption Other
Label for displaying wins Name = lblWins 0  
Label for displaying loses Name = lblLoses 0  
Label "Wins" not necessary Win  
Label "Losses" not necessary Lose  
Switch Button Name = btnKeepSwitch Switch  
Run Button Name = btnRunStop Run  
Clear Button Name = btnClear Clear  
Timer (not visible) Name = Timer1 None Interval = 1

Write the Code

The Run/Stop button controls the timer. It turns the timer on or off and also changes the caption of the RunStop button.

If Timer1.Enabled Then 'if the timer is enables

Timer1.Enabled = False 'turn it off
btnRunStop.Text = "Run"
'change the text property of the RunStop button to "Run"

Else

Timer1.Enabled = True 'otherwise, turn it on
btnRunStop = "Stop"
'change the text property of the RunStop button to "Stop"

End If
 

The Keep/Switch button determines if you are doing a switch or keep

 If btnKeepSwitch.Text.Caption = "Keep" Then

btnKeepSwitch.Text.Caption = "Switch"

Else

btnKeepSwitch.Text.Caption = "Keep"

End If

The Clear button simply puts zeros in the Wins ans Losses labels


lblWin.Text = "0"
lblLoses.Text = "0"

Most of the action takes place in the timer. Every time it ticks, a game is "played" and the result tallied.

Dim PlayerWins, PlayerLoses As Integer
WinningDoor = Int(Rnd() * 3) + 1 'Random Winning Door
PlayerDoor = Int(Rnd() * 3) + 1 'Random Player Door

 If btnStrategy.Text = "Keep" Then

If WinningDoor = PlayerDoor Then

lblWin.Text = Str(Val(lblWin.Text) + 1)

Else

lblLose.Text = Str(Val(lblLose.Text) + 1)

End If

Else

If WinningDoor <> PlayerDoor Then

lblWin.Text = Str(Val(lblWin.Text) + 1)

Else

lblLose.Text = Str(Val(lblLose.Text) + 1)

End If

End If
 

Run a couple of thousand games with a switch strategy and several thousand with a keep strategy. Are you convinced about the correct strategy?

Home Intro to VB 2005 Variable & Constants Decision Structures Loops The String Class Monty Hall Problem Procedures Subprograms Word Processor Project Moon 1 Moon 2 Math Functions Arrays Two-Dimension Arrays Structures Creating Classes Inheritance/Polymorphism ASP.NET Form Validation Using FrontPage History Message Boxes