Professional C__ - Marc Gregoire [286]
exponential_distribution(RealType lambda = 1.0);
template gamma_distribution(RealType alpha = 1.0, RealType beta = 1.0); template weibull_distribution(RealType a = 1.0, RealType b = 1.0); template extreme_value_distribution(RealType a = 0.0, RealType b = 1.0); Normal distributions: template normal_distribution(RealType mean = 0.0, RealType stddev = 1.0); template lognormal_distribution(RealType m = 0.0, RealType s = 1.0); template chi_squared_distribution(RealType n = 1); template cauchy_distribution(RealType a = 0.0, RealType b = 1.0); template fisher_f_distribution(RealType m = 1, RealType n = 1); template student_t_distribution(RealType n = 1); Sampling distributions: template discrete_distribution(initializer_list template template piecewise_constant_distribution(initializer_list UnaryOperation fw); template template piecewise_linear_distribution(initializer_list UnaryOperation fw); Each distribution requires a set of parameters. Explaining all these mathematical parameters in detail is outside the scope of this book, but the rest of this section gives a couple of examples to explain the impact of a distribution on the generated random numbers. Distributions are easiest to understand when you look at a graphical representation of them. For example, the following code generates one million random numbers between 1 and 99 and counts how many times a certain number between 1 and 99 is randomly chosen. This is stored in a map where the key is a number between 1 and 99, and the value associated with a key is the number of times that that key has been selected randomly. After the loop, the results are written to a CSV (Comma Separated Values) file, which can be opened in a spreadsheet application to generate a graphical representation: const unsigned int DIST_START = 1; const unsigned int DIST_END = 99; const unsigned int ITERATIONS = 1000000; // Uniform Mersenne Twister mt19937 eng(static_cast uniform_int_distribution auto gen = bind(dist, eng); map for (unsigned int i = 0; i < ITERATIONS; ++i) { int rnd = gen(); // Search map for a key = rnd. If found, add 1 to the value associated // with that key. If not found, add the key to the map with value 1. ++(m[rnd]); } // Write to a CSV file ofstream of("res.csv"); for (unsigned int i = DIST_START; i <= DIST_END; ++i) { of << i << ","; auto res = m.find(i); of << (res != m.end() ? res->second : 0) << endl; } Code snippet from Random\uniform_int_distribution.cpp The resulting data can then be used to generate a graphical representation. The graph of the preceding uniform Mersenne twister is shown in Figure 16-1. FIGURE 16-1 The horizontal axis represents the range in which random numbers are generated. The graph clearly shows that all numbers between 1 and 99 are randomly chosen around 10,000 times and that the distribution of the generated random numbers is uniform across the entire range. The example can be modified to generate random numbers according to a normal distribution instead of a uniform distribution. Only two small changes are required. First, the creation of the distribution is modified as follows: normal_distribution Because normal distributions use