Monty Hall problem: Difference between revisions

(/* =={{header|APL}}== /*)
Line 191:
Changing: 66.38%
New random choice: 50.17%
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
 
<lang APL>
MontyHall noof;r;Calc
⍝ Optimized for readability
Calc←{ ⍝ Locally defined direct function (lambda)
doors←1 2 3 ⍝ The doors
hit←?3 ⍝ Here is the car
first←?3 ⍝ Candidates first choice
rest←doors≠hit ⍝ What remains
toBeOpened←(rest/doors)~hit,first ⍝ One of them needs to be opened
opened←toBeOpened[?⍴toBeOpened] ⍝ Really opened by the showmaster
r←3⍴⍬ ⍝ Initialise the result
r[1]←hit=first ⍝ Stay with first choice
r[2]←hit=(doors~opened)[?2] ⍝ Throw dice again
r[3]←hit=doors~first,opened ⍝ Change to the remaining one
r ⍝ return result
}
r←+⌿↑Calc¨⍳noof ⍝ Performe the simulation
r←⍕¨⌊0.5+100×r÷noof ⍝ Calculate percentages
⎕←r[1],'% for staying'
⎕←r[2],'% for new choice'
⎕←r[3],'% for changing'
</lang>
Sample Output:
<pre>
MontyHall 10000
33 % for staying
50 % for new choice
67 % for changing
</pre>
 
Anonymous user