3.1 Sequential Random Number Generation with trng¶
Let’s first examine how we can use the trng C++ library to create a sequential version of code (running on one process) that uses a loop to generate one value after another in a stream.
This is the main program that sets up the use of the random number generator and uses a uniform distribution of integers from 1 to 99 for the values in the stream. Study it carefully to see how we use trng to do this.
There are several points to notice about this code:
We declare both a generator object (randGen, line 44) and a type of distribution of the numbers (uniform, line 46). There are other types of distributions of numbers that are useful in many applications, but we will not cover those here.
The syntax of the declaration of the distribution requires that we give it a minimum and maximum value for the range of numbers. The range includes the minimum, but not the maximum.
A seed value is declared as a long unsigned int.
Before requesting numbers from the stream, we first seed the generator (line 49).
To get the next number inside the loop, we call the constructor for the object called uniform and send it the generator object (line 61). Since we declared nextRandValue an int, the use of uniform(randgen) will return an int. This is a nice feature of this C++ library.
If you run it without any changes, it is running with these command line arguments:
-c means use a constant seed. From this if you repeat a run you should get the same stream of numbers. This shows that this generator is reproducible, which you will see is very handy when debugging your code.
-n 8 indicates to generate 8 numbers (repeat the loop 8 times).
You should try it without the -c and run it a few more times to see that a new set of values is generated each time when we seed with a different number each time.
If you are interested in the code for handling these arguments for this sequential version, you could study the following code block. It is included with the code above when it is run, and not intended to be run on its own.
