简体   繁体   中英

Averaging Coin Tosses with Accumulator C++

This is the problem I am working with

Using a loop and rand(), simulate a coin toss 10000 times Calculate the difference between heads and tails. Wrap the above two lines in another loop, which loops 1000 times. Use an accumulator to sum up the differences Calculate and display the average difference between the number of heads and tails.

The accumulator is not working the way I want It to? Very much a C++ Noob, for homework lol. Anyone help please?

Why am I using rand()???? second part of the assignment has us using the newer method (mt19937), just trying to tackle this bit first before moving on.

#include <iostream>
using namespace std;
int main() {


int heads = 0, tails = 0, num, total = 0;

srand(time(NULL));

for (int h = 0; h < 1000; h++) // Loop Coin Toss
{
    for (int i = 0; i < 10000; i++) // COIN TOSS
    {

            int random = rand() % 2;

        if (random == 0)
        {
        heads++;
        }
        else 
        {
        tails++;
        }

    }

    cout << abs((heads++ - tails++));
    cin >> num;
    total =+ num;

}
cout << "The average distance between is " << total / 1000 << endl;

cin.get();
return 0;
}

With your code, you never actually save the values that you need. And there's some unnecessary arithmetic that would throw off your results. This line:
cout << abs((heads++ - tails++)); increments the heads and tails variables, but they shouldn't be.

The next two lines make no sense. Why do you need to get a number from the user, and why do you add that number to your total?

Finally, this expression: total / 1000 performs integer division, which will throw off your results.

Those are the immediate issues I can spot in your code.

Next, we move on to your problem statement. What is an accumulator? To me, it sounds like you're supposed to have a class? It also reminds me of std::accumulate , but if that's what you intended, it would have said as much. Also, std::accumulate would require storing results, and that's not really necessary for this program. The code below performs the main task, ie it runs the necessary simulations and tracks results.

You'll notice I don't bother counting tails. The big average is also calculated as it goes since the total number of simulations is known ahead of time.

#include <cmath>
#include <iostream>
#include <random>

int flip_coin() {
  static std::mt19937 prng(std::random_device{}());
  static std::uniform_int_distribution<int> flip(0, 1);

  return flip(prng);
}

int main() {
  constexpr int tosses = 10'000;
  constexpr int simulations = 1'000;
  double diffAvg = 0.0;

  for (int i = 0; i < simulations; ++i) {
    int heads = 0;
    for (int j = 0; j < tosses; ++j) {
      if (flip_coin()) {
        ++heads;
      }
    }
    diffAvg +=
        std::abs(heads - (tosses - heads)) / static_cast<double>(simulations);
  }

  std::cout << "The average heads/tails diff is: " << diffAvg << '\n';

  return 0;
}

What I ended up doing that seems to work for **THIS VERSION WITH RAND() (using the new method later) **

#include <iostream>
using namespace std;

int main() 
{
int heads = 0, tails = 0, total = 0;

srand(time(NULL));

for (int h = 0; h < 1000; h++) // Loop Coin Toss
{
    {
        for (int i = 0; i < 10000; ++i) // COIN TOSS
            if (rand() % 2 == 0)
                ++heads;
            else
                ++tails;

        total += abs(heads - tails);
    }
}
cout << "The average distance between is " << total / 1000.0 << '\n';

cin.get();
return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM