Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you copy a random-number engine and random-number distribution?

Tags:

c++

c++11

In this code:

std::default_random_engine e;
std::default_random_engine e2 = e;                        //1
std::default_random_engine e3(e2);                        //2
std::default_random_engine e4(std::move(e3));             //3

std::uniform_real_distribution<double> d(0,1);
std::uniform_real_distribution<double> d2 = d;            //4
std::uniform_real_distribution<double> d3(d2);            //5
std::uniform_real_distribution<double> d4(std::move(d3)); //6

Can you explain what exactly happens in each of the cases 1 to 6? I don't see the copy assignment, copy construction, and move construction for these classes documented anywhere.

In particular: When we create a new engine/distribution from another, does it reset or continue where it left off?

like image 751
7cows Avatar asked Jun 04 '13 12:06

7cows


1 Answers

When you copy an engine or distribution, it is guaranteed that both engines (or distributions) will generate the same sequence of values from that point on.

For engines, the standard requires that E(x) will produce an engine that compares equal to x (26.5.1.4, table 117). Engines compare equal if the infinite sequence of future calls to the engine will produce the same values (26.5.1.4).

For distributions, they are required (26.5.1.6p4) to satisfy the requirements of CopyConstructible (table 21). This requires that the created object will be equivalent to the original. The requirement for distributions comparing as equal (26.5.1.6, table 118) is that they have the same params, and the future sequence of values generated (if using equivalent engines) is the same.

like image 173
interjay Avatar answered Sep 26 '22 23:09

interjay