Kefkamaydie
Kefkamaydie

Reputation: 127

Unexpected results in Monty Hall simulation

According to the probabilities I've read about, switching doors should yield ~66% chance to pick the correct door. This code below is what I've come up with and it spits out roughly 50% wins instead of the 66% I am expecting. Any help on where I'm going wrong here would be much appreciated.

for (int count = 0; count < 10000; count++)
{
    // Chooses which door contains DAT GRAND PRIZE YO.
    wDoor = rand() % 3 + 1;

    // AI Contestants Door choice
    aiDoor = rand() % 3 + 1;

    // Using oldChoice to ensure same door isn't picked.
    oldChoice = aiDoor;
    // Used in determining what door to open.
    openedDoor = aiDoor;

    // "Open" a door that is not the winning door and not the door chosen by player.
    do
    {
                openedDoor = rand() % 3 + 1;

    }while (openedDoor != wDoor && openedDoor != aiDoor);

    // Select new door between the remaining two.
    do
    {
              aiDoor = rand() % 3 + 1;

    }while (aiDoor != oldChoice && aiDoor != openedDoor);

    // Increment win counter if new door is correct.
    if (aiDoor == wDoor)
    {
               chooseAgain++;
    }

}

Upvotes: 1

Views: 300

Answers (3)

molbdnilo
molbdnilo

Reputation: 66461

// "Open" a door that is not the winning door and not the door chosen by player.
    do
    {
                openedDoor = rand() % 3 + 1;

    }while (openedDoor != wDoor && openedDoor != aiDoor);

This condition is false (i.e. the loop ends) when you've opened either the winning door (!) or the one the player picked. This is the opposite of what you want.

    // Select new door between the remaining two.
    do
    {
              aiDoor = rand() % 3 + 1;

    }while (aiDoor != oldChoice && aiDoor != openedDoor);

This condition is false (i.e. the loop ends) when the player has picked either the same door as before or the opened door. This is also the opposite of what you want.

Reversing the conditions gives the expected result (~0.66).

Upvotes: 0

JoergB
JoergB

Reputation: 4463

You have your conditions reversed. The do ... while (...) loops would performs as your comments describe, if they were repeat .. until(...), which has the opposite polarity for the termination tests.

Negate the conditions to implement the algorithm you want.

Note that in both cases you have at most two doors to choose from. Using that knowledge, you can determine the next door with at most a single use of rand() and no loop.

Upvotes: 0

NPE
NPE

Reputation: 500743

Your while conditions are the wrong way round:

while (openedDoor != wDoor && openedDoor != aiDoor)

should be

while (openedDoor == wDoor || openedDoor == aiDoor)

etc.

Upvotes: 4

Related Questions