Monty Hall problem: Difference between revisions

→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, removed the original version 2.
(→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, removed the original version 2.)
Line 3,128:
 
===version 2===
<langThis rexx>/*REXX programversion simulatesallows athe number of trialsdoors ofto thebe classicspecified Monty&nbsp; Hall(as problemwell as the number of trials).*/
<lang rexx>/*REXX program simulates a number of trials of the classic Monty Hall problem. */
parse arg # .; if #=='' then #=1000000 /*Not specified? Then use 1 million.*/
winsparse arg # d .=0 /*wins.0obtain the stay,optional args from wins.1 ≡the switchingCL.*/
if #=='' | #=="," then #=1000000 /*door values: 0≡goatNot specified? Then use 1 1≡carmillion. */
if d=='' do| d=="," #; then d= 3 door.=0 /*initialize all" doors to a value of 0" " " three doors.*/
wins.=0 car=random(1,3); door.car=1 /*thewins.0 TV showstay, hides a car randomlywins.1 switching.*/
?=random(1,3) do #; _= door.? =0 /*theinitialize contestantall picksdoors to a randomvalue doorof 0. */
wins._ = wins._ + 1 car=random(1, d); door.car=1 /*bump the typeTV ofshow winhides strategy.a car randomly. */
end /*#*/ ?=random(1, d); _=door.? /* [↑] perform the loopcontestant picks a #random door. times. */
wins._ = wins._ + 1 /*bump the type of win strategy. */
 
end /*#*/ /* [↑] perform the loop # times. */
/* [↑] door values: 0≡goat 1≡car */
say 'switching wins ' format(wins.0 / # * 100, , 1)"% of the time."
say ' staying wins ' format(wins.1 / # * 100, , 1)"% of the time." ; say
say 'performed ' # " times #with " d ' " timesdoors." ' /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million)inputs:
<pre>
switching wins 66.78% of the time.
staying wins 33.32% of the time.
 
performed 1000000 times with 3 doors.
</pre>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with four doors: &nbsp; <tt> , &nbsp; 4 </tt>
 
===version 3===
This REXX version is identical to the REXX version 2, but allows the number of doors to be specified.
<lang rexx>/*REXX program simulates a number of trials of the classic Monty Hall problem.*/
parse arg # d . /*obtain the optional args from the CL.*/
if #=='' | #=="," then #=1000000 /*Not specified? Then use 1 million. */
if d=='' | d=="," then d=3 /* " " " " three doors.*/
wins.=0 /*wins.0 ≡ stay, wins.1 ≡ switching.*/
/*door values: 0≡goat 1≡car */
do #; door.=0 /*initialize all doors to a value of 0.*/
car=random(1,d); door.car=1 /*the TV show hides a car randomly. */
?=random(1,d); _=door.? /*the contestant picks a random door. */
wins._ = wins._ + 1 /*bump the type of win strategy. */
end /*#*/ /* [↑] perform the loop # times. */
 
say 'switching wins ' format(wins.0 / # * 100, , 1)"% of the time."
say ' staying wins ' format(wins.1 / # * 100, , 1)"% of the time." ; say
say 'performed ' # " times with " d ' doors.' /*stick a fork in it, we're done*/</lang>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with four doors:
<pre>
switching wins 75.0% of the time.
Line 3,173 ⟶ 3,157:
performed 1000000 times with 4 doors.
</pre>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with five doors: &nbsp; <tt> , &nbsp; 5 </tt>
<pre>
switching wins 80.0% of the time.
Line 3,180 ⟶ 3,164:
performed 1000000 times with 5 doors.
</pre>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with six doors: &nbsp; <tt> , &nbsp; 6 </tt>
<pre>
switching wins 83.6% of the time.