Online Book Reader

Home Category

Professional C__ - Marc Gregoire [285]

By Root 1210 0
see, to generate a random number by using a software-based engine, you always need to specify the engine and distribution. The std::bind() utility introduced in Chapter 13 and defined in the header file can be used to remove the need to specify both the distribution and the engine when generating a random number. The following example uses the same mt19937 engine and uniform distribution as the previous example. It then defines gen() by using std::bind() to bind eng to the first parameter of dist(). This way, you can call gen() without any argument to generate a new random number. The auto keyword is used in the definition of gen() to avoid having to write the exact type ourselves. The example then demonstrates the use of gen() in combination with the generate() algorithm to fill a vector of 10 elements with random numbers. The generate() algorithm is discussed in Chapter 13 and is defined in :

mt19937 eng(static_cast(time(nullptr)));

uniform_int_distribution dist(1, 99);

auto gen = std::bind(dist, eng);

vector vec(10);

generate(vec.begin(), vec.end(), gen);

for (auto i : vec)

cout << i << " ";

Code snippet from Random\generate.cpp

Remember that the generate() algorithm overwrites existing elements and will not insert new elements. This means that you first need to size the vector to hold the number of elements you need, and then call the generate() algorithm. The previous example sizes the vector by specifying the size as argument to the constructor.

Even though you don’t know the exact type of gen(), because of the use of the auto keyword, it’s still possible to pass gen() to another function that wants to use that generator. However, you cannot use a normal function; you need to use a template function. The previous example can be adapted to do the generation of random numbers in a function called fillVector(), which is a template function that looks as follows:

template

void fillVector(vector& vec, T rndGen)

{

generate(vec.begin(), vec.end(), rndGen);

}

Code snippet from Random\generate_function.cpp

The previous example can then be adapted as follows:

mt19937 eng(static_cast(time(nullptr)));

uniform_int_distribution dist(1, 99);

auto gen = std::bind(dist, eng);

vector vec(10);

fillVector(vec, gen);

for (auto i : vec)

cout << i << " ";

Code snippet from Random\generate_function.cpp

Random Number Distributions

A distribution is a mathematical formula describing how numbers are distributed within a certain range. The C++11 random number generator library comes with the following distributions that can be used with pseudo-random number engines to define the distribution of the generated random numbers. It’s a compacted representation. The first line of each distribution is the class name and class template parameters, if any. The next lines are a constructor for the distribution. Only one constructor for each distribution is shown to give you an idea of the class. Consult the Standard Library Reference resource on the website (www.wrox.com) for a detailed list of all constructors and methods of each distribution.

Uniform distributions:

template class uniform_int_distribution

uniform_int_distribution(IntType a = 0,

IntType b = numeric_limits::max());

template class uniform_real_distribution

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

Bernoulli distributions:

class bernoulli_distribution

bernoulli_distribution(double p = 0.5);

template class binomial_distribution

binomial_distribution(IntType t = 1, double p = 0.5);

template class geometric_distribution

geometric_distribution(double p = 0.5);

template class negative_binomial_distribution

negative_binomial_distribution(IntType k = 1, double p = 0.5);

Poisson distributions:

template class poisson_distribution

poisson_distribution(double mean = 1.0);

template

Return Main Page Previous Page Next Page

®Online Book Reader