From 7fa1ed9ea58d44e7b149040eeae48e3bac3589db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= <ulman@mpi-cbg.de>
Date: Mon, 9 Jan 2017 09:52:04 +0100
Subject: [PATCH] BUG removed: Random generators reseeding happens to fast and
 the seeds (which are a function of time) were not changing...

---
 src/rnd_generators.cpp | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/rnd_generators.cpp b/src/rnd_generators.cpp
index 545e236..1270a16 100644
--- a/src/rnd_generators.cpp
+++ b/src/rnd_generators.cpp
@@ -27,6 +27,7 @@
 float GetRandomGauss(const float mean, const float sigma, const bool reseed)
 {
 	static gsl_rng *randState=NULL;
+	static unsigned long seed=-1;
 
 	//do we need to init random generator?
 	if (randState == NULL || reseed == true)
@@ -34,9 +35,10 @@ float GetRandomGauss(const float mean, const float sigma, const bool reseed)
 		//yes, we do:
 		//create instance of the generator and seed it
 		if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
-		unsigned long s=-1 * (int) time(NULL);
-		DEBUG_REPORT("GetRandomGauss(): randomness started with seed " << s);
-		gsl_rng_set(randState,s);
+		if (seed == -1) seed=-1 * (int) time(NULL);
+		else ++seed;
+		DEBUG_REPORT("GetRandomGauss(): randomness started with seed " << seed);
+		gsl_rng_set(randState,seed);
 	}
 
 	return ( (float)gsl_ran_gaussian(randState, sigma) + mean );
@@ -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)
 {
 	static gsl_rng *randState=NULL;
+	static unsigned long seed=-1;
 
 	//do we need to init random generator?
 	if (randState == NULL || reseed == true)
@@ -53,9 +56,10 @@ float GetRandomUniform(const float A, const float B, const bool reseed)
 		//yes, we do:
 		//create instance of the generator and seed it
 		if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
-		unsigned long s=-1 * (int) time(NULL);
-		DEBUG_REPORT("GetRandomUniform(): randomness started with seed " << s);
-		gsl_rng_set(randState,s);
+		if (seed == -1) seed=-1 * (int) time(NULL);
+		else ++seed;
+		DEBUG_REPORT("GetRandomUniform(): randomness started with seed " << seed);
+		gsl_rng_set(randState,seed);
 	}
 
 	return ( (float)gsl_ran_flat(randState, A,B) );
@@ -65,6 +69,7 @@ float GetRandomUniform(const float A, const float B, const bool reseed)
 unsigned int GetRandomPoisson(const float mean, const bool reseed)
 {
 	static gsl_rng *randState=NULL;
+	static unsigned long seed=-1;
 
 	//do we need to init random generator?
 	if (randState == NULL || reseed == true)
@@ -72,9 +77,10 @@ unsigned int GetRandomPoisson(const float mean, const bool reseed)
 		//yes, we do:
 		//create instance of the generator and seed it
 		if (randState == NULL) randState = gsl_rng_alloc(gsl_rng_default);
-		unsigned long s=-1 * (int) time(NULL);
-		DEBUG_REPORT("GetRandomPoisson(): randomness started with seed " << s);
-		gsl_rng_set(randState,s);
+		if (seed == -1) seed=-1 * (int) time(NULL);
+		else ++seed;
+		DEBUG_REPORT("GetRandomPoisson(): randomness started with seed " << seed);
+		gsl_rng_set(randState,seed);
 	}
 
 	return ( gsl_ran_poisson(randState, mean) );
-- 
GitLab