Monty Hall problem: Difference between revisions
Content added Content deleted
m (→{{header|Java}}: Output by request) |
Underscore (talk | contribs) (Added Perl) |
||
Line 238: | Line 238: | ||
<pre>Switching wins 21924 times. |
<pre>Switching wins 21924 times. |
||
Staying wins 10844 times.</pre> |
Staying wins 10844 times.</pre> |
||
=={{header|Perl}}== |
|||
<perl>my $trials = 10_000; |
|||
sub play |
|||
# Takes a boolean saying whether to swtich doors; returns |
|||
# a boolean saying whether the result was a car. |
|||
{my $door1 = !int(rand 3); |
|||
$_[0] ? !$door1 : $door1;} |
|||
sub msg |
|||
{print "The $_[0] strategy succeeds ", |
|||
100 * $_[1]/$trials, "% of the time.\n";} |
|||
sub count {scalar @_} |
|||
msg 'switch', count grep({$_} map {play 1} (1 .. $trials)); |
|||
msg 'stay', count grep({$_} map {play 0} (1 .. $trials));</perl> |
|||
Sample output: |
|||
<pre>The switch strategy succeeds 66.44% of the time. |
|||
The stay strategy succeeds 32.13% of the time.</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |