Monty Hall problem: Difference between revisions

→‎version 2: elided the code to support more than 3 doors and their outputs.
(→‎version 2: elided the code to support more than 3 doors and their outputs.)
Line 3,453:
 
===version 2===
This<lang rexx>/*REXX versionprogram allowssimulates theany number of doorstrials toof bethe specifiedclassic &nbsp;TV (asshow wellMonty asHall the number of trials)problem.*/
parse arg # d seed . /*obtain the optional args from the CL.*/
 
NOTE: This implementation is incorrect for four doors or more. It assumes that exactly one of the two strategies always wins, but if there are more than three doors it is poosible that both strategies lose. In fact, the more doors, the more likely no one will win, contrary to the produced result of over 80 % chance of winning when there are six doors and only one car.
 
<lang rexx>/*REXX pgm simulates # of trials of the classic Monty Hall problem (with any # of doors)*/
parse arg # d seed . /*obtain the optional args from the CL.*/
if #=='' | #=="," then #= 1000000 /*Not specified? Then 1 million trials*/
if d=='' | d=="," then d= 3 /* " " " use three doors.*/
if datatype(seed, 'W') then call random ,, seed /*Specified? Use as a seed for RANDOM.*/
wins.= 0 /*wins.0 ≡ stay, wins.1 ≡ switching.*/
do #; door. = 0 /*initialize all doors to a value of 0.*/
car= random(1, d3); door.car= 1 /*the TV show hides a car randomly. */
?= random(1, d3); _= door.? /*the contestant picks a (random) door.*/
wins._ = wins._ + 1 /*bump count of 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 '3 doors.'" /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
switching wins 66.87% of the time.
staying wins 33.23% of the time.
 
performed 1000000 times with 3 doors.
</pre>
{{out|output|text=&nbsp; when using the default number of trials &nbsp; (one million) &nbsp; and with four doors: &nbsp; <tt> , &nbsp; 4 </tt>}}
<pre>
switching wins 75.0% of the time.
staying wins 25.0% of the time.
 
performed 1000000 times with 4 doors.
</pre>
{{out|output|text=&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.
staying wins 20.0% of the time.
 
performed 1000000 times with 5 doors.
</pre>
{{out|output|text=&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.
staying wins 16.4% of the time.
 
performed 1000000 times with 6 doors.
</pre>