Mercurial > hg > audiodb
diff common.cpp @ 657:4c4389325ae4
Better RNG initialization
On platforms where it's available, use gettimeofday() and mix in the ~20
bits that that gives to the high (slowly-changing) bits of the seconds
since the Epoch.
author | mas01cr |
---|---|
date | Tue, 05 Jan 2010 14:45:00 +0000 |
parents | 4eedc18634f5 |
children | b1723ae7675e |
line wrap: on
line diff
--- a/common.cpp Tue Jan 05 14:09:43 2010 +0000 +++ b/common.cpp Tue Jan 05 14:45:00 2010 +0000 @@ -39,7 +39,21 @@ error("could not allocate Random Number Generator"); } /* FIXME: maybe we should use a real source of entropy? */ - gsl_rng_set(rng, time(NULL)); + uint32_t seed = 0; +#ifdef WIN32 + seed = time(NULL); +#else + struct timeval tv; + if(gettimeofday(&tv, NULL) == -1) { + error("failed to get time of day"); + } + /* usec field should be less than than 2^20. We want to mix the + usec field, highly-variable, into the high bits of the seconds + field, which will be static between closely-spaced runs. -- CSR, + 2010-01-05 */ + seed = tv.tv_sec ^ (tv.tv_usec << 12); +#endif + gsl_rng_set(rng, seed); } void audioDB::initDBHeader(const char* dbName) {