ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.82 by root, Sat Dec 27 02:33:32 2008 UTC vs.
Revision 1.83 by root, Tue Dec 30 07:24:16 2008 UTC

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
392struct tausworthe_random_generator 392struct 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.
412struct 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
437template<class generator>
438struct 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 {
429protected: 461protected:
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
434typedef tausworthe_random_generator rand_gen; 466typedef random_number_generator<tausworthe_random_generator> rand_gen;
435 467
436extern rand_gen rndm, rmg_rndm; 468extern rand_gen rndm, rmg_rndm;
437 469
438INTERFACE_CLASS (attachable) 470INTERFACE_CLASS (attachable)
439struct refcnt_base 471struct refcnt_base

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines