Thread: Monty Hall Problem

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    29

    Monty Hall Problem

    I have to write a program illustrating how the Monty Hall Problem works, which I will write below. I have a poor teacher, so i have very basic programming knowledge. Any help is appreciated!

    You are to write two programs.
    Code:
    The First Program
    The first program will act as the game host that continuously hosts this game and the user of the program
    will act as the player.
    The program will loop over the playing of this game. In every game, the doors are labelled by 1, 2, and 3 respectively. A game begins by putting the prize behind a door at random, then prompts the user to pick a number from {1;2;3}, which indicates the door the user picks.
    The program then decides a door to open. The door must be one behind which there is no prize and which is not picked by the user. In case there are two such doors available, the program picks one at random.
    The program next prompts the user to decide if he wants to switch. Based on the user's decision, the program outputs if the user won the prize or not. 
    The loop over game plays finishes if the user picks an invalid door number (a number outside {1;2;3}) at the beginning of the game. When the loop is over, the program outputs the total number of games that have been played, the number of games in which the user has won, and the relative frequency of winning.
    
    The Second Program
    The second program simulates the playing of the games by two specific users. The first user consistently decides on "switch" and the second user consistently decides on "not switch". For each user, 10000 game plays will be simulated, and in each game, the initial pick by the user is made random. The program may not print some intermediate results indicating the progress of the program, but in the end it must
    print how many games each user has won and the winning frequencies for each user.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, tysawyers!

    This is a remarkable problem. I remember when Marilyn (very high IQ), posted this problem in Parade Magazine. What a howl! Many math teachers were completely fooled by it, and wrote in to tell her she should apologize for her dumb mistake. Others were giving her props for her insight into the probability of it.

    Be warned, games take longer to program than you expect, nearly always. This will be no exception, I'm sure.

    Read over the description you posted above, and note the words - "continuously" "loop" "every game", etc. Note the patterns of the game as it's being played.

    Then come up with a broad plan to include those patterns, into an algorithm. Refine it down to the simple levels that a computer program could use.

    And post up your code, if/when you get stuck.

    We don't start programs for people, but we will pitch in and help, when you get stuck. We will also discuss the program beforehand, but the real start of the program is up to you.

    Take it part by part, and try to break it down into functions. Typically, a game has a big loop that calls all the other functions that actually are used in playing the game.

    See what you can do to start this program.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    I am honestly really having trouble getting started with this, I know you have to create an array with 3 values and then randomly store the prize within one of those values but im not even sure how to do that?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two things to consider:

    1) making two programs involving probability, is probably 100% not a great choice for one of your first "word problems" to make into a program.

    2) you could get a better knowledge of C, if you clicked on the C tutorial tab near the top center of this web page. It's quite good. Surely it would help to go through the array portion of it, at the very least.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Is it that you don't know how to set up an array(declaring it, etc),
    or you don't know how to represent a prize location in the array and place it there?

    If it's the latter, first you need to decide on values to represent the prize and the empty doors.
    'Zero' suggests itself for the empty door and 'one' would work well for the prize.
    Each array element represents a door.

    The simpliest approach is to first empty all the doors (store zeros in all the arrray elements.
    Then generate arandom value, 1, 2 or3, and store a one in the corresponding array element.

    -

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Adak View Post
    This is a remarkable problem. I remember when Marilyn (very high IQ), posted this problem in Parade Magazine. What a howl! Many math teachers were completely fooled by it, and wrote in to tell her she should apologize for her dumb mistake. Others were giving her props for her insight into the probability of it.
    The fundamental flaw in that logic is that the mere act of choosing has no affect whatsoever on the probability of the actual outcome - in any case, the probability is still simply 50-50. Put another way, odds have nothing to do with the sequence of events, just the degrees of freedom present in a system.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Sebastiani View Post
    The fundamental flaw in that logic is that the mere act of choosing has no affect whatsoever on the probability of the actual outcome - in any case, the probability is still simply 50-50. Put another way, odds have nothing to do with the sequence of events, just the degrees of freedom present in a system.
    There is a significant increase in the probability of winning, if you switch your choice, in this problem, versus keeping the same choice of doors.

    It doesn't seem that way, but that's the beauty of this problem.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    It took my a long time to understand why switching was the best choice.

    I finally figured out a way to explain it to myself; like many probability questions it is easier to figure out your chance of losing and then do 1-x to get chance of winning.

    If you pick 1 out of 3 with random right answer your chance of winning is 1/3 your chance of losing is 2/3.
    Now, if the host reduces the number of not picked doors to one; then the probability of your door and that door must equal 1.
    Therefore, if you switch the prob. of winning is 2/3 instead of the 1st choice of 1/3.

    Note: The above does NOT help you to program the problem; but, I see no way to help till you post code or ask questions that are direct instead of open ended.
    Main thing with random when seeding only do it once at the start of the program for these types of simple programs.


    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by stahta01 View Post
    Main thing with random when seeding only do it once at the start of the program for these types of simple programs.
    Another potential problem is the distribution of random numbers such that the range is [1-3]. This of course depends on how you reduce the range returned by rand()

    Edit: Look Ma, I found a FAQ! FAQ > Generate random numbers? - Cprogramming.com

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Adak View Post
    There is a significant increase in the probability of winning, if you switch your choice, in this problem, versus keeping the same choice of doors.
    I don't think there is. The key words of the premise are "if you switch your choice". Now I ask you, where in all of your probabilistic calculations have you accounted for the free will of the individual being asked? You haven't (and most certainly shouldn't)!

    Suppose now that we replace the individual with a simple machine that makes it's "choices" based on the decay of a some radioactive isotope. Both the initial guess and the choice to switch are now selected based on the random numbers generated by the radioactive sample. Does it really matter whether or not the second number leads to switching?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Sebastiani View Post
    .... based on the decay of a some radioactive isotope. ...
    I think you have a better computer than me if that's how you generate random values :-(

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by stahta01 View Post
    It took my a long time to understand why switching was the best choice.

    I finally figured out a way to explain it to myself; like many probability questions it is easier to figure out your chance of losing and then do 1-x to get chance of winning.

    If you pick 1 out of 3 with random right answer your chance of winning is 1/3 your chance of losing is 2/3.
    Now, if the host reduces the number of not picked doors to one; then the probability of your door and that door must equal 1.
    Therefore, if you switch the prob. of winning is 2/3 instead of the 1st choice of 1/3.
    This is the inherent problem with probability, or rather, our misuse of it as such. The information that it encodes has neither a dimension of causality nor a basis in actuality. It simply tells us what can possibly happen, not what is going to happen or why what does end up happening does happen in the first place. In other words, there is a definite limit to it's applicability to real-world situations.

    Apparently, most mathematicians who've chimed in on this matter seem to have either missed or ignored this distinction, so the consensus by majority rule is in favor of Ms. Savant's position. But there are nonetheless many of us out there who remain unconvinced.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by SirPrattlepod View Post
    I think you have a better computer than me if that's how you generate random values :-(
    Believe it or not, you can generate truly random values on most computers using little more than a loop and counter. I'll leave it as an exercise so that you can figure out for yourself just how and why this even possible...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    The program in question will provide emperical results which are accurate within the limits of the sample size.

    Many will find the results surprising.

    Some will refuse to believe them.

    -

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Sebastiani View Post
    Believe it or not, you can generate truly random values on most computers using little more than a loop and counter. I'll leave it as an exercise so that you can figure out for yourself just how and why this even possible...
    I might write a solution using cogs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help with "Monty Hall" simulator program.
    By jsokol7 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2011, 11:54 PM
  2. MONTY HALL problem
    By jackalope in forum C Programming
    Replies: 10
    Last Post: 10-28-2010, 09:43 PM
  3. Replies: 1
    Last Post: 12-18-2007, 12:39 AM
  4. Monty Python
    By Stoned_Coder in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-18-2001, 01:29 PM

Tags for this Thread