C++抛硬币程序
C++ coin toss program
我试图制作一个C++程序来计算我连续投掷了多少次相同类型的硬币,但结果很奇怪。我试着扔了100万次,我还没有比15次更多的"连续投掷同类型"。我在较少的投掷次数(5-20)上对其进行了测试,它似乎工作正常,除了我也没有连续获得超过15次相同类型的投掷。
#include <iostream>
#include <ctime>
#define LOOP 1000000//how many times do I toss a coin
int main()
{
int maxTails = 0, maxHeads = 0, currentTails = 0, currentHeads = 0, totalHeads = 0, totalTails = 0;
int totalMax = 0;//highest amount of "in a row" out of all iterations
bool heads = false;//last toss
srand(time(0));
for (int x = 0; x < 100; ++x) {//this is to increase the amount of how many times I want to toss a coin LOOP times
for (int i = 0; i < LOOP; ++i)//tosses a coin LOOP times
{
if (rand() % 2 == 0)// heads
{
if (heads == false)//if last toss was tails I check if the "in a row" for it was more than the current maximum
{
if (currentTails > maxTails) maxTails = currentTails;
currentTails = 0;
currentHeads = 0;
heads = true;
}
currentHeads++;
totalHeads++;
if (currentHeads > maxHeads) maxHeads = currentHeads;
}
else//tails
{
if (heads == true)
{
if (currentHeads > maxHeads) maxHeads = currentHeads;
currentHeads = 0;
currentTails = 0;
heads = false;
}
currentTails++;
totalTails++;
if (currentTails > maxTails) maxTails = currentTails;
}
}
if (maxTails > totalMax) totalMax = maxTails;//totalMax is the maximum "in a row" of either tails or heads
if (maxHeads > totalMax) totalMax = maxHeads;
std::cout << "Throws: " << LOOP << ", Total heads: " << totalHeads << ", Total tails: " << totalTails << ", Maximum heads: " << maxHeads << ", Maximum tails: " << maxTails << std::endl;//writes all the info
//std::cout << "Iteration: " << x + 1 << ", Max: " << totalMax << std::endl;
maxTails = maxHeads = currentHeads = currentTails = totalHeads = totalTails = 0;
}
std::cout << "Max: " << totalMax << std::endl;
system("pause");
return 0;
}
我在互联网上读到,如果你掷硬币 1,000,000 次,有 ~38% 的机会连续获得 20 个正面,但我的程序工作方式不同。我的代码中是否存在问题,或者 rand() 函数可能存在问题?
阅读@BathSheba评论。期望与你现在得到的输出不同的输出是不准确的。
为了简单起见,以便读者可以根据需要进行调试,这是代码的工作版本:
#include <iostream>
#include <ctime>
#define LOOP 1000000 //how many times do I toss a coin
int main()
{
int maxTails = 0;
int maxHeads = 0;
int currentTotalTails = 0;
int currentTotalHeads = 0;
srand(time(NULL));
for (int i = 0; i < LOOP; ++i) //tosses a coin LOOP times
{
if (rand() % 2 == 0)// heads
{
currentTotalHeads++;
currentTotalTails = 0;
if (currentTotalHeads > maxHeads)
maxHeads = currentTotalHeads;
}
else // tails
{
currentTotalTails++;
currentTotalHeads = 0;
if (currentTotalTails > maxTails)
maxTails = currentTotalTails;
}
}
std::cout << "Max Heads in a row: " << maxHeads << std::endl;
std::cout << "Max Tails in a row: " << maxTails << std::endl;
system("pause");
return 0;
}
这可能是随机数生成的问题。 由于所有随机数生成器都使用时钟和自种子设定以来经过的时间量来工作,如果代码的执行每次循环花费的时间大致相同,则会出现模式。 这可以解释为什么它只出现 15。 我在入门课上做了一个石头剪刀布游戏,我可以以 90% 的比率持续获胜(这不是一个非常公平的游戏)。
尝试改变控制流。 有一个随机数生成器来控制发生的迭代次数
loopCnt = 0;
while(loopCnt < LOOP)
{
nextLoops = rand() % (LOOP - loopCnt);
for(int i = 0; i < nextLoops; ++i)
{
// COUNT IN A ROW.
++loopCnt;
}
}
这应该会改变你的流程,模式不会出现。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 最小硬币更换问题(自上而下方法)
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 硬币或CBC求解器性能:命令行实用程序与编译的C 程序
- C++抛硬币程序
- 使用类创建抛硬币程序
- 用C++编写一个程序,模拟反复抛硬币,并持续到连续抛出三个头
- 我需要帮助制作一个可以同时掷 2 个硬币的程序
- C++硬币翻转程序错误
- 硬币翻转程序无法运行
- 计算给定零钱金额的硬币面额的程序
- 用c++编写的抛硬币程序