根据我读过的概率,切换门应该会产生〜66%的机会选择正确的门。下面的代码是我想出的,它吐出大约50%的胜利,而不是我期望的66%。对于我在这里出错的任何帮助,将不胜感激。

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++;
    }

}

最佳答案

您的while条件是错误的处理方式:

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

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

等等

关于c++ - Monty Hall模拟中的意外结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14553209/

10-11 16:32