"C++11" random number function library Random__jquery

Source: Internet
Author: User
Tags generator generator generator numeric value
1. Random numbers are generated by the combination of generator and distributorGenerator generator: Can produce discrete and other possible distribution of numerical distribution distributions: Can map the generator generated uniform distribution value to other common distributions, such as uniform distribution uniform, normal distribution normal, two distribution binomial , Poisson distribution Poisson
2. The Distributor uses the operator () to produce random numbers, passing in a generator object as a parameter
	std::d efault_random_engine Generator;
	std::uniform_int_distribution<int> dis (0,100);
	for (int i=0;i<5;i++)
	{
		Std::cout<<dis (generator) <<std::endl;
	}

If you want to pass each call to the generator object trouble, you can use Std::bind, to include the header file functional auto dice = std::bind (distribution,generator) can be called directly after dice () a random number that produces a uniformly distributed compound. However, multiple runs of the previous meeting found that the sequence of random numbers produced each time was the same, because no seed was set (the relationship with Rand and Srand in the Cstdlib library)
	std::d efault_random_engine Generator;
	std::uniform_int_distribution<int> dis (0,100);
	Auto dice= std::bind (dis,generator);
	for (int i=0;i<5;i++)
	{
		std::cout<<dice () <<std::endl;
	}
3. SeedsIn addition to the Random_device generator (true random number generator or F non-deterministic random number Generator) (valid in Linux, Windows is also pseudo random), all the random number engines defined in the library are pseudo-random number generators, they all use a specific algorithm to achieve, These generators require a seed. The seed can be a numeric value, or it may be 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 random number sequence is the same every time, such as the code, resulting in 5 1 to 6 of the random number, but each time is 82, 13 91 84 12 changed to the following code, you can make each generation of random number sequence different:
        std::d efault_random_engine Generator (Time (NULL));
	std::uniform_int_distribution<int> dis (0,100);
	Auto dice= std::bind (dis,generator);
	for (int i=0;i<5;i++)
	{
		std::cout<<dice () <<std::endl;
	}
4. About GeneratorsC++11 standard provides three generator template classes can be instantiated into the generator, but need to have a certain mathematical skills to understand the meaning of each template parameters, can refer to the source of the paper. The three generator class templates are: Linear_congruential_engine linear with congruential mersenne_twister_engine-substract_with_carry_engine hysteresis Fibonacci Linear and congruential examples
Template <class Uinttype, uinttype A, Uinttype C, Uinttype m>
Class Linear_congruential_engine;
First parameter: Generator type unsigned int,unsigned long
Second to fourth parameter: is the linear same Yufa recurrence formula Nj+i = (axnj+c) (mod M) in the three constant values A,c,m
Requirement: If M is not 0,a,c value is less than M
As a common generator to be introduced:
typedef linear_congruential<unsigned Long, 16807, 0, 2147483647> minstd_rand0;
typedef linear_congruential<unsigned Long, 48271, 0, 2147483647> Minstd_rand;
It can be seen that if you instantiate the template class very cumbersome, you need a strong knowledge of the sequence, so there are several commonly used template instantiation generator, they all need a seed parameter to: 4.1 linear with congruential:Minstd_rand () minstd_rand0 the linear congruential knuth_b minstd_rand0 with Shuffle_order_engine using adapter variants 4.2 Mason Rotary Method:Default_random_engine () mt19937 mt19937_64 4.3 hysteresis Fibonacci methodRanlux24_base Ranlux48_base
Lag Fibonacci method with adapter variants: ranlux24 ranlux24_base with discard_block_engine ranlux48 ranlux48_base With discard_block_engine three adapters: Discard_block_engine shuffle_order_engine independent_bits_engine

5. About Distributing deviceIt is easy to know that if only generator with seed can produce discrete and so possible distributions, the resulting value is between generator min and Max, and the result is uinttype value. The distribution range and probability of distribution are not well controlled. If you want to implement this function, you need to use the Distributor. Function 1: Change the generation type, use the template parameter function 2: Change the value range, use the instance constructor parameter. or the member function whose response you set the parameter to. Function 3: Change the probability distribution, choose different type of distributor 5.1 Evenly distributed:Uniform distribution of uniform_real_distribution floating-point numbers with uniformly distributed uniform_int_distribution integers 5.2 Bernoulli type distribution: (only yes/no two results, probability of a p, a 1-p)Bernoulli_distribution Bernoulli distribution binomial_distribution Two-item distribution geometry_distribution geometrical distribution negative_biomial_distribut Ion negative two-item distribution 5.3 rate-based Distributions:Distribution of exponential_distribution exponential distribution of poisson_distribution Poisson distribution gamma_distribution weibull_distribution Weibull distribution Extreme_ Value_distribution Extreme Value Distribution 5.4 Normal distribution Related:Chi_squared_distribution distribution cauchy_distribution Cauchy distribution of normal_distribution normal distribution fisher_f_distribution Fischer F distribution student_t_distribution t distribution 5.5 segment Distribution Related:Discrete_distribution discrete distribution piecewise_constant_distribution piecewise constant distribution piecewise_linear_distribution piecewise linear distribution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.