It's funny that when people "tackle" the Monty Hall problem with a program, they tend to write one that uses a simulation, running thousands (if not more) of test runs. And that while it's easy to brute-force it, as there are at most 108 cases to examine (3 places for the main prices, 3 initial picks, 2 possible doors for Monty to pick, switch/not-switch).
use 5.010; my @doors = (1 .. 3); my $switch_wins = 0; my $switch_losses = 0; my $noswitch_wins = 0; my $noswitch_losses = 0; # # Place a price # foreach my $price (@doors) { # # Pick a door # foreach my $pick (@doors) { # # Monty's pick # my @monty_choices = grep {$_ != $price && $_ != $pick} @doors; foreach my $monty_pick (@monty_choices) { # # Switch or not? # foreach my $switch (0, 1) { # # If switching, find final door # my $final_pick; if ($switch) { ($final_pick) = grep {$_ != $pick && $_ != $monty_pick} @doors; } else { $final_pick = $pick; } my $prob = 1 / @monty_choices; if ($switch) { $switch_wins += $prob if $final_pick == $price; $switch_losses += $prob if $final_pick != $price; } else { $noswitch_wins += $prob if $final_pick == $price; $noswitch_losses += $prob if $final_pick != $price +; } } } } } printf "Switching: win percentage = %.2f%%\n", 100 * $switch_wins / ($switch_wins + $switch_losses); printf "No switching: win percentage = %.2f%%\n", 100 * $noswitch_wins / ($noswitch_wins + $noswitch_losses); __END__ Switching: win percentage = 66.67% No switching: win percentage = 33.33%

In reply to Re: Holiday parcel puzzle by JavaFan
in thread Holiday parcel puzzle 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.