Online Book Reader

Home Category

Professional C__ - Marc Gregoire [286]

By Root 1122 0
class exponential_distribution

exponential_distribution(RealType lambda = 1.0);

template class gamma_distribution

gamma_distribution(RealType alpha = 1.0, RealType beta = 1.0);

template class weibull_distribution

weibull_distribution(RealType a = 1.0, RealType b = 1.0);

template class extreme_value_distribution

extreme_value_distribution(RealType a = 0.0, RealType b = 1.0);

Normal distributions:

template class normal_distribution

normal_distribution(RealType mean = 0.0, RealType stddev = 1.0);

template class lognormal_distribution

lognormal_distribution(RealType m = 0.0, RealType s = 1.0);

template class chi_squared_distribution

chi_squared_distribution(RealType n = 1);

template class cauchy_distribution

cauchy_distribution(RealType a = 0.0, RealType b = 1.0);

template class fisher_f_distribution

fisher_f_distribution(RealType m = 1, RealType n = 1);

template class student_t_distribution

student_t_distribution(RealType n = 1);

Sampling distributions:

template class discrete_distribution

discrete_distribution(initializer_list wl);

template class piecewise_constant_distribution

template

piecewise_constant_distribution(initializer_list bl,

UnaryOperation fw);

template class piecewise_linear_distribution

template

piecewise_linear_distribution(initializer_list bl,

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(time(nullptr)));

uniform_int_distribution dist(DIST_START, DIST_END);

auto gen = bind(dist, eng);

map m;

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 dist(50, 10);

Because normal distributions use

Return Main Page Previous Page Next Page

®Online Book Reader