I need a \'good\' way to initialize the pseudo-random number generator in C++. I\'ve found an article that states:
In order to generate random-like
The real question you must ask yourself is what randomness quality you need.
libc random is a LCG
The quality of randomness will be low whatever input you provide srand with.
If you simply need to make sure that different instances will have different initializations, you can mix process id (getpid), thread id and a timer. Mix the results with xor. Entropy should be sufficient for most applications.
Example :
struct timeb tp;
ftime(&tp);
srand(static_cast<unsigned int>(getpid()) ^
static_cast<unsigned int>(pthread_self()) ^
static_cast<unsigned int >(tp.millitm));
For better random quality, use /dev/urandom. You can make the above code portable in using boost::thread and boost::date_time.
Best way is to use another pseudorandom number generator. Mersenne twister (and Wichmann-Hill) is my recommendation.
http://en.wikipedia.org/wiki/Mersenne_twister
if you need a better random number generator, don't use the libc rand. Instead just use something like /dev/random
or /dev/urandom
directly (read in an int
directly from it or something like that).
The only real benefit of the libc rand is that given a seed, it is predictable which helps with debugging.