… | |
… | |
389 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
389 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
390 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
390 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
391 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
391 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
392 | struct tausworthe_random_generator |
392 | struct tausworthe_random_generator |
393 | { |
393 | { |
394 | // generator |
|
|
395 | uint32_t state [4]; |
394 | uint32_t state [4]; |
396 | |
395 | |
397 | void operator =(const tausworthe_random_generator &src) |
396 | void operator =(const tausworthe_random_generator &src) |
398 | { |
397 | { |
399 | state [0] = src.state [0]; |
398 | state [0] = src.state [0]; |
… | |
… | |
402 | state [3] = src.state [3]; |
401 | state [3] = src.state [3]; |
403 | } |
402 | } |
404 | |
403 | |
405 | void seed (uint32_t seed); |
404 | void seed (uint32_t seed); |
406 | uint32_t next (); |
405 | uint32_t next (); |
|
|
406 | }; |
407 | |
407 | |
|
|
408 | // Xorshift RNGs, George Marsaglia |
|
|
409 | // http://www.jstatsoft.org/v08/i14/paper |
|
|
410 | // this one is about 40% faster than the tausworthe one above (i.e. not much), |
|
|
411 | // despite the inlining, and has the issue of only creating 2**32-1 numbers. |
|
|
412 | struct xorshift_random_generator |
|
|
413 | { |
|
|
414 | uint32_t x, y; |
|
|
415 | |
|
|
416 | void operator =(const xorshift_random_generator &src) |
|
|
417 | { |
|
|
418 | x = src.x; |
|
|
419 | y = src.y; |
|
|
420 | } |
|
|
421 | |
|
|
422 | void seed (uint32_t seed) |
|
|
423 | { |
|
|
424 | x = seed; |
|
|
425 | y = seed * 69069U; |
|
|
426 | } |
|
|
427 | |
|
|
428 | uint32_t next () |
|
|
429 | { |
|
|
430 | uint32_t t = x ^ (x << 10); |
|
|
431 | x = y; |
|
|
432 | y = y ^ (y >> 13) ^ t ^ (t >> 10); |
|
|
433 | return y; |
|
|
434 | } |
|
|
435 | }; |
|
|
436 | |
|
|
437 | template<class generator> |
|
|
438 | struct random_number_generator : generator |
|
|
439 | { |
408 | // uniform distribution, 0 .. max (0, num - 1) |
440 | // uniform distribution, 0 .. max (0, num - 1) |
409 | uint32_t operator ()(uint32_t num) |
441 | uint32_t operator ()(uint32_t num) |
410 | { |
442 | { |
411 | return !is_constant (num) ? get_range (num) // non-constant |
443 | return !is_constant (num) ? get_range (num) // non-constant |
412 | : num & (num - 1) ? (next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
444 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
413 | : next () & (num - 1); // constant, power-of-two |
445 | : this->next () & (num - 1); // constant, power-of-two |
414 | } |
446 | } |
415 | |
447 | |
416 | // return a number within (min .. max) |
448 | // return a number within (min .. max) |
417 | int operator () (int r_min, int r_max) |
449 | int operator () (int r_min, int r_max) |
418 | { |
450 | { |
… | |
… | |
429 | protected: |
461 | protected: |
430 | uint32_t get_range (uint32_t r_max); |
462 | uint32_t get_range (uint32_t r_max); |
431 | int get_range (int r_min, int r_max); |
463 | int get_range (int r_min, int r_max); |
432 | }; |
464 | }; |
433 | |
465 | |
434 | typedef tausworthe_random_generator rand_gen; |
466 | typedef random_number_generator<tausworthe_random_generator> rand_gen; |
435 | |
467 | |
436 | extern rand_gen rndm, rmg_rndm; |
468 | extern rand_gen rndm, rmg_rndm; |
437 | |
469 | |
438 | INTERFACE_CLASS (attachable) |
470 | INTERFACE_CLASS (attachable) |
439 | struct refcnt_base |
471 | struct refcnt_base |