1. ## RNG in C++

Well, the following is an exercise that I have to use RNG with it, but I don't have any experience related to such function (RNG), so I can't understand how to relate between the program and the code at the end of the post which generates random numbers, I am kind of confused between the N & M used in the exercise, and those M & n used in the note.
I can not understand exactly the code at the end of the post.
Thank you,

-Amy

Exercise:
-------
Use a random number generator to simulate N throws of a pair of dice and to give the number of times their sums were 7 or 11. Allow the program to input N (e.g. N = 10, 100, 1000) and to repeat the simulation M runs for the same N (e.g. M = 10). For each N, find the average probability of 7 or 11 for the M runs (sum the results for the M runs and divide by M then divide by N).

Note on using the C++ Random Number Generator (RNG):
-----------------------------------------------------
This program uses random numbers generated by a Random Number Generator (RNG). The RNG in C++ is a function rand( ) that returns a random integer from 0 to 32,767. To obtain random integers from 1 through n, use rand( ) % n + 1. To obtain a random sequence you need to first initialise the RNG using the time of the machine as a seed. This is done so that we do not get the same sequence every time we run the program.
The following is an example of how to generate a random sequence of M integers with values (x) between 1 and n :

Code:
```#include <time.h>
int i , x , n , M
srand ( (unsigned) time (NULL) );        //Initialize RNG
for ( i = 1; i <= M; i++)              // Loop over the sequence
{
x = rand( ) % n + 1;            // Generate a number from the sequence
cout << x << endl;            // Print it
}```

2. well in that code, a random number between 1 and n is generated M times...

in the description, it looks like they want you to throw the dice N times. let's use 10 for example. then it says that it wants you to thow the dice that many times for M times. let's use 5 there. so, now you have N as 10 and M as 5. what it's saying is to create a program to simulate N(10) dice throws M(5) times, and find the probability that the sum will either be 7 or 11 on any given throw.

basically, keep the M and N seperate from the M,n,i, and x from the program code. that was probably not a very good choice on your instructors part. the question has some difficult wording as well, but that's something you'll need to get used to in programming classes...

FYI: it's not really a RNG, it's actually more of a PRNG, or a Psuedo Random Number Generator, because the numbers it generates are not truly random, but it'll be good enough for now...

3. Originally Posted by major_small
well in that code, a random number between 1 and n is generated M times...

in the description, it looks like they want you to throw the dice N times. let's use 10 for example. then it says that it wants you to thow the dice that many times for M times. let's use 5 there. so, now you have N as 10 and M as 5. what it's saying is to create a program to simulate N(10) dice throws M(5) times, and find the probability that the sum will either be 7 or 11 on any given throw.

basically, keep the M and N seperate from the M,n,i, and x from the program code. that was probably not a very good choice on your instructors part. the question has some difficult wording as well, but that's something you'll need to get used to in programming classes...

FYI: it's not really a RNG, it's actually more of a PRNG, or a Psuedo Random Number Generator, because the numbers it generates are not truly random, but it'll be good enough for now...
Aha, well, now I will try working on the program and post it once I am done with it to see if it is made the right way, thanks for clarifying.

- Amy

4. So I think I am done with the program and it's working fine, that's the code, if there are any comments please add them here.
Thank you,

-Amy

Code:
```#include <iostream>
#include <time.h>

using namespace std;

int main()
{
int iN, iM, x1, x2, n , N, M;
float counter = 0;
int sum;

n = 6;
N = 10;
M = 5;

for ( iM = 1; iM <= M; iM++ )
{

srand ( (unsigned) time (NULL) );		//Initialize RNG
for ( iN = 1; iN <= N; iN++ ) // Loop over the sequence
{
x1 = rand( ) % n + 1;	// Generate a number from the sequence
cout<<x1<<" ";			// Print it

x2 = rand( ) % n + 1;	// Generate a number from the sequence
cout<<x2<<endl;			// Print it

sum = x1 + x2;

if((sum==7) || (sum==11))
counter += 1;
}

cout<<endl;
}

cout<<"Average Probability: "<<(counter/M)/N;

cin.get();
return 0;
}```

5. looks good to me. I would only make a very few, very slight changes to the code you had:
Code:
```#include <iostream>
#include <ctime>        //slightly more standard

using namespace std;

int main()
{
int iN, iM, x1, x2, n , N, M;
float counter = 0;
int sum;

n = 6;
N = 10;
M = 5;

for ( iM = 1; iM <= M; iM++ )
{

srand ( (unsigned) time (0) );        //I usually try to avoid macros
for ( iN = 1; iN <= N; iN++ )
{
x1 = rand( ) % n + 1;
cout<<rand( ) % n + 1<<" ";

x2 = rand( ) % n + 1;
cout<<x2<<endl;

sum = x1 + x2;

if((sum==7) || (sum==11))
counter++;  //style
}

cout<<endl;
}

cout<<"Average Probability: "<<(((counter/M)/N)*100)<<'%';
//using a percentage instead of a decimal

cin.ignore(1); //style over substance
return 0;
}```
like I said in that last comment, it's all style over substance, and these changes are opinion rather than fact.

good coding.

6. I think you could place the srand() call before the first for loop - you shouldnt need to call it multiple times.

Also, using rand() % n can lead to a slight bias if the range of possible random integers is not congruent to n (i.e. RAND_MAX+1 is not 0 (mod n) )
There are ways to fix this, and you might want to explore them for fun - or for marks if your teacher considers your solution as creative or something.