However, here are other things to appreciate before coding that. Random numbers generated by C++20 follow a sequence. There are many such sequences so random numbers are not truly random. The user of the program will hardly be able to know which sequence the programmer chose and how to determine the next number, when the random function is called, in the same code.
Each sequence has a starting number. The seed is related to the starting number of a sequence. Each sequence depends on the seed and the sequence distribution. The sequence distribution is the profile of the sequence.
This article explains how to fill an array with random numbers beginning with the classes: random_device, default_random_engine, and uniform_int_distribution. These classes are all in the random library which has to be included. The skeleton of a program to fill an array of 10 elements, with random numbers, is as follows:
#include <random>
using namespace std;
int arr[10];
int main()
{
//statements
return 0;
}
Note that any arithmetic type can be used as element type for the array. The size of the array is 10. However, any number of random numbers can be obtained.
Engine and Distribution
In this topic, an engine is a generator of random numbers.
random_device
This is a class from which objects are instantiated. An object from this class is a device and not an engine. This needs a generator in order to be useful. A generator can take a random_device as argument.
default_random_engine
An engine in this topic generates random numbers. There are different engines from which the programmer can choose. This has to be chosen when the programmer is not sure of which engine to choose. This is a class from which objects are instantiated. It takes a random_device object as argument.
uniform_int_distribution
There are many sequences distribution profiles that the programmer can choose from. The one chosen for this article is: uniform_int_distribution. This is a class from which objects can be created. Its construction takes an engine as argument, as well as the lower and upper limit numbers for the random numbers. It is actually a class template. One of its construction syntaxes is:
The following three statements work together:
default_random_engine eng(rd());
uniform_int_distribution<int> dist(4, 13);
From 4 to 13 are ten integers including the lower and upper limits. The template specialization for the distribution object, dist, is int. So ten different random numbers can be chosen from this range, (4 – 13). Note that the argument for eng() is rd() and not rd. Also note that any arithmetic type could be the template specialization for this distribution construction.
From this code, to obtain the next random number, use “dist(eng);” .
Producing Ten Random Integers
The following program, produces ten random integers, from 4 to 13 inclusive.
#include <random>
using namespace std;
int main()
{
random_devicerd;
default_random_engineeng(rd());
uniform_int_distributiondist(4, 13);
cout<<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<endl;
cout<<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<dist(eng) << ' ' <<endl;
return 0;
}
The output from the author’s computer is:
8 12 6 12 8
Some numbers occurred more than once. The program begins with the inclusion of the iostream library for input and output. After that, the random library is included forr random numbers. The next line is a statement and not a directive. It ends with a semicolon. It insists that any name, not preceded with “std::” is of the standard namespace.
Then there is the C++ main function. The first three statements of the main function have been explained, previously. In the next code segment, dist(eng) outputs the next random number; of course, within the range (inclusive), given as arguments to the distribution constructor.
Filling an Array with Random Numbers
In the above code, ten random numbers were produced with the expression, dist(eng). It was typed ten times. It can be typed once, and be called ten times, if done in a for-loop. The for-loop will have to iterate ten times. In this situation, the return random number will not be sent to the terminal (screen); it will be sent to the next element location, of the array. The following program illustrates this:
#include <random>
using namespace std;
int arr[10];
int main()
{
random_devicerd;
default_random_engineeng(rd());
uniform_int_distributiondist(4, 13);
for (int i=0; i<10; i++)
arr[i] = dist(eng);
for (int i=0; i<10; i++)
cout<<arr[i] << ' ';
cout<<endl;
return 0;
}
The output from the author’s computer, this time, is:
Note how the first for-loop was coded. Of course, any range can be chosen, the following program uses a range from 0 to 100:
#include <random>
using namespace std;
int arr[10];
int main()
{
random_devicerd;
default_random_engineeng(rd());
uniform_int_distributiondist(0, 100);
for (int i=0; i<10; i++)
arr[i] = dist(eng);
for (int i=0; i<10; i++)
cout<<arr[i] << ' ';
cout<<endl;
return 0;
}
The output from the author’s computer, this time, is:
Though the range has more than ten integers, only ten random numbers were produced, as decided upon by the first for-loop.
Conclusion
Carry out the following procedure to fill an array with random numbers: generate a random number and put in the array, as the first element. Generate another random number and put in, as the second element. Generate a third random number and put in, as the third element. Continue this way until the number of random numbers required, is reached. The following code segment is important:
random_device rd;
default_random_engine eng(rd());
uniform_int_distribution<int> dist(0, 100);
for (int i=0; i<10; i++)
arr[i] = dist(eng);