Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Holiday parcel puzzle

by JavaFan (Canon)
on Dec 28, 2011 at 01:17 UTC ( [id://945259]=note: print w/replies, xml ) Need Help??


in reply to Holiday parcel puzzle

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%

Replies are listed 'Best First'.
Re^2: Holiday parcel puzzle
by cavac (Parson) on Dec 29, 2011 at 22:49 UTC

    You are right, of course. And your solution is guaranteed to always be mathematically correct, since you don't depend on random numbers to approximate the result.

    Nice!

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://945259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found