Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add seed management to have independant streams.
[simgrid.git] / src / surf / random_mgr.c
index 4c9cb5f..44f8534 100644 (file)
@@ -2,17 +2,27 @@
 #include "surf/random_mgr.h"
 #include "xbt/sysdep.h"
 
-static double custom_random(int generator){
+#ifdef WIN32
+static double drand48(void)
+{
+   THROW_UNIMPLEMENTED;
+   return -1;
+}
+#endif
+
+static double custom_random(Generator generator, long int *seed){
    switch(generator) {
-      case DRAND48: return drand48(); break;
-      case RAND: return (double)rand()/RAND_MAX; break;
-      default: return drand48();
+   case DRAND48:
+     return drand48();         
+   case RAND: 
+     return (double)rand_r((unsigned int*)seed)/RAND_MAX; 
+   default: return drand48();
    }
 }
 
 /* Generate numbers between min and max with a given mean and standard deviation */
-float random_generate(random_data_t random){  
-  float x1, x2, w, y;
+double random_generate(random_data_t random){  
+  double x1, x2, w, y;
   
   if (random == NULL) return 0.0f;  
 
@@ -22,8 +32,8 @@ float random_generate(random_data_t random){
          y1 = sqrt( - 2 * log(x1) ) * cos( 2 * pi * x2 )
     */ 
     do {
-      x1 = 2.0 * custom_random(random->generator) - 1.0;
-      x2 = 2.0 * custom_random(random->generator) - 1.0;
+      x1 = 2.0 * custom_random(random->generator,&(random->seed)) - 1.0;
+      x2 = 2.0 * custom_random(random->generator,&(random->seed)) - 1.0;
       w = x1 * x1 + x2 * x2;
     } while ( w >= 1.0 );
 
@@ -37,9 +47,12 @@ float random_generate(random_data_t random){
   return y;
 }
 
-random_data_t random_new(int generator, int min, int max, int mean, int stdDeviation){
+random_data_t random_new(Generator generator, long int seed, 
+                        double min, double max, double mean, 
+                        double stdDeviation){
   random_data_t random = xbt_new0(s_random_data_t, 1);
   random->generator = generator;
+  random->seed = seed;
   random->min = min;
   random->max = max;
   random->mean = mean;