246 {
247
248 static bool prepared = false;
249 static unsigned int iT;
250 static unsigned int iK;
251 static unsigned int iS;
252 static int iN;
253 static double fS;
254 static double fT;
255
256 if ( (RAND_MAX >> 31) > 0 )
257 {
258
259 if ( !prepared ) {
260 iS = (unsigned long)RAND_MAX + 1;
261 iK = 1;
262
263 int StoK = iS;
264
265
266
267 if( (unsigned long) (RAND_MAX) <= (( (1uL) << 31 ) - 1 ) ) {
268 iK = 2;
269
270 StoK = iS*iS;
271 }
272 int m;
273 for ( m = 0; m < 64; ++m ) {
274 StoK >>= 1;
275 if (StoK == 0) break;
276 }
277 iT = 1 << m;
278 prepared = true;
279 }
280 int v = 0;
281 do {
282 for ( int i = 0; i < iK; ++i ) {
283 v = iS*v+rand(); ++seq;
284 }
285 } while (v < iT);
286 return v & 0xFFFFFFFF;
287
288 }
289
290 else if ( (RAND_MAX >> 26) == 0 )
291 {
292
293
294
295 if ( !prepared ) {
296 fS = (unsigned long)RAND_MAX + 1;
297 double twoTo32 = std::ldexp(1.0,32);
298 double StoK = fS;
299 for ( iK = 1; StoK < twoTo32; StoK *= fS, iK++ ) { }
300 int m;
301 fT = 1.0;
302 for ( m = 0; m < 64; ++m ) {
303 StoK *= .5;
304 if (StoK < 1.0) break;
305 fT *= 2.0;
306 }
307 prepared = true;
308 }
309 double v = 0;
310 do {
311 for ( int i = 0; i < iK; ++i ) {
312 v = fS*v+rand(); ++seq;
313 }
314 } while (v < fT);
315 return ((unsigned int)v) & 0xFFFFFFFF;
316
317 }
318 else
319 {
320
321
322 if ( !prepared ) {
323 iS = (unsigned long)RAND_MAX + 1;
324 int SshiftN = iS;
325 for (iN = 0; SshiftN > 1; SshiftN >>= 1, iN++) { }
326 iN -= 17;
327 prepared = true;
328 }
329 unsigned int x1, x2;
330 do { x1 = rand(); ++seq;} while (x1 < (1<<16) );
331 do { x2 = rand(); ++seq;} while (x2 < (1<<16) );
332 return x1 | (x2 << 16);
333 }
334
335 }