Monty Hall problem: Difference between revisions

→‎{{header|Perl}}: Removed - See talk page. (It calculates against known % rather than playing the game.
(Added the code for C)
(→‎{{header|Perl}}: Removed - See talk page. (It calculates against known % rather than playing the game.)
Line 483:
msg "stay" stay</ocaml>
 
=={{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}}==
Anonymous user