I did find your logic a bit hard to follow, so I coded
an alternative. It does 10,000 runs with 2 contestants,
one of which will always swap, and one will never. Number
of doors, the number of revealed doors and the number
of runs can be varied with command line options.
#!/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 do
+or%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;
Abigail
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|