Online Book Reader

Home Category

Professional C__ - Marc Gregoire [284]

By Root 1363 0
This is an example of the adapter pattern as described in Chapter 29. The following three adapter templates are defined:

template class

discard_block_engine {...}

template class

independent_bits_engine {...}

template class

shuffle_order_engine {...}

The discard_block_engine adapter generates random numbers by discarding some of the values generated by its base engine. It requires three parameters: the engine to adapt, the block size p, and the used block size r. The base engine is used to generate p random numbers. The adapter then discards p-r of those numbers and returns the remaining r numbers.

The independent_bits_engine adapter generates random numbers with a given number of bits w by combining several random numbers generated by the base engine.

The shuffle_order_engine adapter generates the same random numbers that are generated by the base engine, but delivers them in a different order.

The exact working of these adapters depends on mathematics and falls outside the scope of this book.

The standard includes a number of predefined engine adapters. The following section lists the predefined engines and engine adapters.

Predefined Engines and Engine Adapters

As mentioned earlier, it is not recommended to specify your own parameters for pseudo-random number engines or engine adapters, but instead to use one of the standard ones. C++11 defines the following predefined engines and engine adapters, all in the header file:

typedef linear_congruential_engine

minstd_rand0;

typedef linear_congruential_engine

minstd_rand;

typedef mersenne_twister_engine0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18,

1812433253> mt19937;

typedef mersenne_twister_engine0xb5026f5aa96619e9, 29, 0x5555555555555555, 17, 0x71d67fffeda60000, 37,

0xfff7eee000000000, 43, 6364136223846793005> mt19937_64;

typedef subtract_with_carry_engine

ranlux24_base;

typedef subtract_with_carry_engine ranlux48_base;

typedef discard_block_engine ranlux24;

typedef discard_block_engine ranlux48;

typedef shuffle_order_engine knuth_b;

typedef implementation-defined default_random_engine;

The default_random_engine is compiler dependent.

The following section gives an example of how to use these predefined engines.

Generating Random Numbers

Before you can generate any random number, you first need to create an instance of an engine. If you use a software-based engine, you will also need to define a distribution. A distribution is a mathematical formula describing how numbers are distributed within a certain range. The easiest way to create an engine is to use one of the predefined engines as discussed earlier.

The following example uses the predefined engine called mt19937, using a Mersenne twister engine. This is a software-based generator. Just as with the old srand()/rand() generator, a software-based engine should be initialized with a seed. In this example, the seed is the current system time, which is passed to the constructor of the mt19937 engine:

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

Code snippet from Random\basic.cpp

Next, a distribution is defined. This example uses a uniform integer distribution, for the range 1 to 99. Distributions are explained in detail in the next section, but the uniform distribution is easy enough to use for this example:

uniform_int_distribution dist(1, 99);

Code snippet from Random\basic.cpp

Once the engine and distribution are defined, random numbers can be generated by calling a function whose name is the name of the distribution and passing as a parameter the engine. For this example this is written as dist(eng):

cout << dist(eng) << endl;

Code snippet from Random\basic.cpp

As you can

Return Main Page Previous Page Next Page

®Online Book Reader