Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Marilyn Vos Savant's Monty Hall problem

by mutated (Monk)
on Aug 19, 2004 at 13:43 UTC ( [id://384288]=CUFP: print w/replies, xml ) Need Help??

I recently came across Marilyn Vos Savant's Monty Hall problem, which states: You are at a game show, and you are presented three doors, behind one of the doors there is a car and the other two there are goats. You pick a door, and the gameshow host opens a door that you did NOT pick which contains a goat, you have a better chance of winning the car if you switch your door to the other remaining door. I did not believe this statement when I first heard it, and I actually made a bet that it wasn't true. I then spent some time thinking about it and realized it in fact was true and wrote a little perl program to prove it, you actually have a 66.666...% chance of picking the right door if you switch your answer.. anyways here's the code..
#!/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";
UPDATE: Following Joost's recommendation I have removed my own random wrapper and used int rand instead


daN.

Replies are listed 'Best First'.
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.

      Hey cool thanks!


      daN.
Re: Marilyn Vos Savant's Monty Hall problem
by MADuran (Beadle) on Aug 24, 2004 at 16:12 UTC
    I found this discussion interresting but I notice no one mentioned Monty Hall's Solution to the Monty Hall problem that he gave to a New York Times reporter when this problem first made the rounds in the early Ninties.
    I found a partial interview here

    I found this quote Here (a PDF)
    Monty Hall declared, “Her answer’s right. You should switch.” But, after thinking about it a bit, Monty qualified his response: “If the host is required to open a door all the time and offer you a switch, then you should take the switch. But if he has the choice whether to allow a switch or not, beware. Caveat emptor. It all depends on his mood.”

    Note that the problem is more a psychological one then a probabilty one...


    MADuran
    Who needs a spiffy sig
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
      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
        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).

      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

        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.
      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...


      daN.
Re: Marilyn Vos Savant's Monty Hall problem
by giulienk (Curate) on Aug 25, 2004 at 07:14 UTC
    You still miss a int:
    $goat = rand(3);
    should be
    $goat = int rand(3);


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

      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..


      daN.
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.

      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 ;).


      daN.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://384288]
Approved by Joost
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found