Commit 5848b8bb authored by thomas.forbriger's avatar thomas.forbriger
Browse files

libtf [FEATURE]: change seeding mechanism of random number generator

The GSL random number generators were seeded with the value of the wall clock
second. When requeting several sequences of random numbers, they turned out to
be identical, because each time the random number generator got seeded with
the same value.

The new version accepts seed values set through the environment variable
GSL_RNG_SEED and, if not set, uses the nanosecond value of the wall clock
together with the seconds to ensure different seed for different calls to the
functions.
parent eab9c614
......@@ -31,11 +31,15 @@
* REVISIONS and CHANGES
* - 07/05/2007 V1.0 Thomas Forbriger
* - 13/11/2010 V1.1 changed name of file
* - 14/02/2019 V1.2 change seeding mechanism
* accept default seed value provided through
* environment variable; if no default is set, use
* nanoseconds to seed
*
* ============================================================================
*/
#define TF_TF_GSL_RND_C_CC_VERSION \
"TF_TF_GSL_RND_C_CC V1.1"
"TF_TF_GSL_RND_C_CC V1.2"
#define TF_TF_GSL_RND_C_CC_CVSID \
"$Id$"
......@@ -55,13 +59,19 @@ int tf_gsl_rng_ugaussian__(doublereal* a, integer* n)
int i;
/* set the default type of random number generator
* controlled by the environment variable GSL_RNG_TYPE
* and allocate a random number generator
* seed value can be controlled by GSL_RNG_SEED
*/
gsl_rng_env_setup();
/* seed the generator with the current time if seed is
* not set on command line */
if (gsl_rng_default_seed == 0) {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
gsl_rng_default_seed = tp.tv_nsec + time(0) ;
}
const gsl_rng_type* T=gsl_rng_default;
/* and allocate a random number generator */
gsl_rng* R=gsl_rng_alloc(T);
/* seed the generator with the current time */
gsl_rng_set(R, time(0));
/* read values from the generator, using the desired distribution */
for (i=0; i<=*n; ++i)
{ a[i]=(doublereal)gsl_ran_ugaussian(R); }
......@@ -81,13 +91,19 @@ int tf_gsl_rng_uniform__(doublereal* a, integer* n)
int i;
/* set the default type of random number generator
* controlled by the environment variable GSL_RNG_TYPE
* and allocate a random number generator
* seed value can be controlled by GSL_RNG_SEED
*/
gsl_rng_env_setup();
/* seed the generator with the current time if seed is
* not set on command line */
if (gsl_rng_default_seed == 0) {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
gsl_rng_default_seed = tp.tv_nsec + time(0) ;
}
const gsl_rng_type* T=gsl_rng_default;
/* and allocate a random number generator */
gsl_rng* R=gsl_rng_alloc(T);
/* seed the generator with the current time */
gsl_rng_set(R, time(0));
/* read values from the generator, using the desired distribution */
for (i=0; i<=*n; ++i)
{ a[i]=(doublereal)gsl_rng_uniform(R); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment