(E) Random numbers¶
Goal: I will practice using a random number generator.
Instructions:
Retrieve the code template: templates/03/random_numbers/
->
student/03/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.
If you do not give a seed value at this phase, you can give it later by
calling the function seed
with a desired value as a parameter.
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.
default_random_engine gen(42);
uniform_int_distribution<int> distr(1, 100);
std::cout << distr(gen) << std::endl;
std::cout << distr(gen) << std::endl;
std::cout << distr(gen) << std::endl;
std::cout << distr(gen) << std::endl;
std::cout << distr(gen) << std::endl;
(Also the waterdrop game on the next round uses random numbers. The material concerning it describes the topic more precisely, but the introduction given above is sufficient for this assignment.)
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 c to continue or q to quit: c
Your drawn random number is 2
Press c to continue or q to quit: c
Your drawn random number is 8
Press c to continue or q to quit: c
Your drawn random number is 5
Press c to continue or q to quit: c
Your drawn random number is 6
Press c to continue or q to quit: c
Your drawn random number is 3
Press c to continue or q to quit: c
Your drawn random number is 1
Press c to continue or q to quit: q
Tips for completing the assignment:
- Commands (
c
andq
) 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>>
andgetline
in reading input, especially harmful is the order: first>>
and thengetline
.) - Of course, you can try what happens, if you read the commands with
getline
. The difference between>>
andgetline
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 usegetline
function.
A+ presents the exercise submission form here.