I have a C++11 program that needs to create several independent random generators, for use by different threads in a parallel computation. These generators should be initialized with different seed values so that they all produce different pseudo-random sequences.
I see that there's a std::seed_seq
class that seems to be meant for this purpose, but it's not clear what's the right way to construct one. The examples I've seen, such as the one on cppreference.com, initialize it with a handful of integer constants hard-coded in the program:
std::seed_seq seq{1,2,3,4,5};
I doubt that's actually a recommended best practice, so I'm wondering what is the recommended practice. In particular:
- Since a
seed_seq
can be initialized with an arbitrary number of integers, what's the significance of the length of its initializer list? If I want to produce seeds for 100 random generators, do I need to initialize myseed_seq
with 100 integers? - If the length of the initializer list doesn't have to match the number of seeds I intend to generate, is it OK to initialize a
seed_seq
with just one integer and then use it to produce a large number of seeds? - How about initializing with no integers, i.e. using the default constructor? (This means I'd get the same seeds every time, of course.)
- If it's OK to construct a
seed_seq
from a single integer and then generate lots of seeds from it, what's the benefit of usingseed_seq
instead of an ordinary random generator? Why not just construct astd::mt19937
from that single integer and use that to produce seed values for other generators?