1

根据我读到的概率,切换门应该有大约 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++;
    }

}
4

3 回答 3

4

你的while条件是错误的:

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

应该

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

等等

于 2013-01-27T22:49:53.640 回答
0

你的条件颠倒了。do ... while (...) 循环将按照您的评论所述执行,如果它们是重复的 .. until(...),它对于终止测试具有相反的极性。

否定实现所需算法的条件。

请注意,在这两种情况下,您最多可以选择两扇门。使用这些知识,您最多可以使用一次 rand() 并且没有循环来确定隔壁。

于 2013-01-27T22:59:05.233 回答
0
// "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);

当玩家选择与之前相同的门或打开的门时,此条件为假(即循环结束)。这也与您想要的相反。

颠倒条件给出了预期的结果(~0.66)。

于 2013-01-27T23:22:41.137 回答