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=== |
||
This REXX version allows the number of doors to be specified (as well as the number of trials). |
|||
⚫ | |||
parse arg # .; if #=='' then #=1000000 /*Not specified? Then use 1 million.*/ |
|||
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.*/ |
|||
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. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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 ' |
say 'performed ' # " times with " d ' doors.' /*stick a fork in it, we're all done. */</lang> |
||
'''output''' when using |
'''output''' when using default inputs: |
||
<pre> |
<pre> |
||
switching wins 66. |
switching wins 66.8% of the time. |
||
staying wins 33. |
staying wins 33.2% of the time. |
||
performed 1000000 times. |
performed 1000000 times with 3 doors. |
||
</pre> |
</pre> |
||
⚫ | |||
===version 3=== |
|||
This REXX version is identical to the REXX version 2, but allows the number of doors to be specified. |
|||
⚫ | |||
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.*/ |
|||
⚫ | |||
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. */ |
|||
⚫ | |||
⚫ | |||
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> |
|||
⚫ | |||
<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''' when using the default number of trials (one million) and with five doors: |
'''output''' when using the default number of trials (one million) and with five doors: <tt> , 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''' when using the default number of trials (one million) and with six doors: |
'''output''' when using the default number of trials (one million) and with six doors: <tt> , 6 </tt> |
||
<pre> |
<pre> |
||
switching wins 83.6% of the time. |
switching wins 83.6% of the time. |