1. The random number is generated by the generator and the distributor to produce the generator generator: the ability to produce discrete and other potential distribution of the numerical distributor distributions: the ability to map the generator generated by the uniform distribution of the value of other common distributions, such as uniform distribution uniform, Normal distribution Normal, two-item distribution binomial, Poisson distribution Poisson2. The Distributor uses the operator () to generate a random number, passing in a generator object as a parameter
std::d efault_random_engine Generator; Std::uniform_int_distribution<int> Dis (0,+); for (int i=0;i<5; i++) { std::cout<<dis (generator) <<Std::endl; }
If it is troublesome to pass the generator object every call, you can use Std::bind to include the header file Functionalauto dice = Std::bind (distribution,generator) and you can call dice immediately after () a random number that produces a compound uniform distribution. However, multiple runs of the regular session found the same sequence of random numbers generated each time, because there is no seeding (the rand and Srand relationships in the Cstdlib library)
std::d efault_random_engine Generator; Std::uniform_int_distribution<int> Dis (0,+); Auto Dice= std::bind (dis,generator); for (int i=0;i<5; i++) { std::cout<<dice () <<Std::endl; }
3. Seed in addition to the Random_device generator (true random number generator or called F nondeterministic random number Generator) (valid in Linux, Windows is actually pseudo-random), all the random number engines defined in the library are pseudo-random number generators, they all take advantage of the specific algorithm implementation, These generators all need a seed. The seed can be a numeric value, or an object with a generate member function. In a simple application, use time as a seed. Note: If you do not set the seed, then the resulting sequence of random numbers will be the same every time, such as the code, generating 5 random numbers from 1 to 6, but each time is 82 13 91 84 12 Instead of the following code, you can make a sequence of random numbers of each generation different:
std::d efault_random_engine Generator (Time (NULL)); Std::uniform_int_distribution<int> Dis (0,+); Auto Dice= std::bind (dis,generator); for (int i=0;i<5; i++) { std::cout<<dice () <<Std::endl; }
4. About the generator C++11 standard provides three generator template classes can be instantiated as generators, but need to have a certain degree of mathematics to understand the meaning of each template parameter, you can refer to the source of the algorithm paper. These three generator class templates are:
Linear_congruential_engine Linear Same as congruential mersenne_twister_engine Mason rotation substract_with_carry_engine hysteresis FIBONACC I
Examples of linear and congruential
Template <class Uinttype, uinttype A, Uinttype C, Uinttype m>
Class Linear_congruential_engine;
First parameter: Generator type unsigned int,unsigned long, etc.
The second to the fourth parameter: is the linear same Yufa recursive formula Nj+i = (axnj+c) (mod M) in the three constant value a,c,m
Requirement: If M is not 0,a,c the value is less than M
A common generator that will be introduced:
Long 48271 0 2147483647> minstd_rand; typedef linear_congruentiallong1680702147483647> minstd_rand0;
It can be seen that if you instantiate the template class is cumbersome, need a strong knowledge of the sequence, so there are several commonly used template instantiation generator, they all need a seed parameter to be able: 4.1 linear with congruential:
Minstd_rand () minstd_rand0 uses the linear same congruential Knuth_b minstd_rand0 with Shuffle_order_engine after adapter variants
4.2 Mason Rotation Method:
default_random_engine () mt19937mt19937_64
4.3 hysteresis Fibonacci method
Ranlux24_baseranlux48_base
Hysteresis Fibonacci method After using adapter variant:
ranlux24 ranlux24_base with discard_block_engineranlux48 ranlux48_base with Discard_block_ Engine
Three adapters: Discard_block_engine shuffle_order_engine independent_bits_engine5. With respect to the Distributor, if only the generator with seed can produce discrete may be distributed, resulting in a number between generator min and Max, and the result is a value of uinttype. There is no good control over the distribution interval and probability of distribution of the resulting numerical values. If this function is to be implemented, the Distributor will be used.
role 1: Change the generation type, using the template parameter 2: Change the value interval, using the instance constructor parameters. or its response member function to set parameters. Role 3: Change the probability distribution and choose different distributor types
5.1 Evenly distributed:
uniform_int_distribution integer evenly distributed uniform_real_distribution floating point distribution
5.2 Bernoulli type distribution: (only yes/no two results, probability of one P, one 1-p)
geometry_distribution geometric distribution of bernoulli_distribution Bernoulli distribution binomial_distribution Two-item distribution NEGATIVE_BI Omial_distribution negative two-item distribution
5.3 rate-based Distributions:
Distribution of
exponential_distribution exponential distribution of poisson_distribution Poisson distributions gamma_distribution Gamma distribution Weibull_distribution Extreme_value_distribution extremum Distribution of Weibull distribution
5.4 Normal Distribution Related:
normal_distribution Normal distribution chi_squared_distribution chi-square distribution cauchy_distribution Cauchy distributions fisher_f_distr Ibution Fisher F distribution student_t_distribution t distribution
5.5 Segmented distribution Related:
discrete_distribution Discrete distribution piecewise_constant_distribution piecewise constant distribution piecewise_linear_distribution piecewise linear distribution
"C++11" random number function library random