Your simulation is bad. $opened is always 1 or 2, depending on whether you picked a car. In fact $opened is (1/3 odds) the same as the door you pick. After that it is coincidence that you did a calculation that came out to the right answer.
Here is code that lets you try different scenarios and see how Monty's behaviour and knowledge affect the outcome. You can uncomment the scenario that you want to see that behaviour.
#! perl -slw
use strict;
use List::Util qw[ shuffle ];
my( $stick, $switch, $skip_goat, $skip_car ) = (0, 0, 0, 0);
for ( 1 .. 100_000 ) {
## Randomly hide the prizes behind the doors.
my @doors = shuffle 'car', 'goat', 'goat';
## Pick a door
my $choice = int rand 3;
####
## Uncomment the option you want here to see different scenarios.
###
## Option 1: The host opens a door that I didn't pick
## and that isn't the car
#my @available = grep{ $_ != $choice and $doors[ $_ ] ne 'car' } 0
+ .. 2;
## Option 2: The host opens a random door
#my @available = grep{ $_ != $choice } 0 .. 2;
## Option 3: The host tries to be malicious
#my @available = grep{ $_ != $choice and $doors[ $_ ] eq 'car' } 0
+ .. 2;
#@available = grep{ $_ != $choice } 0..2 if not @available;
####
## End of options
####
# Monty chooses which door to open from the choices
# that he might make.
my $opened = $available[rand(@available)];
if ($doors[$opened] eq 'car') {
$doors[ $choice ] eq 'car' ? $skip_car++ : $skip_goat++;
next;
}
## Count my wins if I stick or switch
$doors[ $choice ] eq 'car' ? $stick++ : $switch++;
}
printf "Odds of
Not getting here if you were originally right: %.3f
Not getting here if you were originally wrong: %.3f
Win if you don't switch: %.3f
Win if you do switch: %.3f\n",
$skip_car / (( $stick + $switch + $skip_goat + $skip_car) / 100 ),
$skip_goat / (( $stick + $switch + $skip_goat + $skip_car) / 100 )
+,
$stick / (( $stick + $switch) / 100 ),
$switch / (( $stick + $switch) / 100 )
Now that said, let me explain why the odds are what they are for each option.
In Option 1, you pick a door and have 1/3 odds of being right. There are 100% odds that you'll see a goat, so the fact that you saw one tells you nothing. Therefore your odds of being right remain 1/3. Since switching makes you right if you were wrong, your odds if you switch are 2/3 - so you want to switch.
In Option 2, you pick a door and have 1/3 initial odds of being right. However if you were right, then you're guaranteed to see a goat next, while if you're wrong, you have only even odds of seeing a goat next. Therefore the knowledge that you actually saw a goat conveys information - if you do the math just enough information to tell you that you now have even odds of being right.
In Option 3, you again pick a door and have 1/3 initial odds of being right. However the fact that you saw a goat gives you considerable information - it literally tells you that you must be right. Since you're right, you don't want to switch.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.