Read other letters about this article
This is written in a console app in Visual Basic Express, which is a really cool IDE and FREE to download from Microsoft.
'In the Monty Hall Game
'you are given three doors, behind
'one door is a brand new Cadillac,
'behind the other two doors are goats.
'You win the Cadillac if you can guess
'which door the car is behind.
'(You would rather win a Cadillac than a goat, wouldn't you?)
'Before opening the door you've picked,
'Monty Hall opens one of the other two doors
'and shows you a goat.
'Now there are two closed doors.
'Monty then gives you a choice;
'You can stay with your original pick
'Or you can switch to the other door.
'Now, what should you do?
'Most people assume the chances are equal either way.
'This program proves that you should switch doors
'because then your chances of winning the Cadillac
'will in fact be two out of three,
'as opposed to one out of three.
'
'--Believe it or not--
Sub Main()
'declare the values
Dim x, car, guess1, choice, WinStay, LoseStay, WinSwitch, LoseSwitch As Integer
Dim strinput As String
'seed the random generator
Randomize()
'loop 2000 times
For x = 1 To 2000
'pick a random number from one to three
car = CInt(Int((3 * Rnd()) + 1))
'this sets the door the car is behind
'pick another random number from one to three
guess1 = CInt(Int((3 * Rnd()) + 1))
'this is the player's first guess
'let's make the player chose to switch half the time
'this will give 1000 trials for each option
If (x Mod 2) = 0 Then
choice = 1
Else
choice = 2
End If
'now see who wins and collect results
If choice = 1 Then 'choice is to stay
If car = guess1 Then
'wins the cadillac
WinStay = WinStay + 1
Else
'loses and wins a goat - maybe
LoseStay = LoseStay + 1
End If
Else 'choice 2 to switch
'Hey, you know what? The door opening business
'doesn't make a damn bit of difference.
'It's a non-event that can't even be coded!
'The fact is, Switchers get to look in TWO doors!
'That's why switching should win two out of three times.
'So:
If car = guess1 Then
'shouldn't have switched
LoseSwitch = LoseSwitch + 1
Else
'the car was in one of the two other doors
WinSwitch = WinSwitch + 1
End If
End If
Next x
'stop looping and display results
Console.WriteLine(("Staying wins ") + CStr(WinStay) + (" times out of 1000 and loses ") + CStr(LoseStay) + (" times."))
Console.WriteLine(("Staying wins ") + CStr(WinSwitch) + (" times out of 1000 and loses ") + CStr(LoseSwitch) + (" times."))
'halt console so you can read results
strInput = UCase(Console.ReadLine())
End Sub
Note: there's nothing here about computers as such, nothing about 'popping the stack' or anything else. This is just an example of using coding to think through a problem. IMV, EVERYONE should be able to code like this. That's what Basic is good for, not to delve into computer basics, but to be a readily understandable and univeral programming language.
Programming, let us note, is something that is completely distinct from controlling computers. In fact, most people really don't want to know what is going on inside their computers and really don't need to know. But they should know how to program.