Thread: MONTY HALL problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    MONTY HALL problem

    I'm not sure if everyone has heard of the monty hall scenario but I have written a program which attempts to reproduce it. I've been quite boggled with it so far and I've written this as an attempt I really need an answer by tonight if possible so any input/help would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	int choice = rand() % 3, prize = rand() % 3, otherdoor, choice2, winstay = 0, winmove = 0, i, x;
    	float percentstay, percentmove;
    
    for(x = 0; x <= 10000; x++)
    	{
    
    if( choice == prize)
    {
    for( i= 0; i <= 3; i++)
    {
    	if( i != prize)
    	{
    		otherdoor = i;
    	}
    }
    
    choice2 = (rand()%2);
    
    	if(choice2 == prize)
    	{
    		if(choice2 == choice)
    		{
    			winstay++;
    		}
    
    	}
    }
    if( choice != prize)
    {
    	for( i = 0; i <= 3; i++)
    	{
    		if(i != prize&&choice)
    		{
    			otherdoor = i;
    		}
    	}
    	choice2 = (rand()%2);
    	if(choice2 == prize)
    	{
    		if(choice2 == prize)
    		{
    			winmove++;
    		}
    
    	}
    
    }
    }
    
    percentstay = (winstay / 10000) * 100;
    percentmove = (winmove / 10000) * 100;
    
    printf("wins changing %.2f\nwins staying %.2f\n" , percentmove , percentstay);
    
    system("pause");
    return 0;
    }
    Last edited by jackalope; 10-27-2010 at 07:21 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I remember when this came out in a column by "The World's Smartest Woman", in Parade Magazine. What a hoot that was! She made a lot of mathematicians (and other smart people), eat crow. I was laughing till my sides ached.

    Your program is difficult to fathom, because it doesn't follow the natural flow of the Monty Hall problem, imo.

    First, the user chooses a door, from the set of doors. Then you figure out the probability of his choice being the correct door for the big prize: 1/number of doors. (never mind the billy goat prize)

    Now as the doors are opened one by one, re-calculate the probability of the first chosen door, being the door with the big prize.

    Unless you want a Monte Carlo analysis of lots of Monty Hall problems, I don't understand why there would be no user input. That's the core of the whole thing, imo.

    So what *exactly* are you trying to do here, and what has you stumped about your program?

    The more exact you can be, the better your help will (in all probability ), be.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    All I can think of when you mention that is the old posters of bearded lady ha.
    Sorry I forgot to mention that it is all by simulation. We are letting the computer run through the program 10000 times and see what the percentage of wins is for changing doors and for staying.
    As well I think it would be much easier if there was user input if I'm not mistaken.
    Last edited by jackalope; 10-27-2010 at 06:46 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't call Marilyn a raving beauty, but no bearded freak job, either:
    Marilyn vos Savant - Wikipedia, the free encyclopedia

    Lots more info on the Monty Hall problem here:
    Monty Hall problem - Wikipedia, the free encyclopedia

    So what is your problem with the program? What has you stumped?

    What looks very wrong to me, in your program, is that the pick of doors (taking the role of Monty Hall), appears to be random - and that's NOT the Monty Hall problem. The key thing is that Monty *knows* which door has the car, and has to always open a door that *doesn't* have the car.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    No I just meant what you said just reminded me of like old magazines and things.
    I digress.
    The idea of my program is that it is random though. Your contestant picks a random door of the 3.
    Then my program finds whether or not he has chosen the prize door. If he has chosen the prize door I can choose the first available other door of the 3 and reveal what is behind it, leaving the contestant to choose a random door of the remaining 2. This would equate to either staying with his original choice or changing doors to the only other available door.
    If on the other hand he chooses the wrong door the only other door which can be revealed is revealed. This leaves my program to choose a random door of the remaining 2. Staying resulting in a loss or changing resulting in a win. I hope that helps.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    *added a for loop i had forgotten which might have been the cause of the confusion*

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The "otherdoor" should be chosen from among the non-prize doors, at random. Currently, the program always chooses the highest non-prize door. (since this is a simulation, and against a program, I'm not sure this matters), but a human would exploit that for a massive advantage. Monty Hall would go postal!

    I thought you'd tell us what's wrong with the program. I'm not going to run it, analyze the results against the real odds, etc.. That's your job.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You have a number of problems. First, your for loops should have i < 3, not i <= 3 as the test condition.

    Second, what are you doing with otherdoor after you assign it a value? Nothing!

    Third, what does this expression (i != prize&&choice) mean?

    Fourth, the second random choice should be choosing between whichever 2 doors were not opened by Monty. But you are always choosing either door 0 or door 1 on the second pick regardless of which door Monty opens.

    And the list goes on ...

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    Changed my code a lot
    the section commented I'm fairly certain is getting the correct value whereas the uncommented is not. Any idea how to get the right value im assuming it has someing to do with my do while loops.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int secondchoice (int x);
    
    int main()
    {
    	srand(time(NULL));
    	int x, prize, choice1, choice2, win = 0, reveal, reveal2, i ;
    	float percent;
    
    for( x = 0; x <= 10000; x++)
    {
    prize = 1+rand()%3;
    choice1 = 1+rand()%3;
    
    if(choice1 == prize) //if choice is the prizedoor
    {
    for (i = 0; i <= 3; i++)
    {
    	if(i != prize)
    	{
    		reveal = i;
    	}
    }
    do
    {
    choice2 = 1 + rand() % 3;
    }
    while( choice2 == reveal);
    
    if( choice2 == prize)
    {
    	win++;
    }
    }
    
    if(choice1 != prize) //if original choice is not the prizedoor
    {
    
    	if(secondchoice(x) == 1)
    	{
    		win++;
    	}
    
    }
    
    }
    percent = win/100;
    	
    	printf("wins %.2f\n" , percent );
    	system("pause");
    
    }
    int secondchoice (int x)
    {
    	int choice1 = 1,prize = 2,choice2;
    choice1 = 1;
    prize = 2;
    choice2 = 1 +rand() % 2;
    if (choice2 = prize)
    {
    	return 1;
    }
    return 0;
    }
    hopefully final code
    Last edited by jackalope; 10-28-2010 at 07:32 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My "pet peeve" after indentation is this:

    for(i=0;i<=SomeNumber;i++)

    That little = sign, has (and loves to), trip up programmers, like a siren's song wrecks sailors. Can you see that you'll actually be running SomeNumber+1 times around that for loop?

    Back to studying your code.
    Lose the bloody =, whenever you can.

    This is not how Let's Make a Deal, was played, or the Monty Hall problem, imo.

    Code:
    if(choice1 == prize) //if choice is the prizedoor
    {
    Monty ALWAYS opened up another door, to show off the merchandisers STUFF, that paid to be on the show. Then he'd ask them if they wanted to take that door (and those prizes), in exchange for the door they'd chosen.

    Then he'd remind them of the other prizes briefly, and ask if they wanted to take the OTHER door, that was still unopened, or stick with their original choice of door.

    As I understand the problem, that's the crux of the whole thing. Are the odds better if you now take the other unopened door, or are they the same as staying with the door you chose before any door was opened?

    Sometimes the contestant would swap doors after the first one was opened (perhaps understanding the odds), but it did happen that they lost out, and chose the door with the Billy goat behind it (or other little mundane gift of dubious value), and the door they first chose, actually had the new car, behind it.

    That was always a big groan, moment!

    I'm getting that firm conviction that you wrote this assignment without benefit of pseudo coding the problem, first.

    If you change the way Let's Make a Deal, worked, I'm not able to tell whether your program is actually testing the Monty Hall problem anymore, or not.
    Last edited by Adak; 10-28-2010 at 09:33 PM.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Adak's comment is correct but your problem is much more fundamental than that. You don' t seem to have a clear grasp of what you are supposed to be counting. I can think of 3 ways to approach this problem but you aren't doing any of them. Either you can assume that the player ALWAYS switches and then only count the number of times he wins. Or you can assume that the player NEVER switches, again counting only the number of times he wins. For either of those approaches the code should be MUCH simpler than yours.

    Or, finally, you can have the player choose randomly whether to switch or to stay, in which case you would count winstay, winswitch, staycount and switchcount and can report the % of times he wins when staying and % of times he wins when switching.

    Even in the 3rd case the code should be simpler than yours.

    You are ignoring information that is available to you. For example, if the original choice was the prize door, the player wins if he stays, loses if he switches. And if the original choice was NOT the prize door, the player is guaranteed to win if he switches and lose if he stays. So there is no reason to do a 2nd random choice of door. You can automatically determine a win or a loss just by knowing if the original choice was correct or not, and whether or not the player switches. In the 3rd approach, you need a random choice of whether to switch or to stay -- knowing that decision automatically tells you which door he ends up with, since Monty takes away the third possibility.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM