Random Number Generator Seed Initialization

Started by
1 comment, last by Bregma 8 years, 3 months ago

Hi,

I always used a global uint32 seed in the anonymous namespace.

Is it better to have each time a seed initialized in the constructor of random number generator and instance each time needed ?

Thanks

Advertisement

Having a per-instance seed allows for things like procedurally generated worlds where the co-ordinates are used to generate a seed, while still allowing a separate per-session random instance to control events. A global value also impacts on thread safety.

As an example, in C++ the classic std::rand() uses a global seed initialised by std::srand(), where as the newer C++ random generators are instance oriented.

It depends on what you're trying to do with what.

If you're only ever going to use a single LCG PRNG, then if you want it to act like a global variable, go ahead and use a global variable as its seed.

If you want to use something more sophisticated than a single 32-bit LCG, like a lagged Fibonacci or a Mersenne Twister PRNG, then a 32-bit initialization vector is insufficient to start with.

If you want to use several different PRNGs in an application, how could a single 32-bit initialization vector ever help you?

If you're relying on a single 32-bit initialization vector in the global namespace to be uninitialized to use as a seed value for any PRNG, you are in for a quality debugging treat.

Initializing an initialization vector in the constructor of a PRNG object makes no sense. You want to pass the initialization vector in to the constructor so it can use it to initialize the PRNG.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement