int main()
{
srand(time(0));
int counter = 100000;
int WinSwitchCounter = 0;
int WinStayCounter = 0;
int decision;
cout << "Number of Times Staying Was The Correct Strategy: "
<< WinStayCounter << endl;
cout << "Number of Times Switching Was The Correct Strategy:"
<< WinStayCounter << endl;
for (int decision = 0; decision <= counter; ++decision)
{
int prize_door = (rand() % 3 + 1);//random choice from 1-3
int stay_choice = (rand() % 3 + 1);
int switch_choice = (rand() % 2 + 1);
if (stay_choice == prize_door)
{
++WinStayCounter;
}
else if (switch_choice == prize_door)
{
++WinSwitchCounter;
}
//Compare Stay to Switch to determine best strategy
if (WinStayCounter > WinSwitchCounter)
{
cout << "Therefore, the best thing to do is to Stay " << endl;
}
else
{
cout << "Therefore, the best thing to do is to Switch " << endl;
}
return 0;
}
}
v
VulpesPosted Oct 3, 2012, 5:28 AM
When I ran it before, I always obtained similar results which is what you'd expect as switching should, of course, be the correct strategy twice as often as staying.
As you'll have gathered, I'm numbering the doors 0, 1 and 2 rather than 1, 2 and 3 as this makes life easier when dealing with zero-based arrays.
The array, doors, simply stores what's actually behind the doors. It's initialized to contain all zeros (goats) and then the prize (car) is placed at random behind one of the doors.
The player also chooses a door at random.
Finally, what's behind one of the doors is shown. This shouldn't be the same as the player's choice or be the car - if it is the do/while loop continues until an acceptable door is found.
You then figure out whether you'd have won by staying or switching and add it to the appropriate counter.
Q FPosted Oct 2, 2012, 8:01 PM
Number of Times Switching Was The Correct Strategy:33409
Therefore, the best thing to do is to Switch
This is not right, I am slightly confused about the arrays and what you put in them
VulpesPosted Oct 2, 2012, 7:10 PM