Cool program. It's much faster using a shuffle.

Your code:

% time perl prisoners-orig.pl == SIMULATION 9900 of 10000 == Success: 3085 Fail: 6915 Fail rate: 69.15% perl prisoners-orig.pl 13.09s user 0.01s system 99% cpu 13.101 total

Modified to use Array::Shuffle:

% time perl prisoners.pl == SIMULATION 9900 of 10000 == Success: 3175 Fail: 6825 Fail rate: 68.25% perl prisoners.pl 0.71s user 0.00s system 99% cpu 0.716 total

Diff:

--- prisoners-orig.pl 2025-04-30 17:24:41.190188166 -0400 +++ prisoners.pl 2025-04-30 17:24:01.156228637 -0400 @@ -7,7 +7,7 @@ use warnings; use diagnostics; -use Array::Contains; +use Array::Shuffle qw(shuffle_array); my $PRISONERCOUNT = 100; # How many prisoners my $OPENBOXES = $PRISONERCOUNT / 2; # Every prisoner can open half th +e boxes @@ -49,15 +49,8 @@ sub generateBoxes { print "Generating boxes...\n" if($DEBUG); - my @boxes; - - while(scalar @boxes < $PRISONERCOUNT) { - my $prisoner = int(rand($PRISONERCOUNT)); - if(!contains($prisoner, \@boxes)) { - push @boxes, $prisoner; - } - } - + my @boxes = (0..$PRISONERCOUNT-1); + shuffle_array(@boxes); return \@boxes; }
90% of every Perl application is already written.
dragonchild

In reply to Re^3: Proving Veritasiums riddle using Perl by pfaut
in thread Proving Veritasiums riddle using Perl by cavac

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.