Monty Hall problem: Difference between revisions

m
→‎version 2: added/changed whitespace and comments to the program and the output.
m (→‎version 2: added/changed whitespace and comments to the program and the output.)
Line 3,070:
 
===version 2===
<lang rexx>/*REXX program simulates a #number of trials of the classic Monty Hall problem.*/
parse arg t .; if t=='' then t=1000000 /*Not specified? Then use default1 million.*/
wins.=0 /*wins.0=stay;, wins.1=switching.*/
/*door values: 0=goat 0≡goat 1≡car 1=car */
do t; door.=0 /*performinitialize thisall loopdoors to a Tvalue of times. 0*/
door.car=0 random(1,3); door.car=1 /*setthe allTV doorsshow tohides a zerocar randomly. */
car?=random(1,3) ; _=door.car=1 ? /*TVthe showcontestant hidespicks a carrandom randomlydoor. */
?wins._ =random( wins._+1,3) ; _=door.? /*contestantbump picksthe atype randomof doorwin strategy. */
wins._=wins._+1end /*t*/ /*bump [↑] perform the typeloop T of win strategytimes. */
end /*DO t*/
 
say 'switching wins ' format(wins.0/t*100,,1)"% of the time."
say ' staying wins ' format(wins.1/t*100,,1)"% of the time." ; say
say 'performed ' t " times." /*stick a fork in it, we're all done.*/</lang>
'''output''' when using the default number of trials &nbsp; (one million):
<pre>
Line 3,089 ⟶ 3,088:
staying wins 33.3% of the time.
 
performed 1000000 times.
</pre>