2 #include "surf/random_mgr.h"
3 #include "xbt/sysdep.h"
6 static double drand48(void)
12 static double rand_r(unsigned int* seed)
20 static double custom_random(Generator generator, long int *seed){
25 return (double)rand_r((unsigned int*)seed)/RAND_MAX;
26 default: return drand48();
30 /* Generate numbers between min and max with a given mean and standard deviation */
31 double random_generate(random_data_t random){
34 if (random == NULL) return 0.0f;
37 /* Apply the polar form of the Box-Muller Transform to map the two uniform random numbers to a pair of numbers from a normal distribution.
38 It is good for speed because it does not call math functions many times. Another way would be to simply:
39 y1 = sqrt( - 2 * log(x1) ) * cos( 2 * pi * x2 )
42 x1 = 2.0 * custom_random(random->generator,&(random->seed)) - 1.0;
43 x2 = 2.0 * custom_random(random->generator,&(random->seed)) - 1.0;
44 w = x1 * x1 + x2 * x2;
47 w = sqrt( (-2.0 * log( w ) ) / w );
50 /* Multiply the Box-Muller value by the standard deviation and add the mean */
51 y = y * random->stdDeviation + random->mean;
52 } while (!(random->min <= y && y <= random->max));
57 random_data_t random_new(Generator generator, long int seed,
58 double min, double max, double mean,
60 random_data_t random = xbt_new0(s_random_data_t, 1);
61 random->generator = generator;
66 random->stdDeviation = stdDeviation;