根据我所读到的概率,开关门应该有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++;
}
}
答案 0 :(得分:4)
您的while
条件是错误的:
while (openedDoor != wDoor && openedDoor != aiDoor)
应该是
while (openedDoor == wDoor || openedDoor == aiDoor)
等
答案 1 :(得分:0)
你的条件已经逆转了。 do ... while(...)循环将按照您的注释描述执行,如果它们重复...直到(...),它具有与终止测试相反的极性。
取消实现所需算法的条件。
请注意,在这两种情况下,您最多有两扇门可供选择。使用这些知识,您可以确定隔壁最多只使用一次rand()而没有循环。
答案 2 :(得分: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)。