Accumulator to average coin tosses Newer Method (MT19937)

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.
Neither of them is correct.

You need to reset heads and tails to zero within (not before) each outer loop.

You've also got a redundant bracket pair { } in each code.


With rand():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
   int total = 0;
   srand( time( 0 ) );

   for (int h = 0; h < 1000; h++ ) // Loop Coin Toss
   {
      int heads = 0, tails = 0;
      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';
}



With mt19937:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>
#include <random>
using namespace std;

int main()
{
   int total = 0;
   mt19937 gen( time( 0 ) );

   for (int h = 0; h < 1000; h++ ) // Loop Coin Toss
   {
      int heads = 0, tails = 0;
      for ( int i = 0; i < 10000; ++i ) // COIN TOSS
      {
         if ( gen() % 2 == 0 ) ++heads;
         else                  ++tails;
      }
      total += abs( heads - tails );
   }
   cout << "The average difference between heads and tales is: " << total / 1000.0 << '\n';
}



Despite the people who hammer rand() I don't think you will find much difference between the two results.
Last edited on
Thank You!!!
For the large numbers in each experiment you should get an answer of about
4 * 50 / sqrt( 2.pi )
which is 79.79

Seems to work.
Topic archived. No new replies allowed.