… | |
… | |
429 | { |
429 | { |
430 | p->~Tp (); |
430 | p->~Tp (); |
431 | } |
431 | } |
432 | }; |
432 | }; |
433 | |
433 | |
434 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
435 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
436 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
437 | struct tausworthe_random_generator |
|
|
438 | { |
|
|
439 | uint32_t state [4]; |
|
|
440 | |
|
|
441 | void operator =(const tausworthe_random_generator &src) |
|
|
442 | { |
|
|
443 | state [0] = src.state [0]; |
|
|
444 | state [1] = src.state [1]; |
|
|
445 | state [2] = src.state [2]; |
|
|
446 | state [3] = src.state [3]; |
|
|
447 | } |
|
|
448 | |
|
|
449 | void seed (uint32_t seed); |
|
|
450 | uint32_t next (); |
|
|
451 | }; |
|
|
452 | |
|
|
453 | // Xorshift RNGs, George Marsaglia |
|
|
454 | // http://www.jstatsoft.org/v08/i14/paper |
|
|
455 | // this one is about 40% faster than the tausworthe one above (i.e. not much), |
|
|
456 | // despite the inlining, and has the issue of only creating 2**32-1 numbers. |
|
|
457 | // see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf |
|
|
458 | struct xorshift_random_generator |
|
|
459 | { |
|
|
460 | uint32_t x, y; |
|
|
461 | |
|
|
462 | void operator =(const xorshift_random_generator &src) |
|
|
463 | { |
|
|
464 | x = src.x; |
|
|
465 | y = src.y; |
|
|
466 | } |
|
|
467 | |
|
|
468 | void seed (uint32_t seed) |
|
|
469 | { |
|
|
470 | x = seed; |
|
|
471 | y = seed * 69069U; |
|
|
472 | } |
|
|
473 | |
|
|
474 | uint32_t next () |
|
|
475 | { |
|
|
476 | uint32_t t = x ^ (x << 10); |
|
|
477 | x = y; |
|
|
478 | y = y ^ (y >> 13) ^ t ^ (t >> 10); |
|
|
479 | return y; |
|
|
480 | } |
|
|
481 | }; |
|
|
482 | |
|
|
483 | template<class generator> |
|
|
484 | struct random_number_generator : generator |
|
|
485 | { |
|
|
486 | // uniform distribution, [0 .. num - 1] |
|
|
487 | uint32_t operator ()(uint32_t num) |
|
|
488 | { |
|
|
489 | return !is_constant (num) ? get_range (num) // non-constant |
|
|
490 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
|
|
491 | : this->next () & (num - 1); // constant, power-of-two |
|
|
492 | } |
|
|
493 | |
|
|
494 | // return a number within the closed interval [min .. max], max can be >, < or == min. |
|
|
495 | int operator () (int r_min, int r_max) |
|
|
496 | { |
|
|
497 | return is_constant (r_min <= r_max) && r_min <= r_max |
|
|
498 | ? r_min + operator ()(r_max - r_min + 1) |
|
|
499 | : get_range (r_min, r_max); |
|
|
500 | } |
|
|
501 | |
|
|
502 | // return a number within the half-open interval [0..1[ |
|
|
503 | double operator ()() |
|
|
504 | { |
|
|
505 | return this->next () / (double)0x100000000ULL; |
|
|
506 | } |
|
|
507 | |
|
|
508 | protected: |
|
|
509 | uint32_t get_range (uint32_t r_max); |
|
|
510 | int get_range (int r_min, int r_max); |
|
|
511 | }; |
|
|
512 | |
|
|
513 | typedef random_number_generator<tausworthe_random_generator> rand_gen; |
|
|
514 | |
|
|
515 | extern rand_gen rndm, rmg_rndm; |
|
|
516 | |
|
|
517 | INTERFACE_CLASS (attachable) |
434 | INTERFACE_CLASS (attachable) |
518 | struct refcnt_base |
435 | struct refcnt_base |
519 | { |
436 | { |
520 | typedef int refcnt_t; |
437 | typedef int refcnt_t; |
521 | mutable refcnt_t ACC (RW, refcnt); |
438 | mutable refcnt_t ACC (RW, refcnt); |