|
#!/usr/bin/perl
sub choice1 {
@s=(0,0,0);
$car=int(rand(3));
$s[$car]+=1;
$pick =int(rand(3));
$s[$pick]+=2;
if($car!=$pick) {
$host=($s[0]==0)? 0: ($s[1]==0)? 1 : 2;
}
else {
$host=($s[0])? 1 : ($s[1])? 2 : 0;
$host+=int(rand(2));
$host=$host%3;
}
$s[$host]=4;
return @s;
}
print int(rand(3));
@t=(0,0,0);
@m=(0,0,0);
$w3=0;
for($i=0;$i<10000;$i++) {
@t=choice1();
if($t[0]==3 or $t[1]==3 or $t[2]==3) { $w3++;}
print "$t[0] $t[1] $t[2] -> 3:$w3\n";
}
printf "Monty Hall Problem\n";
printf "1:car, 2:your choice, 3:your choose car, 4:host's choice\n";
printf "If you don't change your choice, You can get car by %.1f%.\n",$w3/100;
printf "If you change your choice, You can get car by %.1f%.\n",100-$w3/100;
|
|