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 "gras_config.h" /*_XBT_WIN32*/
13 static unsigned int _seed = 2147483647;
16 typedef unsigned __int64 uint64_t;
17 typedef unsigned int uint32_t;
21 unsigned short int __x[3]; /* Current state. */
22 unsigned short int __old_x[3]; /* Old state. */
23 unsigned short int __c; /* Additive const. in congruential formula. */
24 unsigned short int __init; /* Flag for initializing. */
25 unsigned long long int __a; /* Factor in congruential formula. */
28 static struct drand48_data __libc_drand48_data = { 0 };
30 union ieee754_double {
33 /* This is the IEEE 754 double-precision format. */
35 /* Together these comprise the mantissa. */
36 unsigned int mantissa1:32;
37 unsigned int mantissa0:20;
38 unsigned int exponent:11;
39 unsigned int negative:1;
43 /* This format makes it easier to see if a NaN is a signalling NaN. */
45 /* Together these comprise the mantissa. */
46 unsigned int mantissa1:32;
47 unsigned int mantissa0:19;
48 unsigned int quiet_nan:1;
49 unsigned int exponent:11;
50 unsigned int negative:1;
55 #define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */
60 _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer);
63 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
68 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
71 union ieee754_double temp;
73 /* Compute next state. */
74 if (_drand48_iterate(xsubi, buffer) < 0)
77 /* Construct a positive double with the 48 random bits distributed over
78 its fractional part so the resulting FP number is [0.0,1.0). */
80 temp.ieee.negative = 0;
81 temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
82 temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
83 temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
85 /* Please note the lower 4 bits of mantissa1 are always 0. */
86 *result = temp.d - 1.0;
91 int _drand48_iterate(unsigned short int xsubi[3],
92 struct drand48_data *buffer)
97 /* Initialize buffer, if not yet done. */
99 if (buffer->__init == 0) {
100 buffer->__a = 0x5deece66dull;
105 /* Do the real work. We choose a data type which contains at least
106 48 bits. Because we compute the modulus it does not care how
107 many bits really are computed. */
109 X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
111 result = X * buffer->__a + buffer->__c;
114 xsubi[0] = result & 0xffff;
115 xsubi[1] = (result >> 16) & 0xffff;
116 xsubi[2] = (result >> 32) & 0xffff;
122 double _drand48(void)
126 (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data,
132 void _srand(unsigned int seed)
139 const long a = 16807;
140 const long m = 2147483647;
141 const long q = 127773; /* (m/a) */
142 const long r = 2836; /* (m%a) */
157 _seed = (int) (s & RAND_MAX);
162 int _rand_r(unsigned int *pseed)
164 const long a = 16807;
165 const long m = 2147483647;
166 const long q = 127773; /* (m/a) */
167 const long r = 2836; /* (m%a) */
182 return (int) (s & RAND_MAX);
187 #define rand_r _rand_r
188 #define drand48 _drand48
192 static double custom_random(Generator generator, long int *seed)
199 return (double) rand_r((unsigned int *) seed) / RAND_MAX;
201 printf("Seen RNGSTREAM");
208 /* Generate numbers between min and max with a given mean and standard deviation */
209 double random_generate(random_data_t random)
212 double alpha, beta, gamma;
213 double U1, U2, V, W, X;
218 if (random->std == 0)
219 return random->mean * (random->max - random->min) + random->min;
221 a = random->mean * (random->mean * (1 - random->mean) /
222 (random->std * random->std) - 1);
224 random->mean) * (random->mean * (1 -
225 random->mean) / (random->std *
229 if (a <= 1. || b <= 1.)
230 beta = ((1. / a) > (1. / b)) ? (1. / a) : (1. / b);
232 beta = sqrt((alpha - 2.) / (2. * a * b - alpha));
233 gamma = a + 1. / beta;
236 /* Random generation for the Beta distribution based on
237 * R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. _Communications of the ACM_, *21*, 317-322.
238 * It is good for speed because it does not call math functions many times and respect the 4 given constraints
240 U1 = custom_random(random->generator, &(random->seed));
241 U2 = custom_random(random->generator, &(random->seed));
243 V = beta * log(U1 / (1 - U1));
245 } while (alpha * log(alpha / (b + W)) + gamma * V - log(4) <
250 return X * (random->max - random->min) + random->min;
253 random_data_t random_new(Generator generator, long int seed,
254 double min, double max, double mean, double std)
256 random_data_t random = xbt_new0(s_random_data_t, 1);
258 random->generator = generator;
263 /* Check user stupidities */
265 THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
267 THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
270 THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
273 /* normalize the mean and standard deviation before storing */
274 random->mean = (mean - min) / (max - min);
275 random->std = std / (max - min);
277 if (random->mean * (1 - random->mean) < random->std * random->std)
278 THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
279 random->mean, random->std);