# Monty Hall Problem Tester

## Recommended Posts

Monty Hall Problem - Computer Solution

You have a choice of 3 doors, and only 1 has a prize. You pick door #1 and the game show host gives you a hint that the prize is NOT in door #2. Do you switch your answer?

The answer is: If you SWITCH your choice you have a 66% chance of winning the prize, versus a 33% chance of winning with your current choice.

http://en.wikipedia.org/wiki/Monty_Hall_problem

Smart people told me the answer, but I had to make an AutoIt script to make sure.

*Gui for selecting your door and switching

*Tracking of wins/losses

*I am a beginner so I am sure this could be done much simpler, please be understanding

```Global \$wins = 0
Global \$losses = 0

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Func mhgui(\$wins, \$losses)
Global \$door1open = 0
Global \$door1prize = 0
Global \$door1selected = 0

Global \$door2open = 0
Global \$door2prize = 0
Global \$door2selected = 0

Global \$door3open = 0
Global \$door3prize = 0
Global \$door3selected = 0

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Global \$mhall = GUICreate("Monty Hall- Wins:" & \$wins & " Loss:" & \$losses, 615, 438, 192, 124)
GUISetOnEvent(\$GUI_EVENT_CLOSE, "forcequit")
Global \$lstatus = GUICtrlCreateLabel("lstatus", 136, 16, 346, 49, \$SS_CENTER)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global \$ldoor1 = GUICtrlCreateLabel("?", 88, 150, 130, 65, \$SS_CENTER)
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
Global \$ldoor2 = GUICtrlCreateLabel("?", 242, 150, 130, 65, \$SS_CENTER)
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
Global \$ldoor3 = GUICtrlCreateLabel("?", 400, 150, 130, 65, \$SS_CENTER)
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
\$Label4 = GUICtrlCreateLabel("Door 1", 128, 224, 52, 24, \$SS_CENTER)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
\$Label5 = GUICtrlCreateLabel("Door 2", 281, 224, 52, 24, \$SS_CENTER)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
\$Label6 = GUICtrlCreateLabel("Door 3", 440, 224, 52, 24, \$SS_CENTER)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
\$b1pick = GUICtrlCreateButton("Pick", 115, 112, 75, 25)
GUICtrlSetOnEvent(-1, "pickdoor1")
\$b2pick = GUICtrlCreateButton("Pick", 270, 112, 75, 25)
GUICtrlSetOnEvent(-1, "pickdoor2")
\$b3pick = GUICtrlCreateButton("Pick", 425, 112, 75, 25)
GUICtrlSetOnEvent(-1, "pickdoor3")
\$Label7 = GUICtrlCreateLabel("Would you like to switch?", 136, 280, 349, 25, \$SS_CENTER)
GUICtrlSetState(-1, 32)
\$byes = GUICtrlCreateButton("Yes", 224, 320, 67, 25)
GUICtrlSetOnEvent(-1, "switchyes")
GUICtrlSetState(-1, 32)
\$bno = GUICtrlCreateButton("No", 320, 320, 67, 25)
GUICtrlSetOnEvent(-1, "switchno")
GUICtrlSetState(-1, 32)
\$lwinlose = GUICtrlCreateLabel("Lose", 248, 360, 113, 57, \$SS_CENTER)
GUICtrlSetState(-1, 32)
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW, \$mhall)
#EndRegion ### END Koda GUI section ###

;begin experiment

;Randomize prize now
\$prizedoor = Random(1, 3, 1)
If \$prizedoor = 1 Then
\$door1prize = 1
ElseIf \$prizedoor = 2 Then
\$door2prize = 1
ElseIf \$prizedoor = 3 Then
\$door3prize = 1
EndIf

;select door
GUICtrlSetData(\$lstatus, "Wins:" & \$wins & " Loss:" & \$losses)
While \$door1selected = 0 And \$door2selected = 0 And \$door3selected = 0
Sleep(1000)
WEnd
GUICtrlSetState(\$b1pick, 32)
GUICtrlSetState(\$b2pick, 32)
GUICtrlSetState(\$b3pick, 32)
If \$door1selected = 1 Then
;MsgBox(0, "Monty Hall", "You chose: Door 1", 3)
GUICtrlSetData(\$ldoor1, "Picked")
ElseIf \$door2selected = 1 Then
;MsgBox(0, "Monty Hall", "You chose: Door 2", 3)
GUICtrlSetData(\$ldoor2, "Picked")
ElseIf \$door3selected = 1 Then
;MsgBox(0, "Monty Hall", "You chose: Door 3", 3)
GUICtrlSetData(\$ldoor3, "Picked")
EndIf

;Disable a door
MsgBox(0, "Monty Hall", "I will now remove a door that has no prize.", 0)
If \$door1selected = 0 And \$door1prize = 0 Then
GUICtrlSetState(\$ldoor1, 32)
GUICtrlSetState(\$Label4, 32)
GUICtrlSetData(\$lstatus, "I can tell you it is not door 1")
ElseIf \$door2selected = 0 And \$door2prize = 0 Then
GUICtrlSetState(\$ldoor2, 32)
GUICtrlSetState(\$Label5, 32)
GUICtrlSetData(\$lstatus, "I can tell you it is not door 2")
ElseIf \$door3selected = 0 And \$door3prize = 0 Then
GUICtrlSetState(\$ldoor3, 32)
GUICtrlSetState(\$Label6, 32)
GUICtrlSetData(\$lstatus, "I can tell you it is not door 3")
EndIf

;Would you like to switch
GUICtrlSetState(\$Label7, 16)
GUICtrlSetState(\$byes, 16)
GUICtrlSetState(\$bno, 16)
Global \$chosenswitch = 0
While \$chosenswitch = 0
Sleep(1000)
WEnd
GUICtrlSetState(\$Label7, 32)
GUICtrlSetState(\$byes, 32)
GUICtrlSetState(\$bno, 32)

;Disclose prize
If \$door1selected = 1 And \$door1prize = 1 Then
GUICtrlSetState(\$lwinlose, 16)
GUICtrlSetData(\$lstatus, "Door 1 had the prize the whole time")
GUICtrlSetData(\$lwinlose, "Win!")
iwin()
ElseIf \$door2selected = 1 And \$door2prize = 1 Then
GUICtrlSetState(\$lwinlose, 16)
GUICtrlSetData(\$lstatus, "Door 2 had the prize the whole time")
GUICtrlSetData(\$lwinlose, "Win!")
iwin()
ElseIf \$door3selected = 1 And \$door3prize = 1 Then
GUICtrlSetState(\$lwinlose, 16)
GUICtrlSetData(\$lstatus, "Door 3 had the prize the whole time")
GUICtrlSetData(\$lwinlose, "Win!")
iwin()
Else
GUICtrlSetState(\$lwinlose, 16)
GUICtrlSetData(\$lstatus, "You chose the wrong door!")
GUICtrlSetData(\$lwinlose, "Lose")
ilose()
EndIf

If \$door1prize = 1 Then
MsgBox(0, "Monty Hall", "Door 1 had the prize.", 0)
ElseIf \$door2prize = 1 Then
MsgBox(0, "Monty Hall", "Door 2 had the prize.", 0)
ElseIf \$door3prize = 1 Then
MsgBox(0, "Monty Hall", "Door 3 had the prize.", 0)
EndIf

Sleep(500)
GUIDelete(\$mhall)
EndFunc   ;==>mhgui

;BUTTON FUNCTIONS
Func forcequit()
Exit
EndFunc   ;==>forcequit
Func pickdoor1()
\$door1selected = 1
EndFunc   ;==>pickdoor1
Func pickdoor2()
\$door2selected = 1
EndFunc   ;==>pickdoor2
Func pickdoor3()
\$door3selected = 1
EndFunc   ;==>pickdoor3
Func switchyes()
\$chosenswitch = 1
EndFunc   ;==>switchyes
Func switchno()
\$chosenswitch = 1
EndFunc   ;==>switchno

If \$door1selected = 1 Then
GUICtrlSetData(\$ldoor1, "?")
\$door1selected = 0
\$door3selected = 1
GUICtrlSetData(\$ldoor3, "Picked")
\$door1selected = 0
\$door2selected = 1
GUICtrlSetData(\$ldoor2, "Picked")
EndIf

ElseIf \$door2selected = 1 Then
GUICtrlSetData(\$ldoor2, "?")
\$door2selected = 0
\$door3selected = 1
GUICtrlSetData(\$ldoor3, "Picked")
\$door2selected = 0
\$door1selected = 1
GUICtrlSetData(\$ldoor1, "Picked")
EndIf
ElseIf \$door3selected = 1 Then
GUICtrlSetData(\$ldoor3, "?")
\$door3selected = 0
\$door2selected = 1
GUICtrlSetData(\$ldoor2, "Picked")
\$door3selected = 0
\$door1selected = 1
GUICtrlSetData(\$ldoor1, "Picked")
EndIf
EndIf
Func ilose()
\$losses = \$losses + 1
EndFunc   ;==>ilose
Func iwin()
\$wins = \$wins + 1
EndFunc   ;==>iwin

Global \$playagain = 1
While \$playagain = 1
;open gui
mhgui(\$wins, \$losses)

;play again?
\$restartnow = MsgBox(4, "Monty Hall", "Wins:" & \$wins & " Loss:" & \$losses & @CRLF & "Would you like to restart?")
If \$restartnow = 6 Then
\$playagain = 1
Else
\$playagain = 0
Exit
EndIf
WEnd```

