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


In reply to Re: The Monty Hall Trap Simulator by Abigail-II
in thread The Monty Hall Trap Simulator by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.