:Class Duck3 : MiPage :Include #.HTMLInput :Field Public Seq←'' ⍝ Name of edit field :Field Public Rule←'' ⍝ Name of edit field :Field Public Step←0 ⍝ Name of text variable steps through the lesson 1-3 :Field Public Pick1←'' ⍝ Your choice 1-3 :Field Public Odds1←'' ⍝ Your guess at odds for first pick :Field Public Win←0 ⍝ where the money is :Field Public Not←'' ⍝ where the money is not for sure :Field Public Pos←'' ⍝ possible to pick. is not current pick or the Not :Field Public Pick2←'' ⍝ door picked 2nd time :Field Public Odds2←'' ⍝ Your guess at odds for 2nd pick :Field Public Door1←'' ⍝ image :Field Public Door2←'' ⍝ image :Field Public Door3←'' ⍝ image :Field Public StOrSw←'' ⍝ 'Stick' or 'Switch' :Field Public NDoors←'3' ⍝ # doors for 10000 simulation :Field Public Show←'1' ⍝ # doors opened after choice for 10000 simulation :Field Public StickSwitch←'' ⍝ # wins for stick and switch for 10000 simulation :Field Public CBcnt←⍬ ⍝ Name of variable field :Field Public Action←'NewDucks' ⍝ All action buttons have this name :Field Public init←0 ⍝ flag to indicate if game has been initialized GraphHtml←{'<img height=25% width="75%" src="/',path,⍵,'">'} ∇ Render req;html;path;odds :Access Public :If (Step>0)^(Pick1≡,'') ⋄ init=0 ⋄ :EndIf ⍝ due to leftover from recent prev run?? :If init=0 ⋄ init←1 Step←0 ⋄ Win←⍕?3 ⋄ Action←'' ⋄ StickSwitch←'' Pick1 Odds1 Pick2 Odds2 Door1 Door2 Door3 StOrSw←⊂'' :EndIf DoAction ⍝ If a button was pressed, deal with it :If init=0 ⋄ init←1 Step←0 ⋄ Win←⍕?3 ⋄ Action←'' ⋄ StickSwitch←'' Pick1 Odds1 Pick2 Odds2 Door1 Door2 Door3 StOrSw←⊂'' :EndIf html←'h2'Enclose'3 Ducks: Test Your DeDUCKtive Logic Skills Game' ⍝ display headline html,←'<b>The goal of this game is to guess the DUCK that has a million dollars behind it.' html,←' The other two have nothing.' odds←'0' '1/3' '1/2' '2/3' Step←5⌊Step+1 :If (Step=2)^(Pick1≡'') ⋄ Step←1 :ElseIf (Step=3)^(Odds1≡'') ⋄ Step←2 :ElseIf (Step=4)^(Pick2≡'') ⋄ Step←3 :ElseIf (Step=5)^(Odds2≡'') ⋄ Step←4 :EndIf :Select Step :Case 1 path←'jbgames/picsDucks/' ⍝ used by GraphHtml defined above folder←req.Server.Root,'jbgames\picsDucks\' ⍝ This is where we keep the duck pics AllAnimals←¯4↓¨#.Files.Dir folder,'*.jpg' ⍝ Use list of files as list of valid animals D←AllAnimals[3?⍴AllAnimals] Door1 Door2 Door3←GraphHtml¨ D,¨(⊂'.jpg') ⍝ pick 3 random duck pics html,←BR,'Pick a DUCK now. DUCK=',('Pick1'DropDown(⍕¨⍳3)(,Pick1)) :Case 2 Not←⍕(?2)⊃2⍴(⍳3)~⍎¨Win Pick1 ⍝ Not=hint door that≠million. if Win=Pick1 then 2 possible html,←BR,'You picked DUCK ',Pick1,' What do you think are the odds you are correct? ' html,←('Odds1'DropDown(odds)(,Odds1)) :Case 3 html,←BR,'Your guess of ',(⍕Odds1),' is ',((1+Odds1≡'1/3')⊃'Wrong' 'Correct') html,←'. DUCK ',Pick1,' has a 1/3 chance of being the right DUCK of the 3 DUCKS.' html,←BR,'Now I am going to give you a hint. I looked at the other 2 DUCKS and it isn''t DUCK ',Not,'.' Pos←(⍳3)~⍎¨Pick1 Not ⍝ possible one. Not the pick or the one show in the hint(Not). html,←BR,'Now do you want to Stick with DUCK ',Pick1,' or Switch to DUCK ',(⍕Pos),' ' html,←('Pick2'DropDown(Pick1(⍕Pos))(,Pick2)) :Case 4 html,←BR,'You chose to ',(StOrSw←(1+=/⍎¨Pick1 Pick2)⊃'Switch' 'Stick'),' with DUCK ',Pick2 html,←'. What do you think the odds are of you being correct now?',('Odds2'DropDown(odds)(,Odds2)) :Case 5 html,←BR,'You thought the odds to ',StOrSw,' were ',Odds2,'.',' That is ' html,←(1+((=/⍎¨Pick1 Pick2)∧(Odds2≡'1/3'))∨((≠/⍎¨Pick1 Pick2)∧(Odds2≡'2/3')))⊃'WRONG.' 'CORRECT.' html,←BR,'You chose to ',StOrSw,' which is ',((1+StOrSw≡'Switch')⊃'Foolish.' 'Smart.') html,←' Switching is the best strategy 2/3 of the time.' html,←BR,'In summary, you were very ' tbl←2 2⍴'FOOLISH and LOST.' 'FOOLISH BUT GOT LUCKY and WON.' 'SMART BUT LOST.' 'SMART and WON.' html,←(⊂(1+StOrSw≡'Switch')(1+=/⍎¨Win Pick2))⊃tbl html,←BR,'You chose DUCK ',Pick2,' and DUCK ',Win,' had the million dollars.' html,←BR,BR,'Let me explain how this works. The odds of your first choice being right is 1/3 and ' html,←'thus 2/3 of the time one of the other two DUCKS will be correct. Now I tell you which of ' html,←'the other 2 DUCKS to pick. Therefore if you switch you will be correct 2/3 of time.' html,←BR,BR,'This problem(Google "The Monte Hall Problem" ) arose from a TV program called ' html,←'''Let''s Make A Deal'' which began in 1963 where contestants picked one of three doors, ' html,←'and then the host looked behind the other two doors and opened one that did not have the prize. ' html,←'PhD statisticians argued over the odds for years so don''t feel too bad if you missed it, ' html,←'but please do try to learn deDUCKtive statistics. Statistics is very important in virtually all ' html,←'decisions we make, whether it be jumping across a stream, running a red light, ' html,←'choosing a date, going surfing, dropping out of school, buying a stock, ' html,←'trying a parachute jump, eating junk food, smoking, drinking, taking medication, ' html,←'choosing a career or saving for retirement. It''s all deDUCKtive statistics. And we are very easily fooled. ' html,←'Most people frequently make bad deDUCKtive statistical decisions. It is not intuitive. It must be learned.' html,←BR,BR,'If your still not a believer click Run10000 below repeatedly and compare stick to switch results. ' html,←BR,'And if your really want to improve your deDUCKtive statistical skills you can even check different #''s of DUCKS and #''s of DUCKS I disclose.' html,←BR,'Total # DUCKS=',('NDoors'DropDown(⍕¨⍳10)(,NDoors)),' DUCKS I Disclose=',('Show'DropDown(⍕¨¯1+⍳11)(,Show)) html,←('Action'Submit'Run10000'),' Your wins in 10,000 trials if stick and if switch=',StickSwitch :EndSelect html,←BR,('Action'Submit'Continue'),' Or ',('Action'Submit'New Game With Different Ducks') html,←BR,BR,Table(1 3⍴Door1 Door2 Door3)'align="center" border=20' html←req('post'Form)html ⍝ Put a 'submit' form around it req.Return html ∇ ∇ DoAction;inc2chk;n;incnum;intchk;percent :If Action≡'New Game With Different Ducks' ⋄ init←0 ⍝ Step←0 ⋄ Win←⍕?3 ⋄ Action←'' ⋄ StickSwitch←'' :ElseIf Action≡'Run10000' StickSwitch←⍕+/(⍎Show)monty¨10000⍴⍎NDoors ⍝ 10000 NDoor simulations showing doors=Show, ~2/3 win if switch Action←'' :ElseIf Win=0 ⋄ Win←⍕?3 :EndIf Step←5⌊Step ⍝ don't let Step>5 ∇ monty←{ ⍝ Monty Hall problem - http://www.nytimes.com/2008/04/08/science/08monty.html ⍝ ⍵ is the number of doors ⍝ ⍺ is the number of doors that are shown to the contestant (default 1) ⍝ result is [1] original selection won, [2] switched entry won ⍺←1 ⍝ default to showing 1 door selector←{⍵[(⍺⌊⍴⍵)?⍴⍵]} ⍝ randomly select ⍺ items from ⍵ doors←⍵ ⍝ number of doors is the argument alldoors←⍳doors ⍝ build a list of the doors winner←?doors ⍝ randomly select the winning door selected←?doors ⍝ randomly select the player's choice shown←(0⌈⍺⌊doors-2)selector alldoors~selected,winner ⍝ pick the door to show new←1 selector alldoors~shown,selected ⍝ pick the new selection winner=selected,new } ⍝It turns out that using N doors, and opening P doors, the probability that you win by switching is: ⍝ (N-1)÷N×N-P+1 ⍝Examples: ⍝ monty 3 ⍝ single case standard Monty Hall problem, switching won in this case ⍝0 1 ⍝ +/monty¨ 1000⍴3 ⍝ 1000 3-door simulations, ~2/3 chance of winning when you switch ⍝320 680 ⍝ +/monty¨1000⍴2 ⍝ 1000 2-door simulations, about a 50/50 chance of winning ⍝514 486 ⍝ +/monty¨1000⍴1 ⍝ 1000 1-door simulations, you always win J degenerative case ⍝1000 ⍝ +/monty¨1000⍴4 ⍝ 1000 4-door simulations, .375 probability that switching wins ⍝217 394 ⍝ +/2 monty¨1000⍴4 ⍝ 1000 4-door simulations, showing 2 doors, .75 probability ⍝274 726 ⍝ +/monty¨1000⍴10 ⍝ 1000 10-door simulations ⍝101 117 ⍝ +/5 monty¨1000⍴10 ⍝ 1000 10-door simulations, showing 5 doors - .225 probability ⍝98 218 :EndClass