Talk:Monty Hall problem: Difference between revisions

still commenting perl removing, and proposing commented code (is this a provocation? it isn't)
(logical about perl removing)
(still commenting perl removing, and proposing commented code (is this a provocation? it isn't))
Line 45:
::This is actually the problem in many tasks. The tasks need to be made simple in order to keep the implementations short and clear. But then many contributors, instead of implementing solution to the problem, only implement solution to that specific case, or even jump directly to the answer without doing any computing.
::--[[User:PauliKL|PauliKL]] 14:56, 7 December 2008 (UTC)
:::It was not the case of Perl code, that indeed simulated like the others code, but with logical optimization. Would the following (commented) code meets the needed criteria?
 
<perl>#! /usr/bin/perl
use strict;
my $trials = 10000;
 
my $stay = 0;
my $switch = 0;
 
my $show;
 
for(my $i=0; $i < $trials; $i++)
{
my $prize = int(rand 3);
# let monty randomly choose a door where he puts the prize
my $chosen = int(rand 3);
# let us randomly choose a door...
if ( $prize == $chosen )
{
# a "further" optimization would strip the next
# line since it basically does nothing
while( ($show = int(rand 3) ) == $chosen ) { }
# monty opens a door which is not the one with the
# prize, that he knows it is the one the player chosen
$stay++;
# no matter what $show is... player wins only if he
# stays... and looses otherwise; this is because we
# wrote this code inside a $prize == $chosen if
# we could add something that logical optimization
# would strip
} else {
# monty has only one choice for $show,
# now, we don't need to waste cpu time: we (as monty!)
# already know
# that being the prize behind $prize and the user
# choice is $chosen, $show won't be one nor the other;
# so if the user stay, he looses, if switch, surely
# he wins.
$switch++;
}
}
 
print "Stay win ratio " . (100.0 * $stay/10000.0) . "\n";
print "Switch win ratio " . (100.0 * $switch/10000.0) . "\n";
exit 0;</perl>
 
:::--[[User:ShinTakezou|ShinTakezou]] 23:01, 7 December 2008 (UTC)