Online Book Reader

Home Category

Professional C__ - Marc Gregoire [282]

By Root 1164 0
associated steady clock

time_point tp1;

// Add 10 minutes to the time_point

tp1 += minutes(10);

// Store the duration between epoch and time_point

auto d1 = tp1.time_since_epoch();

// Convert the duration to seconds and write to console

duration d2(d1);

cout << d2.count() << " seconds" << endl;

Code snippet from Chrono\time_point.cpp

The output should be:

600 seconds

RANDOM NUMBER GENERATION


Generating good random numbers in software is a complex topic. Before C++11, the only way to generate random numbers was to use the C-style srand() and rand() functions. The srand() function needs to be called once in your application and is used to initialize the random number generator, also called seeding. Usually the current system time would be used as a seed.

You need to make sure that you use a good quality seed for your software-based random number generator. If you initialize the random number generator with the same seed every time, you will create the same sequence of random numbers every time. This is why the seed is usually the current system time.

Once the generator is initialized, random numbers can be generated by using rand(). The following example shows how to use srand(), to initialize the generator with the current system time as the seed, and rand(), to generate a random number. The time(nullptr) call returns the system time, and is defined in the header file:

srand(static_cast(time(nullptr)));

cout << rand() << endl;

Code snippet from Random\old.cpp

A random number within a certain range can be generated by using the following function:

int getRandom(int min, int max)

{

return (rand() % static_cast(max + 1 - min)) + min;

}

Code snippet from Random\old.cpp

The old C-style rand() function generates random numbers in the range 0 to RAND_MAX, which is defined by the standard to be at least 32767. Unfortunately, the low-order bits of rand() are often not very random, which means, using the previous getRandom() number to generate a random number in a small range, such as 1 to 6, will not result in very good randomness.

Software-based random number generators can never generate truly random numbers and are therefore called pseudo-random number generators because they rely on mathematical formulas to give the impression of randomness.

The old srand() and rand() functions don’t offer much in terms of flexibility. You cannot, for example, change the distribution of the generated random numbers. C++11 adds a powerful library to generate random numbers by using different algorithms and distributions. The library is defined in the header file. The library has three big components: engines, engine adapters, and distributions. A random number engine is responsible for generating the actual random numbers and storing the state for generating subsequent random numbers. The distribution determines the range of the generated random numbers and how they are mathematically distributed within that range. A random number engine adapter modifies the results of a random number engine you associate it with.

Random Number Engines

C++11 defines the following random number engine templates:

random_device

linear_congruential_engine

Mersenne_twister_engine

subtract_with_carry_engine

The random_device engine is not a software-based generator; it is a special engine that requires a piece of hardware attached to your computer that generates truly non-deterministic random numbers, for example by using the laws of physics. A classic mechanism measures the decay of a radioactive isotope by counting alpha-particles-per-time-interval or something like that, but there are many other kinds of physics-based random-number generators, including measuring the “noise” of reverse-biased diodes (thus eliminating the concerns about radioactive sources in your computer). The details of these mechanisms fall outside the scope of this book.

According to the specification for random_device, if no such device is attached to the computer, the library is

Return Main Page Previous Page Next Page

®Online Book Reader