A computer simulation of the Monty Hall problem

The Monty Hall is a famous problem, which can be stated as follows: 

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?

The answer is counter-intuitive; yes, it is advantageous to switch. In fact, if you switch, you have doubled your chances to win. The theory may be difficult to grasp at first, so a computer simulation should verify the answer. 

Confusion everywhere

The solution of the problem can be found everywhere in the internet (e.g. here) and I do not want to replicate it. It turns out that the important point is that the host knows what's behind the doors. I have written a computer simulation of this game, which is given below. The code is not optimized, as I only needed to follow the steps of the game and examine all different solutions. The winning door, as well as the pick of the player, are chosen randomly using a random number generator:

    Private Sub TheHostKnowsThePlayerSwitches()

 

        Dim gCounter As Long = 1000000 'a million games

        Dim gA As Long

        Dim gWins As Long

        Dim gWinner As Long 'the winner number 1,2,3

        Dim gPick As Long   'the number the player picks

 

        Dim Generator As System.Random = New System.Random()

 

        For gA = 1 To gCounter

 

            'pick a winner

            gWinner = GetRandom(Generator, 1, 3)

 

            'the player picks at random

            gPick = GetRandom(Generator, 1, 3)

 

            Select Case gPick

                Case 1

                    'one of 2,3 is a loser, show it

                    If gWinner <> 2 Then

                        'show 2, the player switches to 3

                        If gWinner = 3 Then gWins += 1

                    ElseIf gWinner <> 3 Then

                        'show 3, the player switches to 2

                        If gWinner = 2 Then gWins += 1

                    End If

                Case 2

                    'one of 1,3 is a loser, show it

                    If gWinner <> 1 Then

                        'show 1, the player switches to 3

                        If gWinner = 3 Then gWins += 1

                    ElseIf gWinner <> 3 Then

                        'show 3, the player switches to 1

                        If gWinner = 1 Then gWins += 1

                    End If

                Case 3

                    'one of 1,2 is a loser, show it

                    If gWinner <> 1 Then

                        'show 1, the player switches to 2

                        If gWinner = 2 Then gWins += 1

                    ElseIf gWinner <> 2 Then

                        'show 2, the player switches to 1

                        If gWinner = 1 Then gWins += 1

                    End If

            End Select

 

        Next gA

 

        MsgBox("The win ratio for the switching strategy and a host that knows is " & System.Convert.ToDouble(gWins / gCounter).ToString)

 

    End Sub

It turns out that Microsoft thought it was a good idea to return the next random integer with the minimum bound included and the maximum excluded! This fixes this problem:

    Private Function GetRandom(myGenerator As System.Random, ByVal Min As Long, ByVal Max As Long) As Long

        'min is inclusive, max is exclusive (dah!)

        Return myGenerator.Next(Min, Max + 1)

    End Function 

Running the code provides the verdict: if the player switches, he wins with a probability 2/3. If he sticks to his initial pick, the probability is only half: 1/3! The code for the no-switch strategy is given below:

 

    Private Sub TheHostKnowsThePlayerDoesntSwitch()

 

        Dim gCounter As Long = 1000000 'a million games

        Dim gA As Long

        Dim gWins As Long

        Dim gWinner As Long 'the winner number 1,2,3

        Dim gPick As Long   'the number the player picks

 

        Dim Generator As System.Random = New System.Random()

 

        For gA = 1 To gCounter

 

            'pick a winner

            gWinner = GetRandom(Generator, 1, 3)

 

            'the player picks at random

            gPick = GetRandom(Generator, 1, 3)

 

            Select Case gPick

                Case 1

                    'one of 2,3 is a loser, show it

                    If gWinner <> 2 Then

                        'show 2, the player sticks to 1

                        If gWinner = 1 Then gWins += 1

                    ElseIf gWinner <> 3 Then

                        'show 3, the player sticks to 1

                        If gWinner = 1 Then gWins += 1

                    End If

                Case 2

                    'one of 1,3 is a loser, show it

                    If gWinner <> 1 Then

                        'show 1, the player sticks to 2

                        If gWinner = 2 Then gWins += 1

                    ElseIf gWinner <> 3 Then

                        'show 3, the player sticks to 2

                        If gWinner = 2 Then gWins += 1

                    End If

                Case 3

                    'one of 1,2 is a loser, show it

                    If gWinner <> 1 Then

                        'show 1, the player sticks to 3

                        If gWinner = 3 Then gWins += 1

                    ElseIf gWinner <> 2 Then

                        'show 2, the player sticks to 3

                        If gWinner = 3 Then gWins += 1

                    End If

            End Select

 

        Next gA

 

        MsgBox("The win ratio for the no-switch strategy and a host that knows is " & System.Convert.ToDouble(gWins / gCounter).ToString)

 

    End Sub

Note that if the host is actually clueless about where the winning door is, then the switching strategy is not advantageous! The player wins with an overall probability equal to 1/3 whether switching or sticking to his initial choice. Of course, this means that occasionally the host will reveal the winning door (instead of a goat). The code for this is given below:

    Private Sub TheHostIsCluelessThePlayerSwitches()

 

        Dim gCounter As Long = 1000000 'a million games

        Dim gA As Long

        Dim gWins As Long

        Dim gWinner As Long 'the winner number 1,2,3

        Dim gPick As Long   'the number the player picks

 

        Dim Generator As System.Random = New System.Random()

 

        For gA = 1 To gCounter

 

            'pick a winner

            gWinner = GetRandom(Generator, 1, 3)

 

            'the player picks at random

            gPick = GetRandom(Generator, 1, 3)

 

            Select Case gPick

                Case 1

                    'show one of the other two randomly (the host is clueless)

                    If GetRandom(Generator, 1, 2) = 1 Then

                        'show 2

                        If gWinner = 2 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 3

                            If gWinner = 3 Then gWins += 1

                        End If

                    Else

                        'show 3

                        If gWinner = 3 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 2

                            If gWinner = 2 Then gWins += 1

                        End If

                    End If

                Case 2

                    'show one of the other two randomly (the host is clueless)

                    If GetRandom(Generator, 1, 2) = 1 Then

                        'show 1

                        If gWinner = 1 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 3

                            If gWinner = 3 Then gWins += 1

                        End If

                    Else

                        'show 3

                        If gWinner = 3 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 1

                            If gWinner = 1 Then gWins += 1

                        End If

                    End If

                Case 3

                    'show one of the other two randomly (the host is clueless)

                    If GetRandom(Generator, 1, 2) = 1 Then

                        'show 1

                        If gWinner = 1 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 2

                            If gWinner = 2 Then gWins += 1

                        End If

                    Else

                        'show 2

                        If gWinner = 2 Then

                            'sorry, you lose, host wins by showing the car

                        Else

                            'the player switches to 1

                            If gWinner = 1 Then gWins += 1

                        End If

                    End If

            End Select

 

        Next gA

 

        MsgBox("The win ratio for the switching strategy and clueless host is " & System.Convert.ToDouble(gWins / gCounter).ToString)

 

    End Sub

Conclusion

By writing the code for the simulation, a simple explanation for the Monty Hall problem arises. In the beginning, there are two possible outcomes of the game: (a) the player randomly picks the winning door, with a probability 1/3 and (b) the winning door is one of the other two, with a probability 1-1/3=2/3. By showing a goat, the host removes the losing option when (b) is true. This means that by switching, the player gets the full 2/3 probability of success without need to further multiply it by 1/2 to account for the two doors of (b).