#!/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 sticking to your initial decision.\n";
####
#!/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 select the remaining one **
# First, make a "list" of parcels and remove the one the user is holding
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 changing your decision.\n";
##
##
You were lucky 33% of the time sticking to your initial decision.
You were lucky 66% of the time changing your decision.