Code:
Module MontyHall
Sub Main()
Dim aRnd As New Random
Dim Doors() As String = {"Goat", "Goat", "Goat"} '//put 3 Goats behind 3 doors
Dim intDoorWithCar As Integer '//Number of the Door with the Car
Dim intDoorChosen As Integer '//Number of the Door we choose
Dim intChosenDoorWinCount As Integer = 0 '//Accumulated wins sticking with original chosen door
Dim intAlternateDoorWinCount As Integer = 0 '//Accumulated wins swapping to alternate door
Dim intNumIterations As Integer = 100000
For intI As Integer = 1 To intNumIterations
intDoorWithCar = aRnd.Next(3) '//Randomly replace one of the
Doors(intDoorWithCar) = "Car" '// 3 Goats with a Car
intDoorChosen = aRnd.Next(3) '//Choose one of the 3 doors at random
If intDoorChosen = intDoorWithCar Then intChosenDoorWinCount += 1 Else intAlternateDoorWinCount += 1
Doors(intDoorWithCar) = "Goat" '//Replace the Car with a Goat
Next intI '// and go around again
Console.WriteLine("Percentage probability of winning Car:")
Console.WriteLine("Keeping Original Choice of Door = " & Format(intChosenDoorWinCount / intNumIterations, "Percent"))
Console.WriteLine("Swapping Choice to the Other Door = " & Format(intAlternateDoorWinCount / intNumIterations, "Percent"))
Console.ReadKey(True)
End Sub
End Module
We choose one of the 3 doors at random.
Monty removes one of the remaining 2 doors
that has a goat behind it.
We don't need to know which door, just that a goat
has been removed and now we only have 2 doors left
- A goat behind one and a Car behind the other, and
that our chosen door is one of them.
The If/THEN/ELSE line of the code works as follows:
If we stick with our choice and win, then
increase the count of wins for the Chosen Door.
(in the "Swapping Doors Scenario" we would have lost)
If we stick with our choice and lose, then in the
"Swapping Doors Scenario" we would have won, so
increase the count of wins for the Alternate Door.