warozell
warozell

Reputation: 69

C++ coin toss program

I tried to make a C++ program to calculate how many coin tosses of the same type I've gotten in a row, but the results are strange. I tried to toss it 1,000,000 times and I haven't gotten more "in a row tosses of the same type" than 15. I tested it on smaller number of tosses (5-20) and it seemed to be working correctly except I also haven't gotten more than 15 of the same type in a row.

#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;
}

I've read on the internet, that there is a ~38% chance of getting 20 heads in a row if you toss a coin 1,000,000 times but my program works differently. Is there a problem in my code or is it maybe some problem with the rand() function?

Upvotes: 0

Views: 4429

Answers (2)

This could be a problem with your random number generation. Since all random number generators work using the clock and the amount of time that has passed since it was seeded, if the execution of your code takes about the same amount of time each loop, then patterns will emerge. This would explain why it only comes up as 15. I had made a rock paper scissors game in my intro class that I could win consistently at a 90% rate (not a very fair game).

Try varying your control flow. Have a random number generator that will control how many iterations take place

loopCnt = 0;
while(loopCnt < LOOP)
{
    nextLoops = rand() % (LOOP - loopCnt);
    for(int i = 0; i < nextLoops; ++i)
    {
        // COUNT IN A ROW.
        ++loopCnt;
    }
}

That should vary up your flow that patterns won't emerge.

Upvotes: 0

Khalil Khalaf
Khalil Khalaf

Reputation: 9407

Read @BathSheba comment. It is just not accurate to expect different output than what you are getting now.

For the sake of simplicity so readers can debug if desired, this is a working version of your code:

#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;
}

Upvotes: 1

Related Questions