73 #if defined(USE_GETTIMEOFDAY) || defined(unix) || defined(__unix__) || defined(__unix)
75 #if defined(USE_GETTIMEOFDAY) || (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))
77 #define USE_GETTIMEOFDAY
83 #define M_LOGSQRT2PI 0.9189385332046727417803297
89 #define MT_MATRIX_A 0x9908B0DFUL
90 #define MT_UPPER_MASK 0x80000000UL
91 #define MT_LOWER_MASK 0x7FFFFFFFUL
95 typedef struct randmtstruct_t
138 #if ULONG_MAX > 0xFFFFFFFFUL
139 seed &= 0xFFFFFFFFUL;
141 (mt = generator->
mt)[0] = seed;
143 for(mti = 1; mti <
MT_N; mti++)
145 mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
151 #if ULONG_MAX > 0xFFFFFFFFUL
152 mt[mti] &= 0xFFFFFFFFUL;
164 unsigned long int seed;
167 seed = (
unsigned long int) time(NULL);
170 seed += ((
unsigned long int) generator);
172 #if defined(USE_GETTIMEOFDAY)
176 (void) gettimeofday(&tp, NULL);
192 const unsigned long mag01[2] = { 0x0UL,
MT_MATRIX_A };
193 unsigned long y, *mt;
200 mti = generator->
mti;
210 for(kk = 0; kk <
MT_N - MT_M; kk++)
213 mt[kk] = mt[kk + MT_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
215 for(; kk <
MT_N - 1; kk++)
218 mt[kk] = mt[kk + (MT_M -
MT_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
222 mt[MT_N - 1] = mt[MT_M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
230 y ^= (y << 7) & 0x9D2C5680UL;
231 y ^= (y << 15) & 0xEFC60000UL;
234 generator->
mti = mti;
243 const double d = a - 1.0/3.0;
244 const double c = 1.0/sqrt(9*d);
260 }
while(u >= 1 - 0.0331*x*x
261 && log(u) >= x/2 + d*(1 - v + log(v)));
274 static double logfactorial(
double n)
277 static const double Table[10] = {
278 0.69314718055994530942, 1.7917594692280550008,
279 3.1780538303479456197, 4.7874917427820459943,
280 6.5792512120101009951, 8.5251613610654143002,
281 10.604602902745250228, 12.801827480081469611,
282 15.104412573075515295, 17.502307845873885839};
284 static const double C[7] = { -1.910444077728e-03,
285 8.4171387781295e-04, -5.952379913043012e-04,
286 7.93650793500350248e-04, -2.777777777777681622553e-03,
287 8.333333333333333331554247e-02, 5.7083835261e-03};
288 double x, xsq, res, corr;
293 x = 1 + floor(n + 0.5);
300 for(i = 0; i < 6; i++)
301 res = res/xsq + C[i];
311 i = (int)floor(n + 0.5);
312 return (i >= 2) ? Table[i - 2] : 0;
320 double k, b, a, U, V, us, vr, invalpha, logmu;
336 b = 0.931 + 2.53*sqrt(mu);
337 a = -0.059 + 0.02483*b;
338 vr = 0.9277 - 3.6224/(b - 2);
339 invalpha = 1.1239 + 1.1328/(b - 3.4);
347 k = floor((2*a/us + b)*U + mu + 0.43);
348 }
while((us < 0.07 || V > vr)
349 && (k < 0 || (us < 0.013 && V > us)
350 || log(V*invalpha/(a/(us*us) + b))
351 > -mu + k*logmu - logfactorial(k)));