1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "surf/random_mgr.h"
8 #include "xbt/sysdep.h"
9 #include "simgrid_config.h" /*_XBT_WIN32*/
13 static unsigned int _seed = 2147483647;
15 typedef unsigned __int64 uint64_t;
16 typedef unsigned int uint32_t;
19 unsigned short int __x[3]; /* Current state. */
20 unsigned short int __old_x[3]; /* Old state. */
21 unsigned short int __c; /* Additive const. in congruential formula. */
22 unsigned short int __init; /* Flag for initializing. */
23 unsigned long long int __a; /* Factor in congruential formula. */
26 static struct drand48_data __libc_drand48_data = { 0 };
28 union ieee754_double {
31 /* This is the IEEE 754 double-precision format. */
33 /* Together these comprise the mantissa. */
34 unsigned int mantissa1:32;
35 unsigned int mantissa0:20;
36 unsigned int exponent:11;
37 unsigned int negative:1;
41 /* This format makes it easier to see if a NaN is a signalling NaN. */
43 /* Together these comprise the mantissa. */
44 unsigned int mantissa1:32;
45 unsigned int mantissa0:19;
46 unsigned int quiet_nan:1;
47 unsigned int exponent:11;
48 unsigned int negative:1;
53 #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
58 _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer);
61 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
66 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
69 union ieee754_double temp;
71 /* Compute next state. */
72 if (_drand48_iterate(xsubi, buffer) < 0)
75 /* Construct a positive double with the 48 random bits distributed over
76 its fractional part so the resulting FP number is [0.0,1.0). */
78 temp.ieee.negative = 0;
79 temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
80 temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
81 temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
83 /* Please note the lower 4 bits of mantissa1 are always 0. */
84 *result = temp.d - 1.0;
89 int _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer)
94 /* Initialize buffer, if not yet done. */
96 if (buffer->__init == 0) {
97 buffer->__a = 0x5deece66dull;
102 /* Do the real work. We choose a data type which contains at least
103 48 bits. Because we compute the modulus it does not care how
104 many bits really are computed. */
106 X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
108 result = X * buffer->__a + buffer->__c;
111 xsubi[0] = result & 0xffff;
112 xsubi[1] = (result >> 16) & 0xffff;
113 xsubi[2] = (result >> 32) & 0xffff;
119 double _drand48(void)
123 (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data, &result);
128 void _srand(unsigned int seed)
135 const long a = 16807;
136 const long m = 2147483647;
137 const long q = 127773; /* (m/a) */
138 const long r = 2836; /* (m%a) */
153 _seed = (int) (s & RAND_MAX);
158 int _rand_r(unsigned int *pseed)
160 const long a = 16807;
161 const long m = 2147483647;
162 const long q = 127773; /* (m/a) */
163 const long r = 2836; /* (m%a) */
178 return (int) (s & RAND_MAX);
183 #define rand_r _rand_r
184 #define drand48 _drand48
188 static double custom_random(Generator generator, long int *seed)
195 return (double) rand_r((unsigned int *) seed) / RAND_MAX;
201 /* Generate numbers between min and max with a given mean and standard deviation */
202 double random_generate(random_data_t random)
205 double alpha, beta, gamma;
206 double U1, U2, V, W, X;
211 if (random->std == 0)
212 return random->mean * (random->max - random->min) + random->min;
215 random->mean * (random->mean * (1 - random->mean) /
216 (random->std * random->std) - 1);
219 random->mean) * (random->mean * (1 -
220 random->mean) / (random->std *
224 if (a <= 1. || b <= 1.)
225 beta = ((1. / a) > (1. / b)) ? (1. / a) : (1. / b);
227 beta = sqrt((alpha - 2.) / (2. * a * b - alpha));
228 gamma = a + 1. / beta;
231 /* Random generation for the Beta distribution based on
232 * R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. _Communications of the ACM_, *21*, 317-322.
233 * It is good for speed because it does not call math functions many times and respect the 4 given constraints
235 U1 = custom_random(random->generator, &(random->seed));
236 U2 = custom_random(random->generator, &(random->seed));
238 V = beta * log(U1 / (1 - U1));
240 } while (alpha * log(alpha / (b + W)) + gamma * V - log(4) <
245 return X * (random->max - random->min) + random->min;
248 random_data_t random_new(Generator generator, long int seed,
249 double min, double max, double mean, double std)
251 random_data_t random = xbt_new0(s_random_data_t, 1);
253 random->generator = generator;
258 /* Check user stupidities */
260 THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
262 THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
264 THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
266 /* normalize the mean and standard deviation before storing */
267 random->mean = (mean - min) / (max - min);
268 random->std = std / (max - min);
270 if (random->mean * (1 - random->mean) < random->std * random->std)
271 THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
272 random->mean, random->std);