Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rand_r() function is not implemented on Windows
[simgrid.git] / src / surf / random_mgr.c
index a5dc07f..2a28e07 100644 (file)
@@ -5,23 +5,31 @@
 #ifdef WIN32
 static double drand48(void)
 {
-   THROW_UNIMPLEMENTED();
+   THROW_UNIMPLEMENTED;
    return -1;
 }
+
+static double rand_r(unsigned int* seed)
+{
+       THROW_UNIMPLEMENTED;
+   return -1;
+}
+
 #endif
 
-static double custom_random(int generator){
+static double custom_random(Generator generator, long int *seed){
    switch(generator) {
-      
-       case DRAND48:return drand48();  
-       case RAND: return (double)rand()/RAND_MAX; 
+   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;  
 
@@ -31,8 +39,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 );
 
@@ -46,9 +54,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;