Zero  0.1.0
RandomBase.hpp
Go to the documentation of this file.
1 #ifndef LINTEL_RANDOM_BASE_HPP
2 #define LINTEL_RANDOM_BASE_HPP
3 
4 #include <cstdint>
5 #include <vector>
6 
7 #ifndef UINT32_MAX
8 #define UINT32_MAX (0xFFFFFFF)
9 #endif
10 
11 namespace lintel {
12 
13  template<class R>
14  class RandomTempl : public R {
15  public:
16 
17  RandomTempl(uint32_t seed = 0) : R(seed),
18  boolShift(0),
19  boolState(0) {}
20 
21  RandomTempl(std::vector<uint32_t> seed_array) : R(seed_array),
22  boolShift(0),
23  boolState(0) {}
24 
26 
27  inline void init(uint32_t seed) {
28  R::init(seed);
29  }
30 
31  inline void initArray(std::vector<uint32_t> seed_array) {
32  R::initArray(seed_array);
33  }
34 
35  inline uint32_t randInt() {
36  return R::randInt();
37  }
38 
39  inline unsigned long long randLongLong() {
40  unsigned long long ret;
41  ret = randInt();
42  ret = ret << 32;
43  ret = ret | randInt();
44  return ret;
45  }
46 
47  // Slightly biased
48  inline uint32_t randInt(uint32_t max) {
49  return randInt() % max;
50  }
51 
52  inline uint32_t randIntUnbiased(uint32_t max) {
53  do {
54  uint32_t res = randInt();
55  if (UINT32_MAX - max > res) {
56  //Quick test to handle most cases
57  return res % max;
58  } else if ((UINT32_MAX / max) * max > res) {
59  //Slow test to handle the biased corner
60  return res % max;
61  }
62  } while (1);
63  }
64 
65  // randDouble() gives you doubles with 32 bits of randomness.
66  // randLongDouble() gives you doubles with ~53 bits of randomness
67  // *Open() gives values in [0,1)
68  // *Closed() gives values in [0,1]
69 
70  // HP-UX aCC won't let me define these as const double;
71  // const double foo = 5.0 reports error 481.
72 #define MTR_int_to_open (1.0/4294967296.0)
73 #define MTR_int_to_closed (1.0/4294967295.0)
74 #define MTR_AMult (67108864.0) // 2^(32-6)
75  // 9007199254740992 = 2^53, 9007199254740991 = 2^53 -1
76 #define MTR_53bits_to_open(a, b) ((a * MTR_AMult + b)/9007199254740992.0)
77 #define MTR_53bits_to_closed(a, b) ((a * MTR_AMult + b)/9007199254740991.0)
78 
79  inline double randDoubleOpen() { // in [0,1), 32 bits of randomness
80  return (double)randInt() * MTR_int_to_open;
81  }
82 
83  inline double randDoubleClosed() { // in [0,1], 32 bits of randomness
84  return (double)randInt() * MTR_int_to_closed;
85  }
86 
87  inline double randDouble() { // in [0,1), 32 bits of randomness
88  return randDoubleOpen();
89  }
90 
91  // casting a long long to double is unbelievably slow on pa2.0 aCC C.03.30
92  inline double randDoubleOpen53() { // in [0,1), 53 bits of randomness
93  uint32_t a = randInt() >> 5, b = randInt() >> 6;
94  return MTR_53bits_to_open(a, b);
95  }
96 
97  inline double randDoubleClosed53() { // in [0,1]
98  uint32_t a = randInt() >> 5, b = randInt() >> 6;
99  return MTR_53bits_to_closed(a, b);
100  }
101 
102  inline bool randBool() {
103  return (randInt() & 0x1) ? true : false;
104  }
105 
106  inline bool randBoolFast() {
107  if (boolShift == 0) {
109  boolShift = 64;
110  }
111  bool ret = boolState & 0x1;
112  boolState >>= 1;
113  --boolShift;
114  return ret;
115  }
116 
117  protected:
118  uint8_t boolShift;
119 
120  uint64_t boolState;
121  };
122 };
123 
124 #endif // LINTEL_RANDOM_BASE_HPP
#define MTR_53bits_to_closed(a, b)
Definition: RandomBase.hpp:77
void initArray(std::vector< uint32_t > seed_array)
Definition: RandomBase.hpp:31
#define MTR_int_to_closed
Definition: RandomBase.hpp:73
#define UINT32_MAX
Definition: RandomBase.hpp:8
uint32_t randInt()
Definition: RandomBase.hpp:35
unsigned long long randLongLong()
Definition: RandomBase.hpp:39
~RandomTempl()
Definition: RandomBase.hpp:25
Definition: MersenneTwisterRandom.hpp:18
uint64_t boolState
Definition: RandomBase.hpp:120
uint8_t boolShift
Definition: RandomBase.hpp:118
double randDouble()
Definition: RandomBase.hpp:87
double randDoubleOpen()
Definition: RandomBase.hpp:79
RandomTempl(uint32_t seed=0)
Definition: RandomBase.hpp:17
const T max(const T x, const T y)
Definition: w_minmax.h:45
#define MTR_int_to_open
Definition: RandomBase.hpp:72
bool randBoolFast()
Definition: RandomBase.hpp:106
uint32_t randIntUnbiased(uint32_t max)
Definition: RandomBase.hpp:52
void init(uint32_t seed)
Definition: RandomBase.hpp:27
Definition: RandomBase.hpp:14
RandomTempl(std::vector< uint32_t > seed_array)
Definition: RandomBase.hpp:21
#define MTR_53bits_to_open(a, b)
Definition: RandomBase.hpp:76
double randDoubleClosed()
Definition: RandomBase.hpp:83
double randDoubleClosed53()
Definition: RandomBase.hpp:97
double randDoubleOpen53()
Definition: RandomBase.hpp:92
uint32_t randInt(uint32_t max)
Definition: RandomBase.hpp:48
bool randBool()
Definition: RandomBase.hpp:102