蒙蒂霍尔模拟中的意外结果
Unexpected results in Monty Hall simulation
根据我读到的概率,切换门应该产生~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)
等。
你的条件颠倒了。做...而 (...) 循环将按照您的注释描述执行,如果它们是重复的......直到(...),其端接测试的极性相反。
否定实现所需算法的条件。
请注意,在这两种情况下,您最多都有两扇门可供选择。利用这些知识,您最多只能使用 rand() 并且没有循环来确定隔壁。
// "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)。
相关文章:
- 尝试将字符串/字符转换为整数会产生意外结果
- RapidXML - 代码创建意外结果
- 类中静态函数C++意外结果
- 指针数组中的意外结果
- 使用指针访问数组元素时出现意外结果
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- C++:比较运算符>和字符串文本的意外结果
- 具有意外结果的 C++ 闭包
- yaml-cpp 比较的意外结果
- 每次都出现意外结果
- 在 Qt 中解析嵌套的 JSON 时出现意外结果(数组不存在)
- 如何避免 std::abs 的意外结果?
- 使用嵌套 if 语句的意外结果
- A ^= B ^= A ^= B;C# Visual Studio 中的意外结果
- 逻辑错误,我将获得意外结果
- 在 c++ 中使用异步的意外结果
- 从函数的返回值将元素C++存储到 std::vector 中时出现意外结果
- 执行递增和递减时"cout"链接会产生意外结果
- OpenCL - 内核方法返回意外结果
- C++使用 std::get_time 解析 YYMMDD ISO 8601 日期字符串会得到意外结果