mvidas
06-23-2006, 10:50 AM
Hi everyone,
As stated above, this is more for discussion than a question, or even just something to think about.
So I stumbled across this article in the wikipedia:
http://en.wikipedia.org/wiki/Monty_Hall_problem
Givens: 3 doors to choose from. 1 contains a car, the other 2 contain a goat. You make your choice of a door. After you choose, one of the remaining doors is revealed to you to show a goat. You then have the option of either staying with your original choice, or switching to the other unopened door.
Presumption from the wikipedia link above: You have a better chance of getting the car if you switch.
Though it did make sense, I still couldn't really believe it. After you are shown a door, there are 2 left. One with a car, one with a goat. Common sense tells me its like flipping a coin: the past means nothing, should be a 50/50 chance. But there are some massive mathematical formulas on the link that seem to say otherwise. Being as I often think in code, I figured I'd put my thoughts down in the vbe and run a few tests. To my surprise, it was exactly as they said; if you switch your guess when there are only 2 left, you have a higher chance than if you stay. Who knew!
I'm secretly hoping I have a mistake in logic here, if you can find it please tell me!Sub MontyHallProblem()
'http://en.wikipedia.org/wiki/Monty_Hall_problem
Dim TheDoors(1 To 3) As String, SheetData(1 To 50001, 1 To 7)
Dim i As Long, j As Long, WinCount As Long
Dim TheCar As Long, GimmeGoat As Long
Dim InitialGuess As Long, SwitchGuess As Long
'Test this 50,000 times
For i = 1 To 50000
'unnecessary, but I still stuck it in here
Randomize Int(Rnd() * Int(Rnd() * 10000000 + 1) + 1)
'Initialize doors
TheDoors(1) = "GOAT"
TheDoors(2) = "GOAT"
TheDoors(3) = "GOAT"
'Make your guess
InitialGuess = Int(Rnd() * 3 + 1)
'Put the car in one of the doors
TheCar = Int(Rnd() * 3 + 1)
TheDoors(TheCar) = "CAR"
'Choose the "Gimme Goat"
GimmeGoat = Int(Rnd() * 3 + 1)
Do Until GimmeGoat <> TheCar And GimmeGoat <> InitialGuess
GimmeGoat = Int(Rnd() * 3 + 1)
Loop
'Assign the door variable if you switch your door after the gimme
SwitchGuess = Int(Rnd() * 3 + 1)
Do Until SwitchGuess <> GimmeGoat And SwitchGuess <> InitialGuess
SwitchGuess = Int(Rnd() * 3 + 1)
Loop
'Populate array with data and outcome
SheetData(i, 1) = TheDoors(1)
SheetData(i, 2) = TheDoors(2)
SheetData(i, 3) = TheDoors(3)
SheetData(i, 4) = InitialGuess
SheetData(i, 5) = GimmeGoat
SheetData(i, 6) = IIf(TheDoors(InitialGuess) = "CAR", "YOU WIN!", "")
SheetData(i, 7) = IIf(TheDoors(SwitchGuess) = "CAR", "YOU WIN!", "")
Next
'Count the wins for each, put in last row of array
For i = 6 To 7
WinCount = 0
For j = 1 To 50000
If SheetData(j, i) = "YOU WIN!" Then WinCount = WinCount + 1
Next
SheetData(50001, i) = Format(WinCount, "#,##0 cars won")
Next
'Put the data into a worksheet
Application.ScreenUpdating = False
Workbooks.Add 1
Range("A1:G1").Value = Array("Door 1", "Door 2", "Door 3", "Your Guess", _
"GimmeGoat", "If you stay", "If you switch")
Range("A2:G50002").Value = SheetData
Columns.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
Range("G50002").Select
End Sub
As Fridays are often a little slower around here, why not?
EDIT: For what it was worth, I tried fixing my initial guess to always choose door #1 in the beginning:
'Make your guess
InitialGuess = 1 'Int(Rnd() * 3 + 1)
No change to the outcome.
Matt
As stated above, this is more for discussion than a question, or even just something to think about.
So I stumbled across this article in the wikipedia:
http://en.wikipedia.org/wiki/Monty_Hall_problem
Givens: 3 doors to choose from. 1 contains a car, the other 2 contain a goat. You make your choice of a door. After you choose, one of the remaining doors is revealed to you to show a goat. You then have the option of either staying with your original choice, or switching to the other unopened door.
Presumption from the wikipedia link above: You have a better chance of getting the car if you switch.
Though it did make sense, I still couldn't really believe it. After you are shown a door, there are 2 left. One with a car, one with a goat. Common sense tells me its like flipping a coin: the past means nothing, should be a 50/50 chance. But there are some massive mathematical formulas on the link that seem to say otherwise. Being as I often think in code, I figured I'd put my thoughts down in the vbe and run a few tests. To my surprise, it was exactly as they said; if you switch your guess when there are only 2 left, you have a higher chance than if you stay. Who knew!
I'm secretly hoping I have a mistake in logic here, if you can find it please tell me!Sub MontyHallProblem()
'http://en.wikipedia.org/wiki/Monty_Hall_problem
Dim TheDoors(1 To 3) As String, SheetData(1 To 50001, 1 To 7)
Dim i As Long, j As Long, WinCount As Long
Dim TheCar As Long, GimmeGoat As Long
Dim InitialGuess As Long, SwitchGuess As Long
'Test this 50,000 times
For i = 1 To 50000
'unnecessary, but I still stuck it in here
Randomize Int(Rnd() * Int(Rnd() * 10000000 + 1) + 1)
'Initialize doors
TheDoors(1) = "GOAT"
TheDoors(2) = "GOAT"
TheDoors(3) = "GOAT"
'Make your guess
InitialGuess = Int(Rnd() * 3 + 1)
'Put the car in one of the doors
TheCar = Int(Rnd() * 3 + 1)
TheDoors(TheCar) = "CAR"
'Choose the "Gimme Goat"
GimmeGoat = Int(Rnd() * 3 + 1)
Do Until GimmeGoat <> TheCar And GimmeGoat <> InitialGuess
GimmeGoat = Int(Rnd() * 3 + 1)
Loop
'Assign the door variable if you switch your door after the gimme
SwitchGuess = Int(Rnd() * 3 + 1)
Do Until SwitchGuess <> GimmeGoat And SwitchGuess <> InitialGuess
SwitchGuess = Int(Rnd() * 3 + 1)
Loop
'Populate array with data and outcome
SheetData(i, 1) = TheDoors(1)
SheetData(i, 2) = TheDoors(2)
SheetData(i, 3) = TheDoors(3)
SheetData(i, 4) = InitialGuess
SheetData(i, 5) = GimmeGoat
SheetData(i, 6) = IIf(TheDoors(InitialGuess) = "CAR", "YOU WIN!", "")
SheetData(i, 7) = IIf(TheDoors(SwitchGuess) = "CAR", "YOU WIN!", "")
Next
'Count the wins for each, put in last row of array
For i = 6 To 7
WinCount = 0
For j = 1 To 50000
If SheetData(j, i) = "YOU WIN!" Then WinCount = WinCount + 1
Next
SheetData(50001, i) = Format(WinCount, "#,##0 cars won")
Next
'Put the data into a worksheet
Application.ScreenUpdating = False
Workbooks.Add 1
Range("A1:G1").Value = Array("Door 1", "Door 2", "Door 3", "Your Guess", _
"GimmeGoat", "If you stay", "If you switch")
Range("A2:G50002").Value = SheetData
Columns.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
Range("G50002").Select
End Sub
As Fridays are often a little slower around here, why not?
EDIT: For what it was worth, I tried fixing my initial guess to always choose door #1 in the beginning:
'Make your guess
InitialGuess = 1 'Int(Rnd() * 3 + 1)
No change to the outcome.
Matt