Overview
CRT provides limited capability of generating random numbers based on a seed. In C++ 11 numerous random number engines and distributions were introduced.
Details
The mechanism is a two step process. The first step involves generating uniformly distributed random values between a min and max range by the random value generators. The second step involves using different distribution mechanisms such as uniform, Normal or Binomial to achieve randomness.
Except for random_device, all the random value generators produce pseudo random values, meaning the numbers are generated using a pre determined way. Also seed can be uses to initialize these engines.
seed_seq
Pseudo random numbers are repetitive when used without a seed. A seed is an unique number that can be used. For example, the the current time in nanoseconds since the system start.
The seed_seq class provides a way to seed random number engines or a generator that requires a lot of entropy. It uses a initial seed sequence of integer values to produce a series of unsigned integer values with 32 significant bits that can be used to seed a pseudo-random generator engine.
member types
Following properties are defined.
Name | Definition |
---|---|
uint_least32_t result_type | The type of the numbers generated |
Constructor
Name | Description |
---|---|
|
//1 seed_seq seed; //2 seed_seq seed2 = {21,9,2018}; //3 string foo = "Khrisha Rao"; seed_seq seed3 (foo.begin(),foo.end()); |
Methods
Name | Description |
---|---|
void generate (RandomAccessIterator first, RandomAccesIterator last) | Populates the output range [first, last) with 32-bit unsigned integer values that are seeds, based on the initial seed sequence. Example seed_seq seed {21,9,2018};
unsigned seeds[5];
seed.generate(seeds,seeds+5);
//prints 565048846 -2000421301 2106023878 263474872 1635843284 copy ( seeds,seeds+5, ostream_iterator<unsigned> (cout," ") ); |
size_t size() | Returns the size of the initial seed sequence. Example seed_seq seed {21,9,2018}; |
void param (OutputIterator dest) | Copies the internal sequence into dest. Example seed_seq seed {21,9,2018};//prints 21 9 2018 seed.param(ostream_iterator<unsigned> (cout," ") ); |
Multiple classes are provided to generate pseudo random numbers using mathematical functions. Also a truly random device is also provided.
generate_canonical()
Generates a random floating point number in range [0, 1) using random number engines.
It's light weight compared to distributions.
Syntax
//RealType:A floating-point type. //n:Produce random numbers with n bits of randomness //URNG:A uniform random number generator class. template <class RealType, size_t n, class URNG> RealType generate_canonical (URNG& g)
:
random_device rd; mt19937 gen(rd()); //prints 0.335205 0.10016 0.721013 for (int n = 0; n < 3; ++n) cout << generate_canonical<double, 4>(gen) << ' ';
Multiple classes are provided to generate pseudo random numbers sequences. These classes use a pseudo random number engine to generate numbers and later distribute them into sequences using mathematical functions.
No comments:
Post a Comment