ddxfish Posted August 17, 2014 Share Posted August 17, 2014 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 expandcollapse popupGlobal $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 $door1dead = 0 Global $door2open = 0 Global $door2prize = 0 Global $door2selected = 0 Global $door2dead = 0 Global $door3open = 0 Global $door3prize = 0 Global $door3selected = 0 Global $door3dead = 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) $door1dead = 1 GUICtrlSetData($lstatus, "I can tell you it is not door 1") ElseIf $door2selected = 0 And $door2prize = 0 Then GUICtrlSetState($ldoor2, 32) GUICtrlSetState($Label5, 32) $door2dead = 1 GUICtrlSetData($lstatus, "I can tell you it is not door 2") ElseIf $door3selected = 0 And $door3prize = 0 Then GUICtrlSetState($ldoor3, 32) GUICtrlSetState($Label6, 32) $door3dead = 1 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 Global $switchanswer = -1 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 $switchanswer = 1 switchanswer() EndFunc ;==>switchyes Func switchno() $chosenswitch = 1 $switchanswer = 0 EndFunc ;==>switchno Func switchanswer() If $door1selected = 1 Then GUICtrlSetData($ldoor1, "?") If $door2dead = 1 Then $door1selected = 0 $door3selected = 1 GUICtrlSetData($ldoor3, "Picked") ElseIf $door3dead = 1 Then $door1selected = 0 $door2selected = 1 GUICtrlSetData($ldoor2, "Picked") EndIf ElseIf $door2selected = 1 Then GUICtrlSetData($ldoor2, "?") If $door1dead = 1 Then $door2selected = 0 $door3selected = 1 GUICtrlSetData($ldoor3, "Picked") ElseIf $door3dead = 1 Then $door2selected = 0 $door1selected = 1 GUICtrlSetData($ldoor1, "Picked") EndIf ElseIf $door3selected = 1 Then GUICtrlSetData($ldoor3, "?") If $door1dead = 1 Then $door3selected = 0 $door2selected = 1 GUICtrlSetData($ldoor2, "Picked") ElseIf $door2dead = 1 Then $door3selected = 0 $door1selected = 1 GUICtrlSetData($ldoor1, "Picked") EndIf EndIf EndFunc ;==>switchanswer 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 Link to comment Share on other sites More sharing options...
JohnOne Posted August 17, 2014 Share Posted August 17, 2014 Good little exercise in coding that. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted August 17, 2014 Share Posted August 17, 2014 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. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted August 18, 2014 Share Posted August 18, 2014 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.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
orbs Posted August 18, 2014 Share Posted August 18, 2014 sorry, but i just couldn't resist: http://xkcd.com/1282/ Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
ddxfish Posted August 18, 2014 Author Share Posted August 18, 2014 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. Link to comment Share on other sites More sharing options...
kaz Posted August 18, 2014 Share Posted August 18, 2014 If i take door 3 and the prize is behind, the door 1 will be remove all the time. Link to comment Share on other sites More sharing options...
jchd Posted August 18, 2014 Share Posted August 18, 2014 ??? Why? This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
MikahS Posted August 18, 2014 Share Posted August 18, 2014 Bad coding practices will create bad habits. Fix it sooner rather than later.. Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now