#!/usr/bin/perl # #Monty Hall's Hall Of Doors v2.0 19991026 #by Leonard Richardson leonardr@ucla.edu # #"And you may ask yourself, where is that large automobile?" # - Talking Heads, 'Once in a Lifetime' # use CGI qw/:standard/; $max_games = 10000; $disable_logging_at = 100; #preliminaries prints out the usual suspects and gets the input. preliminaries(); #Initialize data structures. foreach $a ("switch","stick") { $wins{$a} = 0; $losses{$a} = 0; } #Play a bunch of games. $switch_counter = 1; for ($counter = 1; $counter <= $times; $counter++) { play_game(); } #Let's tabulate those scores! print_statistics(); ########################################### sub play_game() #Plays one round of the Monty Hall game of fun. { $mega_door = int(rand(3)) + 1; @door_stats[$position]++; if ($same_door_always) { $first_choice = 1; } else { $first_choice = int(rand(3))+1; } #Figure out which door Monty opens. The door that neither the #player nor Monty picked is our choice if we want to switch. $opened = monty_decision($first_choice, $mega_door); $switch_choice = other_door($first_choice,$opened); #Now we need to figure out if we're going to switch or not. # #We can switch randomly rather than with a counter, to exchange #experimental error (eg., 40% switching on 30 games) for random #error. if ($random) { if (int(rand(100))+1 <= $percent_switch) { $action = "switch"; } else { $action = "stick"; } } else { #Or, we can switch with a counter, to exchange random error for #experimental error. This is the default. if ($switch_counter <= $percent_switch) { $action = "switch"; } else { $action = "stick"; } $switch_counter++; if ($switch_counter > 100) { $switch_counter = 1; } } if ($action eq "switch") { $new_choice = $switch_choice; } else { $new_choice = $first_choice; } #Now we see whether or not we won, and increment the appropriate variables. if ($mega_door == $new_choice) { $wins{$action}++; } else { $losses{$action}++; } if ($log) { print "
Log for round $counter:
\n";
print "I chose door $first_choice.
\n";
print "Monty opened door $opened.
\n";
print "I decided ";
if ($action eq "stick") {print "not ";}
print "to switch";
if ($action eq "switch") {print " to door $new_choice"};
print ".
\n";
if ($mega_door == $new_choice)
{
print "Woohoo!";
} else {
print "Damn!";
}
print " The mega prize was behind door $mega_door";
if ($action eq "switch" && $mega_door ne $new_choice) { print " after all";}
print "!
\n
$counter rounds played, disabling log.
\n"; $log = 0; } } } sub print_statistics #See also print_lies and print_damn_lies { %past = ("switch" => "switched", "stick" => "stuck",); #First, using only the starting conditions, we will predict the total #percentage of rounds that should be won. %theoretical_win_chance = ("switch" => 2/3, "stick" => 1/3,); $theoretical_win_chance = ($theoretical_win_chance{switch} * $percent_switch) + ($theoretical_win_chance{stick} * (100-$percent_switch)); #Now we print them out. print "".ucfirst($a)."ing: ";
print "You $past{$a} $total times (" . $total/$times * 100 ."% of the time)
".
"Wins on $a: $wins{$a}
".
"Losses on $a: $losses{$a}
".
"You won $percentage{$a}% of the time you $past{$a}.
";
}
print "
The Bottom Line:
";
print $total_percentage{stick}+$total_percentage{switch} .
"% of all rounds were won.
";
print "Theory predicts $theoretical_win_chance%.\n
Can't play $times games, playing $max_games games instead.
"; $times = $max_games; } }