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.105 by root, Fri Apr 30 20:43:18 2010 UTC vs.
Revision 1.106 by root, Sun May 2 11:23:51 2010 UTC

481}; 481};
482 482
483template<class generator> 483template<class generator>
484struct random_number_generator : generator 484struct random_number_generator : generator
485{ 485{
486 // uniform distribution, 0 .. max (0, num - 1) 486 // uniform distribution, [0 .. num - 1]
487 uint32_t operator ()(uint32_t num) 487 uint32_t operator ()(uint32_t num)
488 { 488 {
489 return !is_constant (num) ? get_range (num) // non-constant 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 490 : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two
491 : this->next () & (num - 1); // constant, power-of-two 491 : this->next () & (num - 1); // constant, power-of-two
497 return is_constant (r_min <= r_max) && r_min <= r_max 497 return is_constant (r_min <= r_max) && r_min <= r_max
498 ? r_min + operator ()(r_max - r_min + 1) 498 ? r_min + operator ()(r_max - r_min + 1)
499 : get_range (r_min, r_max); 499 : get_range (r_min, r_max);
500 } 500 }
501 501
502 // return a number within the closed interval [0..1] 502 // return a number within the half-open interval [0..1[
503 double operator ()() 503 double operator ()()
504 { 504 {
505 return this->next () / (double)0xFFFFFFFFU; 505 return this->next () / 0x100000000;
506 } 506 }
507 507
508protected: 508protected:
509 uint32_t get_range (uint32_t r_max); 509 uint32_t get_range (uint32_t r_max);
510 int get_range (int r_min, int r_max); 510 int get_range (int r_min, int r_max);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines