(N) Random numbers

Goal: I will practice using a random number generator.

Instructions:

Retrieve the code template: templates/02/random_numbers/ -> student/02/random_numbers/.

On random numbers

The library random comprises the class default_random_engine, which is one of the random number generators provided by the class. When you call the constructor of the random number generator class (i.e. create a random number generator object), you can give so called seed value as a parameter. Seed value acts as a starting point for throwing random numbers.

Besides giving a seed value, you must tell, which distribution you want to use. The library random provides several distributions, and we will use one of them called uniform_int_distribution. In addition, we must tell to the distribution the interval, from which the random numbers will be taken.

For example, the code below generates five random numbers from the interval [1, 100] with the seed value 42.

// creating a random number generator with the seed value 42
default_random_engine gen(42);

// creating a distribution
uniform_int_distribution<int> distr(1, 100);

// drawing and printing five random numbers
cout << distr(gen) << endl;
cout << distr(gen) << endl;
cout << distr(gen) << endl;
cout << distr(gen) << endl;
cout << distr(gen) << endl;

Random numbers generated by the above kind of random number generator are so called pseudorandom numbers. It means that the same seed value generates always the same series of random numbers. If you keep the seed value the same (e.g. 42), the above code produces always the same five random numbers. But if you change the seed value, also the series of random numbers will change (since their starting point changes).

Pseudorandom numbers are useful for testing purposes, because they allow you to repeat the same execution for several times. This requires only to know the seed value used.

If you do not give a seed value when creating a random number generator, you can set it later by calling the function seed:

// creating a random number generator without a seed value
default_random_engine gen;

// setting a seed value for a random number generator
gen.seed(42);

A random number generator is created, i.e. a constructor is called also in the above code, even if the first line looks like a usual declaration. (Classes, objects, and constructors will be considered later more precisely.)

Assignment

The code template has the main function that reads lower and upper bounds for the interval, from which the random numbers will be generated. In addition, the main function checks if the given lower bound is strictly less than the upper bound.

Your task is to implement the function produce_random_numbers that produces random numbers for the given interval until the user enters the quit command q.

The program is intended to work as below:

Enter a lower bound: 1
Enter an upper bound: 10
Enter a seed value: 1

Your drawn random number is 1
Press q to quit or any other key to continue: c

Your drawn random number is 2
Press q to quit or any other key to continue: c

Your drawn random number is 8
Press q to quit or any other key to continue: c

Your drawn random number is 5
Press q to quit or any other key to continue: c

Your drawn random number is 6
Press q to quit or any other key to continue: c

Your drawn random number is 3
Press q to quit or any other key to continue: c

Your drawn random number is 1
Press q to quit or any other key to continue: q

Tips for completing the assignment:

  • Commands (c and q) given by the user are better to be read by using the operator >>, since also lower and upper bounds have been read with this operator. (It is not a good idea to mix >> and getline in reading input, especially harmful is the order: first >> and then getline.)

  • Of course, you can try what happens, if you read the commands with getline. The difference between >> and getline is that the former one does not read the line feed, which is given after the actual input. This unread character must be read away from the input stream, if the aim is to later use getline function.

A+ presents the exercise submission form here.