Mercurial > hg > audiodb
changeset 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 | b82b90c6445e |
children | e7fae71ee676 |
files | audioDB.h common.cpp |
diffstat | 2 files changed, 17 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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 <stdlib.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/time.h> #if !defined(WIN32) #include <sys/mman.h> #endif @@ -16,7 +17,7 @@ #include <map> #include <string> #include <math.h> -#include <sys/time.h> +#include <time.h> #include <assert.h> #include <float.h> #include <signal.h>
--- 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) {