Mercurial > hg > audiodb
comparison 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 |
comparison
equal
deleted
inserted
replaced
656:b82b90c6445e | 657:4c4389325ae4 |
---|---|
37 rng = gsl_rng_alloc(gsl_rng_mt19937); | 37 rng = gsl_rng_alloc(gsl_rng_mt19937); |
38 if(!rng) { | 38 if(!rng) { |
39 error("could not allocate Random Number Generator"); | 39 error("could not allocate Random Number Generator"); |
40 } | 40 } |
41 /* FIXME: maybe we should use a real source of entropy? */ | 41 /* FIXME: maybe we should use a real source of entropy? */ |
42 gsl_rng_set(rng, time(NULL)); | 42 uint32_t seed = 0; |
43 #ifdef WIN32 | |
44 seed = time(NULL); | |
45 #else | |
46 struct timeval tv; | |
47 if(gettimeofday(&tv, NULL) == -1) { | |
48 error("failed to get time of day"); | |
49 } | |
50 /* usec field should be less than than 2^20. We want to mix the | |
51 usec field, highly-variable, into the high bits of the seconds | |
52 field, which will be static between closely-spaced runs. -- CSR, | |
53 2010-01-05 */ | |
54 seed = tv.tv_sec ^ (tv.tv_usec << 12); | |
55 #endif | |
56 gsl_rng_set(rng, seed); | |
43 } | 57 } |
44 | 58 |
45 void audioDB::initDBHeader(const char* dbName) { | 59 void audioDB::initDBHeader(const char* dbName) { |
46 if(!adb) { | 60 if(!adb) { |
47 adb = audiodb_open(dbName, forWrite ? O_RDWR : O_RDONLY); | 61 adb = audiodb_open(dbName, forWrite ? O_RDWR : O_RDONLY); |