1 #include "surf/random_mgr.h"
2 #include "xbt/sysdep.h"
6 static unsigned int _seed = 2147483647;
8 typedef unsigned __int64 uint64_t;
9 typedef unsigned int uint32_t;
13 unsigned short int __x[3]; /* Current state. */
14 unsigned short int __old_x[3]; /* Old state. */
15 unsigned short int __c; /* Additive const. in congruential formula. */
16 unsigned short int __init; /* Flag for initializing. */
17 unsigned long long int __a; /* Factor in congruential formula. */
20 static struct drand48_data __libc_drand48_data = {0};
26 /* This is the IEEE 754 double-precision format. */
29 /* Together these comprise the mantissa. */
30 unsigned int mantissa1:32;
31 unsigned int mantissa0:20;
32 unsigned int exponent:11;
33 unsigned int negative:1;
37 /* This format makes it easier to see if a NaN is a signalling NaN. */
40 /* Together these comprise the mantissa. */
41 unsigned int mantissa1:32;
42 unsigned int mantissa0:19;
43 unsigned int quiet_nan:1;
44 unsigned int exponent:11;
45 unsigned int negative:1;
50 #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
56 _drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer);
59 _erand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, double *result);
63 _erand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, double *result)
65 union ieee754_double temp;
67 /* Compute next state. */
68 if (_drand48_iterate(xsubi, buffer) < 0)
71 /* Construct a positive double with the 48 random bits distributed over
72 its fractional part so the resulting FP number is [0.0,1.0). */
74 temp.ieee.negative = 0;
75 temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
76 temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
77 temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
79 /* Please note the lower 4 bits of mantissa1 are always 0. */
80 *result = temp.d - 1.0;
86 _drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer)
91 /* Initialize buffer, if not yet done. */
93 if(buffer->__init == 0)
95 buffer->__a = 0x5deece66dull;
100 /* Do the real work. We choose a data type which contains at least
101 48 bits. Because we compute the modulus it does not care how
102 many bits really are computed. */
104 X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
106 result = X * buffer->__a + buffer->__c;
109 xsubi[0] = result & 0xffff;
110 xsubi[1] = (result >> 16) & 0xffff;
111 xsubi[2] = (result >> 32) & 0xffff;
122 (void) _erand48_r (__libc_drand48_data.__x, &__libc_drand48_data, &result);
128 _srand(unsigned int seed)
136 const long a = 16807;
137 const long m = 2147483647;
138 const long q = 127773; /* (m/a) */
139 const long r = 2836; /* (m%a) */
154 _seed = (int)(s & RAND_MAX);
160 _rand_r(unsigned int* pseed)
162 const long a = 16807;
163 const long m = 2147483647;
164 const long q = 127773; /* (m/a) */
165 const long r = 2836; /* (m%a) */
180 return (int)(s & RAND_MAX);
185 #define rand_r _rand_r
186 #define drand48 _drand48
190 static double custom_random(Generator generator, long int *seed){
196 return (double)rand_r((unsigned int*)seed)/RAND_MAX;
202 /* Generate numbers between min and max with a given mean and standard deviation */
203 double random_generate(random_data_t random) {
205 double alpha, beta, gamma;
206 double U1, U2, V, W, X;
208 if (random == NULL) return 0.0f;
210 if (random->std == 0)
211 return random->mean * (random->max - random->min) + random->min;
213 a = random->mean * ( random->mean * (1 - random->mean) / (random->std*random->std) - 1 );
214 b = (1 - random->mean) * ( random->mean * (1 - random->mean) / (random->std*random->std) - 1 );
217 if (a <= 1. || b <= 1.)
218 beta = ((1./a)>(1./b))?(1./a):(1./b);
220 beta = sqrt ((alpha-2.) / (2.*a*b - alpha));
224 /* Random generation for the Beta distribution based on
225 * R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. _Communications of the ACM_, *21*, 317-322.
226 * It is good for speed because it does not call math functions many times and respect the 4 given constraints
228 U1 = custom_random(random->generator,&(random->seed));
229 U2 = custom_random(random->generator,&(random->seed));
231 V = beta * log(U1/(1-U1));
233 } while (alpha * log(alpha/(b + W)) + gamma*V - log(4) < log(U1*U1*U2));
237 return X * (random->max - random->min) + random->min;
240 random_data_t random_new(Generator generator, long int seed,
241 double min, double max,
242 double mean, double std){
243 random_data_t random = xbt_new0(s_random_data_t, 1);
245 random->generator = generator;
250 /* Check user stupidities */
252 THROW2(arg_error,0,"random->max < random->min (%f < %f)",max, min);
254 THROW2(arg_error,0,"random->mean < random->min (%f < %f)",mean, min);
256 THROW2(arg_error,0,"random->mean > random->max (%f > %f)",mean, max);
258 /* normalize the mean and standard deviation before storing */
259 random->mean = (mean - min) / (max - min);
260 random->std = std / (max - min);
262 if (random->mean * (1-random->mean) < random->std*random->std)
263 THROW2(arg_error,0,"Invalid mean and standard deviation (%f and %f)",random->mean, random->std);