BDE 4.14.0 Production release
|
Provide a suite of procedures for random-number generation.
This component provides a utility struct
, bdlb::Random
, that is a namespace for a suite of functions used to efficiently generate random numbers over a specific range of values. The seed (or current state) is maintained externally. Two variants of a 15-bit random number generator are provided: one has a single [in/out] seed parameter, which is first used then updated; the other takes the current seed as an [input] parameter, and stores a new seed in an [output] parameter. A third generator produces 32-bit random numbers employing the PCG algorithm.
This section illustrates intended use of this component.
This example shows how one might use bdlb::Random
to create and use a class to simulate the roll of a single die in a game, such as craps, that uses dice.
First, we define the Die
class itself:
Now, we can use our Dice
class to get the random numbers needed to simulate a game of craps. Note that the game of craps requires two dice.
We can instantiate a single Die
and role it twice,
Alternatively, we could create two instances of Die
, with separate initial seeds, and role each one once:
Note that the specification of separate seeds is important to produce a proper distribution for our game. If we had shared the seed value each die would always produce the same sequence of values as the other.