Re: Marilyn Vos Savant's Monty Hall problem
by Joost (Canon) on Aug 19, 2004 at 13:53 UTC
|
Cool. One remark: using int rand() instead of your random() sub should make it more efficient, and IMHO more readable:
#!/usr/bin/perl
my $wins = 0;
my $lose = 0;
my $goat;
for (my $i=0;$i<1000000;$i++) {
my $car = int rand(3);
my $choice = int rand(3);
do {
$goat = int rand(3);
} while (($goat != $car) && ($goat != $choice));
if ($choice != $car) {$wins++} else {$lose++};
}
print "WINS = $wins!\nLOSE = $lose!\n";
takes about 10 wallclock seconds at my machine, while the original takes more than 70 seconds.
| [reply] [d/l] [select] |
|
| [reply] |
Re: Marilyn Vos Savant's Monty Hall problem
by MADuran (Beadle) on Aug 24, 2004 at 16:12 UTC
|
| [reply] |
Re: Marilyn Vos Savant's Monty Hall problem
by BrowserUk (Patriarch) on Aug 19, 2004 at 15:02 UTC
|
This is a falacy. It ignores the possibility that the host will pick the door with the car and therebye deprive you of the option of switching your original choice.
Try this simulation and see if you still agree?
#! perl -slw
use strict;
use List::Util qw[ shuffle ];
our $TRIALS ||= 1_000_000;
my $DIVISOR = $TRIALS / 100;
my( $stick, $switch, $loseEitherWay );
for my $try ( 1 .. $TRIALS ) {
## Randomly hide the prizes behind the doors.
my @doors = shuffle 'car', 'goat', 'goat';
## Pick a door
my $choice = int rand 3;
## The host opens a door that I didn't pick
my $opened = ( grep{ $_ != $choice } 0 .. 2 ) [ rand 2 ];
## If the host picks the car, I lose anyway.
$loseEitherWay++, next if $doors[ $opened ] eq 'car';
## Count my wins if I stick or switch
$doors[ $choice ] eq 'car' ? $stick++ : $switch++;
}
printf "Odds of
Losing either way: %.3f
Win if you don't switch: %.3f
Win if you do switch: %.3f\n",
$loseEitherWay / $DIVISOR, $stick / $DIVISOR, $switch / $DIVISOR;
__END__
P:\test>384288
Odds of
Losing either way: 33.370
Win if you don't switch: 33.307
Win if you do switch: 33.324
P:\test>384288 -TRIALS=5000000
Odds of
Losing either way: 33.318
Win if you don't switch: 33.336
Win if you do switch: 33.346
Any bias between the outcomes is statistical variation only.
It runs very quickly because it doesn't loop trying to pick a number that hasn't already been picked. The grep removes the number I picked from the choices, and rand 2 picks between the 2 that are left.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
| [reply] [d/l] [select] |
|
That's nice, but the stipulation of the Monty Hall problem is that he ALWAYS reveals a goat, never the prize.
_____________________________________________________
Jeff japhy Pinyan,
P.L., P.M., P.O.D, X.S.:
Perl,
regex,
and perl
hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
| [reply] |
|
I've never seen the Monty Hall problem presented with that stipulation explicit.
That extra information is an assumption that is supposed to be based on your knowledge of how game shows work. In fact based on Monty's knowledge and motivations it is possible that you want to switch (Monty knows where the car is and always shows a goat), it doesn't matter (Monty doesn't know where the car is), or you should stick with your choice (Monty knows where the car is and is trying to keep you from getting it).
| [reply] |
|
|
|
|
|
|
|
|
That fallacy is the point of the exercise. If you assume that the host will never pick the door with the car (because the host shows you what's behind the door), your chance of picking the car is doubled if you change your mind after seeing the goat-door the host picks.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] |
|
your chance of picking the car is doubled if you change your mind after seeing the goat-door the host picks.
Not exactly--the chance goes from 1 in 3 to 1 in 2.
| [reply] |
|
|
|
|
|
|
It is stipulated that he will always open a door that you did not pick, reveal a goat, and allow you to try again. Of course if you vary from the proposed problem you will obtain a different solution. I still think it's interesting that changing your choice between to unknowns effects your odds. I understand why, it just seems odd...
| [reply] |
Re: Marilyn Vos Savant's Monty Hall problem
by giulienk (Curate) on Aug 25, 2004 at 07:14 UTC
|
$goat = rand(3);
should be
$goat = int rand(3);
$|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g
| [reply] [d/l] [select] |
|
Yup I guess I really didn't pay much attention to that part of the code as it doesn't really do anything, it's fixed now regardless though, I could remove the entire do while loop and the code still works fine..
| [reply] |
Re: Marilyn Vos Savant's Monty Hall problem
by sharkey (Scribe) on Aug 27, 2004 at 19:41 UTC
|
Why use rand?
If you stay with your first choice, you have 1/3 chance of getting the car.
If you switch, there is 2/3 chance of getting the car:
2/3 chance you will pick a goat on the first pick, he reveals the other goat, and then you switch to the car.
1/3 chance you will pick a car, he reveals a goat, and you switch to the other goat.
| [reply] |
|
Your right, Rand really isn't neccessary, and I can remove the do while loop as well, but I think that having them in there helps connect the word problem with the code..after all once someone has thought the problem through enough to realise that the do while is not neccesary they don't need a demonstration program any more to prove it ;).
| [reply] |