PerlMonks Santa (or s/Santa/friend of yours/g on PM, depending on your religious views) has given you a challenge:
You have three (3) nicely wrapped up parcels in front of you. They look, smell, weigh etc. exactly the same. Except two hold a blank scrap of paper, one of them holds a coupon for 1000XP and instant sainthood on PerlMonks (thats the one you desired for years!).
But given that you can only select ONE of the parcels at random, your changes of gratification are slim.
You select one. Then Santa removes one of the other ones. He also promises you that the one he removed was in fact one of the empty ones. You have now the chance to either stick to the one you selected or switch to the remaining one still on the table (giving the one you currently hold back to Santa).
How are your chances if you stick to the parcel you selected at the start? How are your chances if you switch to the one Santa has left on the table?
Take an educated guess and write it down.
Now, write two scripts, one where you stick to your initial decision, one where you switch. Loop a sufficient number of times (say 10000) to get some statistical significance. Did your guesses match your expectation? So, if you encounter this challenge in real life, what would be the best strategy?
Ok, here's the scripts, statistical results and the answer i came up with:
First, the script where we stick to out decision:
#!/usr/bin/perl use strict; use warnings; my $runs = 10000; my $luck = 0; for(1..$runs) { # Generate the lucky parcel my $hasxp = int(rand(3)) + 1; # Now, select one parcel from the 3 at random my $parcel = int(rand(3)) + 1; # We stick to our decision, so we don't care # what happens to the other parcels # Now, see if we were lucky: if($parcel == $hasxp) { $luck++; } } # print out the end result in percent print "You were lucky " . int($luck * 100 / $runs) . "% of the time st +icking to your initial decision.\n";
Next, we just add some parcel switcheroo to the first script to simulate impartial Santa and your decision to switch. Here's the script:
#!/usr/bin/perl use strict; use warnings; my $runs = 10000; my $luck = 0; for(1..$runs) { # Generate the lucky parcel my $hasxp = int(rand(3)) + 1; # Now, select one parcel from the 3 at random my $parcel = int(rand(3)) + 1; # ** Now, the computer removes one of the empty parcels and we sel +ect the remaining one ** # First, make a "list" of parcels and remove the one the user is h +olding my $all = '123'; $all =~ s/$parcel//; # Now, randomly select one of the remaining ones and check # if it's empty and *not* currently held by the user while(1) { my $gone = int(rand(3)) + 1; # Check if we can remove that one from the list next if($gone == $parcel); # can't remove, held by the user next if($gone == $hasxp); # don't remove the lucky one $all =~ s/$gone//; # Remove it and finish last; } # Given that we eliminated two parcels from a list of three, # there is one left the user *can* select, so let's do it $parcel = $all; # Now, see if we were lucky: if($parcel == $hasxp) { $luck++; } } # print out the end result in percent print "You were lucky " . int($luck * 100 / $runs) . "% of the time ch +anging your decision.\n";
And, tadaa, here are the statistical results i came up with:
You were lucky 33% of the time sticking to your initial decision. You were lucky 66% of the time changing your decision.
Given the results, the correct strategy when encountering this situation is: Always change your decision!
I hope this little holiday themed puzzle was enjoyable.
Happy Holidays!
BREW /very/strong/coffee HTTP/1.1 Host: goodmorning.example.com 418 I'm a teapot
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Holiday parcel puzzle
by choroba (Cardinal) on Dec 22, 2011 at 17:55 UTC | |
by cavac (Prior) on Dec 22, 2011 at 18:01 UTC | |
|
Re: Holiday parcel puzzle
by Not_a_Number (Prior) on Dec 22, 2011 at 20:12 UTC | |
by cavac (Prior) on Dec 22, 2011 at 20:17 UTC | |
by Not_a_Number (Prior) on Dec 22, 2011 at 20:34 UTC | |
by cavac (Prior) on Dec 22, 2011 at 20:52 UTC | |
by JavaFan (Canon) on Dec 22, 2011 at 22:33 UTC | |
|
Re: Holiday parcel puzzle
by JavaFan (Canon) on Dec 22, 2011 at 21:38 UTC | |
by cavac (Prior) on Dec 22, 2011 at 22:16 UTC | |
by aaron_baugher (Curate) on Dec 23, 2011 at 00:12 UTC | |
by JavaFan (Canon) on Dec 23, 2011 at 01:28 UTC | |
by Don Coyote (Hermit) on Dec 27, 2011 at 12:14 UTC | |
|
Re: Holiday parcel puzzle
by JavaFan (Canon) on Dec 28, 2011 at 01:17 UTC | |
by cavac (Prior) on Dec 29, 2011 at 22:49 UTC | |
|
Re: Holiday parcel puzzle
by Don Coyote (Hermit) on Dec 27, 2011 at 12:48 UTC | |
by cavac (Prior) on Dec 29, 2011 at 22:43 UTC | |
|
Re: Holiday parcel puzzle
by tweetiepooh (Hermit) on Jan 10, 2012 at 15:51 UTC | |
by tobyink (Canon) on Jan 10, 2012 at 16:07 UTC | |
by cavac (Prior) on Jan 10, 2012 at 16:16 UTC | |
by tweetiepooh (Hermit) on Jan 23, 2012 at 16:44 UTC |