0

I wrote a cpp class that generates a random process. I defined the random engine as a public attribute of my class.

My question is : what happens with the engine when I use the operator= :

Proc A; 
// operations on A 
Proc B;
B=A;

will it create a new engine for B initialised randomly ? Or will it create a engine that will generate pseudo-random numbers from where the engine of A stopped ?

Here is how I wrote the class :

class Proc {
  public:
mt19937 eng {random_device{}()};
double Tps;
vector<int> prc;
... }

Thanks for your help !

  • Did you define an `operator=` for your class or are you using the default one? – NathanOliver Jan 26 '16 at 14:18
  • 1
    The default assignment operator will copy the state of the mersenne twister member, which will deliver the same sequence of random numbers from that point on. – BeyelerStudios Jan 26 '16 at 14:19
  • Possible duplicate of [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Jef Patat Jan 26 '16 at 15:58

2 Answers2

0

Search for implicit copy constructor

The implicit copy constructor does a member-wise copy of the source object

Copy constructors

Jef Patat
  • 999
  • 1
  • 10
  • 26
0

The default copy assignment operator (as well as the default copy constructor) will just copy the whole mt19937 with all its internal state, so both instances will get the same sequence of random numbers.

One solution would be to reseed on every copy by designing a suitable assignment operator and copy constructor. This might take some time if you make many copy, especially if really want to produce a 19937 bit seed every time.

The other possibility would be a private static thread_local generator shared by all instances. That would get rid of the seeding problem and is probably the best solution.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Thanks for your help. My first idea was actually to declare my engine as local. I wanted to do write something like : "static m19937 eng {...} " as one attribute of the class Proc. But it doesnt compile. Can you please tell me how to declare the engine as static in my class ? Thanks ! –  Jan 27 '16 at 14:20
  • Actually what do you mean by "both instances will get the same sequence of random numbers.". When I do some tests like A=B; cout << A.random() << B.random() << endl; where random is a method of Proc written as: double random() { uniform_real_distribution<>dis(1,2); return dis(eng); } The results not similar at all. Looks like the results are "not correlated". –  Jan 27 '16 at 14:51
  • @CômeHuré Cannot reproduce that. http://coliru.stacked-crooked.com/a/7e9b2adbf01cfd58 the distribution does not change that. http://coliru.stacked-crooked.com/a/ca54fdadf78f20fb – Baum mit Augen Jan 27 '16 at 18:28
  • @CômeHuré And for your first comment: http://coliru.stacked-crooked.com/a/63e7def3112b6586 – Baum mit Augen Jan 27 '16 at 18:32
  • It's very strange. I copied and compiled your cpp on my mac and a linux server : the assert failed in both during both of the executions of the outputs. –  Jan 28 '16 at 14:43
  • Thank you for the second code you posted to me : my g++ on mac don't want to compile it, but it compiles well on my linux –  Jan 28 '16 at 14:44
  • @CômeHuré *"the assert failed in both during both of the executions of the outputs"* That really should not happen, what I stated in my answer is what the standard says. Might be worth posting a question for that (including information on what implementation you use), maybe someone can point you to a bug report. Tbh, I do not quite see how that would happen. – Baum mit Augen Jan 28 '16 at 15:07
  • @CômeHuré *"my g++ on mac don't want to compile it"* Maybe it's too old, `thread_local` is C++11. If your code is not multithreaded anyways, you can just drop it. – Baum mit Augen Jan 28 '16 at 15:09
  • I noticed I mixed the cpp files up when I did the tests yesterday... As you said, the 2 copied generators draw the same pseudo random numbers. –  Jan 29 '16 at 10:29
  • my mac compiled the file well when I removed the "thread_local" terms. Thanks a lot for the help. –  Jan 29 '16 at 10:30