# HG changeset patch # User mas01cr # Date 1262702700 0 # Node ID 4c4389325ae445042b84fd52c3e57514167f0275 # Parent b82b90c6445e427282ea0f4b9b694244f3852095 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. diff -r b82b90c6445e -r 4c4389325ae4 audioDB.h --- a/audioDB.h Tue Jan 05 14:09:43 2010 +0000 +++ b/audioDB.h Tue Jan 05 14:45:00 2010 +0000 @@ -5,6 +5,7 @@ #include #include #include +#include #if !defined(WIN32) #include #endif @@ -16,7 +17,7 @@ #include #include #include -#include +#include #include #include #include diff -r b82b90c6445e -r 4c4389325ae4 common.cpp --- 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) {