Prior to C ++ 11, I used rand() from <cstdlib> , and it was very simple to select the seeds (or not) of the generator in the main() function (for example), and then use random numbers generated by the function in libraryA somewhere in library B. The code looked like this:
LibraryB (generates random numbers, old fashioned way):
#include <cstdlib> // rand, RAND_MAX double GetRandDoubleBetween0And1() { return ((double)rand()) / ((double)RAND_MAX); }
The main program:
#include <cstdlib> // srand #include <ctime> // time, clock int main() { bool iWantToSeed = true; // or false, // decide HERE, applies everywhere! if(iWantToSeed){ srand((unsigned)time(0) + (unsigned int)clock()); } // (...) }
LibraryA (uses random numbers from LibraryB generated according to the seed specified in main() ):
#include "../folderOfLibraryB/Utils_random.h"
Easy, right?
Now, I would like to update GetRandDoubleBetween0And1() using the C ++ 11 standards available through #include <random> . I already read and saw examples here and there , but I donβt know. Look how to adapt it to my three modules. Of course, sowing the engine inside GetRandDoubleBetween0And1() is not what you need to do ...
Do you think I will have to pass the seed engine from main() to UseSomeRandomness() to LibraryA, and then from UseSomeRandomness() to GetRandDoubleBetween0And1() to LibraryB? Or is there an easier way?
Georg source share