Browse Source

initial commit

(one of ours, one from the internet)
main
user 10 years ago
commit
c825f584d2
2 changed files with 127 additions and 0 deletions
  1. +75
    -0
      from_the_internet.pl
  2. +52
    -0
      monty.pl

+ 75
- 0
from_the_internet.pl View File

@ -0,0 +1,75 @@
use strict;
my $iterations = 3000; # How many games to play
my $verbosity = 0;
while (@ARGV) {
my $param = shift @ARGV;
$verbosity = 1 if $param eq '-v';
$iterations = int (shift @ARGV) if $param eq '-i';
}
sub verbose {
print $_[0]."\\n" if $verbosity;
}
my $stickers;
my $switchers;
print "Playing $iterations games...\\n\\n";
for(1..$iterations) {
my @items = qw(goat goat prize); # two goats, one prize
my @door;
while (@items) { # this puts the @items into the @door array in random order
push (@door, splice (@items, int rand @items, 1));
}
verbose ("Door 0: $door[0]; Door 1: $door[1]; Door 2: $door[2]");
my $contestant = int rand 3;
verbose ("Contestant chooses door $contestant.");
my $monty;
# If the contestant picked the prize, Monty picks another door by random.
if ($door[$contestant] eq 'prize') {
$monty = ($contestant + (int rand 2) + 1) % 3;
}
# Otherwise, he picks the other goat.
else {
$monty = $door [ ($contestant+1) % 3 ] eq 'goat' ? ($contestant+1) % 3 : ($contestant+2) % 3;
}
verbose ("Monty opens door $monty.");
# Now only two doors are open. If the sticker wins, the switcher loses and vice versa.
if ($door[$contestant] eq 'prize') {
verbose ("Sticker wins. Switcher loses.");
$stickers++;
} else {
verbose ("Sticker loses. Switcher wins.");
$switchers++;
}
}
print "Grand totals:
Sticker has won $stickers times
Switcher has won $switchers times
";

+ 52
- 0
monty.pl View File

@ -0,0 +1,52 @@
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
# basic setup:
# you get 3 doors
# - initially, you pick a door at random
# - second, the host eliminates one of the two remaining doors that you haven't picked
# - now, you have the option to switch to the remaining door from the one you've already picked
my ($switched, $unswitched) = (0, 0);
my $doors = 3;
for (my $i = 0; $i < 100000000; $i++) {
my $goat = int(rand($doors));
my $initial_selection = int(rand($doors));
my $host_eliminates = int(rand($doors));
until (($host_eliminates != $initial_selection) && ($host_eliminates != $goat)) {
$host_eliminates = int(rand($doors));
}
my $final_selection = int(rand($doors));
until ($final_selection != $host_eliminates) {
$final_selection = int(rand($doors));
}
my $result = ($final_selection == $goat);
if ($final_selection == $initial_selection) {
$unswitched++;
} else {
$switched++;
}
# say "you pick: $initial_selection"; say "host eliminates: $host_eliminates";
# say "goat is: $goat";
# say "final selection is: $final_selection";
# say "correct? " . ($result ? 'yes' : 'no');
if ($i % 10000 == 0) {
say "$i :: " . ($switched - $unswitched);
}
}
say "switched from initial decision: $switched";
say "unswitched from initial decision: $unswitched";

Loading…
Cancel
Save