#!/usr/bin/perl use strict; use warnings 'all'; use Getopt::Long; GetOptions 'doors=i' => \(my $doors = 3), 'reveals=i' => \(my $reveals = 1), 'runs=i' => \(my $runs = 10_000), or exit 1; # Sanity checks. $doors = 3 if $doors < 3; $reveals = 1 if $reveals < 1; $runs = 1 if $runs < 1; $reveals = $doors - 2 if $reveals + 2 > $doors; use constant GOAT => 0x00; use constant PRICE => 0x01; use constant PICK => 0x02; use constant REVEALS => 0x04; my ($non_swapper, $swapper) = (0) x 2; foreach (1 .. $runs) { # Put goats behind the doors. my @doors = (GOAT) x $doors; # Upgrade one goat to the price. $doors [my $price = int rand @doors] |= PRICE; # Our contestants pick a door. $doors [my $f_pick = int rand @doors] |= PICK; # Doors Monty can reveal. my @monties = grep {!$doors [$_]} 0 .. $doors - 1; # Monty reveals doors. my $r = $reveals; while (@monties && $r --) { $doors [splice @monties, rand @monties, 1] |= REVEALS; } # Swap. my @closed = grep {!($doors [$_] & REVEALS) && !($doors [$_] & PICK)} 0 .. $doors - 1; my $s_pick = $closed [rand @closed]; $non_swapper ++ if $price == $f_pick; $swapper ++ if $price == $s_pick; } printf "$runs runs with $doors doors. Monty reveals $reveals closed door%s.\n", $reveals == 1 ? "" : "s"; printf "The non swapping contestant won %.2f%% of the prices.\n", 100 * $non_swapper / $runs; printf "The swapping contestant won %.2f%% of the prices.\n", 100 * $swapper / $runs;