C++抛硬币程序

C++ coin toss program

本文关键字:程序 硬币 C++      更新时间:2023-10-16

我试图制作一个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;
    }
}

这应该会改变你的流程,模式不会出现。