Monty Hall problem: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, removed the original version 2.)
Line 3,128: Line 3,128:


===version 2===
===version 2===
<lang rexx>/*REXX program simulates a number of trials of the classic Monty Hall problem.*/
This REXX version allows the number of doors to be specified &nbsp; (as well 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.*/
wins.=0 /*wins.0 stay, wins.1 ≡ switching.*/
parse arg # d . /*obtain the optional args from the CL.*/
/*door values: 0≡goat 1≡car */
if #=='' | #=="," then #=1000000 /*Not specified? Then use 1 million. */
do #; door.=0 /*initialize all doors to a value of 0*/
if d=='' | d=="," then d= 3 /* " " " " three doors.*/
car=random(1,3); door.car=1 /*the TV show hides a car randomly. */
wins.=0 /*wins.0 stay, wins.1 switching.*/
?=random(1,3); _=door.? /*the contestant picks a random door. */
do #; door. =0 /*initialize all doors to a value of 0.*/
wins._ = wins._ + 1 /*bump the type of win strategy. */
car=random(1, d); door.car=1 /*the TV show hides a car randomly. */
end /*#*/ /* [↑] perform the loop # times. */
?=random(1, d); _=door.? /*the contestant picks a random door. */
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 'switching wins ' format(wins.0 / # * 100, , 1)"% of the time."
say ' staying wins ' format(wins.1 / # * 100, , 1)"% of the time." ; say
say ' staying wins ' format(wins.1 / # * 100, , 1)"% of the time." ; say
say 'performed ' # " times." /*stick a fork in it, we're all done.*/</lang>
say 'performed ' # " times with " d ' doors.' /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million):
'''output''' &nbsp; when using default inputs:
<pre>
<pre>
switching wins 66.7% of the time.
switching wins 66.8% of the time.
staying wins 33.3% of the time.
staying wins 33.2% of the time.


performed 1000000 times.
performed 1000000 times with 3 doors.
</pre>
</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>
<pre>
switching wins 75.0% of the time.
switching wins 75.0% of the time.
Line 3,173: Line 3,157:
performed 1000000 times with 4 doors.
performed 1000000 times with 4 doors.
</pre>
</pre>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with five doors:
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with five doors: &nbsp; <tt> , &nbsp; 5 </tt>
<pre>
<pre>
switching wins 80.0% of the time.
switching wins 80.0% of the time.
Line 3,180: Line 3,164:
performed 1000000 times with 5 doors.
performed 1000000 times with 5 doors.
</pre>
</pre>
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with six doors:
'''output''' &nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with six doors: &nbsp; <tt> , &nbsp; 6 </tt>
<pre>
<pre>
switching wins 83.6% of the time.
switching wins 83.6% of the time.