##### Share on other sites

Good little exercise in coding that.

Monkey's are, like, natures humans.

##### Share on other sites

If you are going to use Global variables, you should declare them in the global scope, and put them all together (at the top after includes usually)

It does not in your script, but it can affect a script if a global is declared in the wrong place.

Monkey's are, like, natures humans.

##### Share on other sites

Vos Savant presentation is debatable --and has been fought-- as it's based on an untold parameter: how does the host choose the door when you pick the car first?

From that point of view (much more explicit of the actual game) the first greyed table in this wiki page is incomplete. It shoud read:

Door1   Door2   Door3  Staying   Switching

Car     Goat    Open     Car        Goat

Car     Open    Goat     Car        Goat

Goat    Car     Open     Goat       Car

Goat    Open    Car      Goat       Car

Now you can see why tenants of the 1/2 probability also have an argument, and a very strong one! Because the table with only 3 lines doesn't reflect all the possible outcomes when you have to make the choice of switching or not. At that time there is one door open and which one (assuming you first picked the car) is the key to the true probability in this real world. Merging the first two lines of the above table is cheating with you by hiding the host own choice.

I remember well the hearted debates around this when it poped up under Martin Gardner pen.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

sorry, but i just couldn't resist:

http://xkcd.com/1282/

##### Share on other sites

Glad to see I got some people talking. I just wanted to learn GUI a bit more so I made this. It has no purpose other than learning.

Yes, my code is not great as I stated in my notes. I even have variables that I never use. I should have made a single variable to hold the prize door-  \$doorprize

Still, I HAD to try this myself-- just in case the world was lying about statistics all along.

##### Share on other sites

If i take door 3 and the prize is behind, the door 1 will be remove all the time.

##### Share on other sites

??? Why?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Bad coding practices will create bad habits. Fix it sooner rather than later..

Snips & Scripts

My Snips: graphCPUTemp ~ getENVvars
My Scripts: message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...