2 #include "surf/random_mgr.h"
3 #include "xbt/sysdep.h"
6 static double drand48(void)
8 return rand()/(double)RAND_MAX;
12 static double custom_random(int generator){
15 case DRAND48:return drand48();
16 case RAND: return (double)rand()/RAND_MAX;
17 default: return drand48();
21 /* Generate numbers between min and max with a given mean and standard deviation */
22 float random_generate(random_data_t random){
25 if (random == NULL) return 0.0f;
28 /* 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.
29 It is good for speed because it does not call math functions many times. Another way would be to simply:
30 y1 = sqrt( - 2 * log(x1) ) * cos( 2 * pi * x2 )
33 x1 = 2.0 * custom_random(random->generator) - 1.0;
34 x2 = 2.0 * custom_random(random->generator) - 1.0;
35 w = x1 * x1 + x2 * x2;
38 w = sqrt( (-2.0 * log( w ) ) / w );
41 /* Multiply the Box-Muller value by the standard deviation and add the mean */
42 y = y * random->stdDeviation + random->mean;
43 } while (!(random->min <= y && y <= random->max));
48 random_data_t random_new(int generator, int min, int max, int mean, int stdDeviation){
49 random_data_t random = xbt_new0(s_random_data_t, 1);
50 random->generator = generator;
54 random->stdDeviation = stdDeviation;