]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/yasli/random.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / yasli / random.h
1 #ifndef YASLI_RANDOM_H_
2 #define YASLI_RANDOM_H_
3
4 // $Id: random.h 754 2006-10-17 19:59:11Z syntheticpp $
5
6
7 #include <ctime>
8
9 class Random
10 {
11         unsigned int seed_;
12 public:
13         Random(unsigned int seed = 0)
14                 : seed_(seed ? seed : static_cast<unsigned int>(std::time(0)))
15         {
16         }
17         unsigned short nextShort()
18         {
19                 /* Use any number from this list for "a"
20                     18000 18030 18273 18513 18879 19074 19098 19164 19215 19584
21                     19599 19950 20088 20508 20544 20664 20814 20970 21153 21243
22                     21423 21723 21954 22125 22188 22293 22860 22938 22965 22974
23                     23109 23124 23163 23208 23508 23520 23553 23658 23865 24114
24                     24219 24660 24699 24864 24948 25023 25308 25443 26004 26088
25                     26154 26550 26679 26838 27183 27258 27753 27795 27810 27834
26                     27960 28320 28380 28689 28710 28794 28854 28959 28980 29013
27                     29379 29889 30135 30345 30459 30714 30903 30963 31059 31083
28                 */
29                 static const unsigned int a = 18000;
30                 return static_cast<unsigned short>(seed_ =
31                                                        a * (seed_ & 65535) +
32                                                        (seed_ >> 16));
33         }
34
35         unsigned int nextUint()
36         {
37                 return (unsigned int)nextShort() << (CHAR_BIT * sizeof(unsigned short)) |
38                        nextShort();
39         }
40         unsigned int nextUint(unsigned int high)
41         {
42                 assert(high < ULONG_MAX - 1);
43                 ++high;
44                 const unsigned int bucket_size = ULONG_MAX / high;
45                 unsigned int a;
46                 do
47                 {
48                         a = nextUint() / bucket_size;
49                 }
50                 while (a >= high);
51                 return a;
52         }
53 };
54
55 #endif // RANDOM_H_