Ok so here is the problem that was assigned:
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.
Repeat the above using mt19937()
So I did it with the rand () method as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#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 difference between heads and tales is: " << total / 1000.0 << '\n';
cin.get();
return 0;
}
|
But whenever i try doing it with the mt19937 random number generator method it yeilds vastly different results than above. Example of some of the code i tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, 1);
int heads = 0, tails = 0, total = 0;
for (int h = 0; h < 1000; h++) // Loop Coin Toss
{
{
for (int i = 0; i < 10000; ++i) // COIN TOSS
if (dis(gen) == 1)
++heads;
else
++tails;
total += abs(heads - tails);
}
}
cout << "The average distance between is " << total / 1000.0 << '\n';
cin.get();
return 0;
}
|
Somebody please help me, and go easy on me bc I'm a noob lol, I'm not the smartest at c++ so don't tell me i made a stupid mistake intentionally lol.