I'm writing a code based on the Monty Hall problem. For those of you not familiar with it, you are on a game show and must pick 1 of 3 doors to see if you won the grand prize. However, after making your initial decision, the host reveals one of the incorrect doors (or remaining incorrect door if you have picked the other incorrect door). He then gives you the option to switch your decision to the other unopened door. The program simulates this scenario 10,000 times.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int games = 0;
int stayWins = 0;
int switchWins = 0;
int chosenDoor;
int remainingDoor;
int revealedDoor;
int winningDoor;
int option;
srand (time(NULL));
chosenDoor = rand() % 3 + 1;
winningDoor = rand() % 3 + 1;
do
{
do
{
revealedDoor = rand() % 3 + 1;
} while (revealedDoor == chosenDoor || revealedDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == chosenDoor || remainingDoor == revealedDoor);
option = rand() % 2 + 1;
if (option == 1)
{
if (chosenDoor == winningDoor)
{
stayWins++;
}
}
if (option == 2)
{
chosenDoor = remainingDoor;
if (chosenDoor == winningDoor)
{
switchWins++;
}
}
games++;
} while (games < 10000);
cout << "Out of 10,000 games, the contestant won " << stayWins << " times by staying with his/her original choice and won " << switchWins << " times by switching his/her choice.\n";
return 0;
}
Option 1 is when the contestant chooses to stay with his original choice. Option 2 is when the contestant switches doors. According to the Monty Hall solution, Option 1 should win 1/3 of the time and Option 2 should win 2/3 of the time. However, when I run this program, it seems that he wins 50% of the time, regardless of which option he chooses. Is there an error in my logic or code?