Skip to content
Snippets Groups Projects
Commit 7fa1ed9e authored by Vladimír Ulman's avatar Vladimír Ulman
Browse files

BUG removed: Random generators reseeding happens to fast and the seeds (which...

BUG removed: Random generators reseeding happens to fast and the seeds (which are a function of time) were not changing...
parent 5493cc24
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
float GetRandomGauss(const float mean, const float sigma, const bool reseed) float GetRandomGauss(const float mean, const float sigma, const bool reseed)
{ {
static gsl_rng *randState=NULL; static gsl_rng *randState=NULL;
static unsigned long seed=-1;
//do we need to init random generator? //do we need to init random generator?
if (randState == NULL || reseed == true) if (randState == NULL || reseed == true)
...@@ -34,9 +35,10 @@ float GetRandomGauss(const float mean, const float sigma, const bool reseed) ...@@ -34,9 +35,10 @@ float GetRandomGauss(const float mean, const float sigma, const bool reseed)
//yes, we do: //yes, we do:
//create instance of the generator and seed it //create instance of the generator and seed it
if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default); if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
unsigned long s=-1 * (int) time(NULL); if (seed == -1) seed=-1 * (int) time(NULL);
DEBUG_REPORT("GetRandomGauss(): randomness started with seed " << s); else ++seed;
gsl_rng_set(randState,s); DEBUG_REPORT("GetRandomGauss(): randomness started with seed " << seed);
gsl_rng_set(randState,seed);
} }
return ( (float)gsl_ran_gaussian(randState, sigma) + mean ); return ( (float)gsl_ran_gaussian(randState, sigma) + mean );
...@@ -46,6 +48,7 @@ float GetRandomGauss(const float mean, const float sigma, const bool reseed) ...@@ -46,6 +48,7 @@ float GetRandomGauss(const float mean, const float sigma, const bool reseed)
float GetRandomUniform(const float A, const float B, const bool reseed) float GetRandomUniform(const float A, const float B, const bool reseed)
{ {
static gsl_rng *randState=NULL; static gsl_rng *randState=NULL;
static unsigned long seed=-1;
//do we need to init random generator? //do we need to init random generator?
if (randState == NULL || reseed == true) if (randState == NULL || reseed == true)
...@@ -53,9 +56,10 @@ float GetRandomUniform(const float A, const float B, const bool reseed) ...@@ -53,9 +56,10 @@ float GetRandomUniform(const float A, const float B, const bool reseed)
//yes, we do: //yes, we do:
//create instance of the generator and seed it //create instance of the generator and seed it
if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default); if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
unsigned long s=-1 * (int) time(NULL); if (seed == -1) seed=-1 * (int) time(NULL);
DEBUG_REPORT("GetRandomUniform(): randomness started with seed " << s); else ++seed;
gsl_rng_set(randState,s); DEBUG_REPORT("GetRandomUniform(): randomness started with seed " << seed);
gsl_rng_set(randState,seed);
} }
return ( (float)gsl_ran_flat(randState, A,B) ); return ( (float)gsl_ran_flat(randState, A,B) );
...@@ -65,6 +69,7 @@ float GetRandomUniform(const float A, const float B, const bool reseed) ...@@ -65,6 +69,7 @@ float GetRandomUniform(const float A, const float B, const bool reseed)
unsigned int GetRandomPoisson(const float mean, const bool reseed) unsigned int GetRandomPoisson(const float mean, const bool reseed)
{ {
static gsl_rng *randState=NULL; static gsl_rng *randState=NULL;
static unsigned long seed=-1;
//do we need to init random generator? //do we need to init random generator?
if (randState == NULL || reseed == true) if (randState == NULL || reseed == true)
...@@ -72,9 +77,10 @@ unsigned int GetRandomPoisson(const float mean, const bool reseed) ...@@ -72,9 +77,10 @@ unsigned int GetRandomPoisson(const float mean, const bool reseed)
//yes, we do: //yes, we do:
//create instance of the generator and seed it //create instance of the generator and seed it
if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default); if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
unsigned long s=-1 * (int) time(NULL); if (seed == -1) seed=-1 * (int) time(NULL);
DEBUG_REPORT("GetRandomPoisson(): randomness started with seed " << s); else ++seed;
gsl_rng_set(randState,s); DEBUG_REPORT("GetRandomPoisson(): randomness started with seed " << seed);
gsl_rng_set(randState,seed);
} }
return ( gsl_ran_poisson(randState, mean) ); return ( gsl_ran_poisson(randState, mean) );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment