TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Random.h
Go to the documentation of this file.
1 
12 #ifndef G3D_Random_h
13 #define G3D_Random_h
14 
15 #include "G3D/platform.h"
16 #include "G3D/g3dmath.h"
17 #include "G3D/GMutex.h"
18 
19 namespace G3D {
20 
39 class Random {
40 protected:
41 
43  enum {
44  N = 624,
45  M = 397,
46  R = 31,
47  U = 11,
48  S = 7,
49  T = 15,
50  L = 18,
51  A = 0x9908B0DF,
52  B = 0x9D2C5680,
53  C = 0xEFC60000};
54 
59 
62 
64  int index;
65 
67 
70  virtual void generate();
71 
74  Random(void*);
75 
76 
77 private:
78 
79  Random& operator=(const Random&) {
80  alwaysAssertM(false,
81  "There is no copy constructor or assignment operator for Random because you "
82  "probably didn't actually want to copy the state--it would "
83  "be slow and duplicate the state of a pseudo-random sequence. Maybe you could "
84  "provide arguments to a member variable in the constructor, "
85  "or pass the Random by reference?");
86  return *this;
87  }
88 
89  Random(const Random& r) {
90  *this = r;
91  }
92 
93 public:
94 
99  Random(uint32 seed = 0xF018A4D2, bool threadsafe = true);
100 
101  virtual ~Random();
102 
103  virtual void reset(uint32 seed = 0xF018A4D2, bool threadsafe = true);
104 
107  virtual uint32 bits();
108 
110  virtual int integer(int min, int max);
111 
113  virtual inline float uniform(float low, float high) {
114  // We could compute the ratio in double precision here for
115  // about 1.5x slower performance and slightly better
116  // precision.
117  return low + (high - low) * ((float)bits() / (float)0xFFFFFFFFUL);
118  }
119 
121  virtual inline float uniform() {
122  // We could compute the ratio in double precision here for
123  // about 1.5x slower performance and slightly better
124  // precision.
125  const float norm = 1.0f / (float)0xFFFFFFFFUL;
126  return (float)bits() * norm;
127  }
128 
130  virtual float gaussian(float mean, float stdev);
131 
134  virtual void cosHemi(float& x, float& y, float& z);
135 
138  virtual void cosSphere(float& x, float& y, float& z);
139 
143  virtual void cosPowHemi(const float k, float& x, float& y, float& z);
144 
147  virtual void hemi(float& x, float& y, float& z);
148 
150  virtual void sphere(float& x, float& y, float& z);
151 
159  static Random& common();
160 };
161 
162 }
163 
164 #endif
virtual void generate()
Definition: Random.cpp:85
Definition: Random.h:52
Definition: Random.h:53
virtual uint32 bits()
Definition: Random.cpp:51
Definition: AABox.h:25
Spinlock lock
Definition: Random.h:58
Definition: Random.h:46
virtual void hemi(float &x, float &y, float &z)
Definition: Random.cpp:205
Definition: Random.h:50
virtual float uniform()
Definition: Random.h:121
T max(const T &x, const T &y)
Definition: g3dmath.h:320
virtual float uniform(float low, float high)
Definition: Random.h:113
int index
Definition: Random.h:64
T min(const T &x, const T &y)
Definition: g3dmath.h:305
virtual void cosHemi(float &x, float &y, float &z)
Definition: Random.cpp:168
Random(const Random &r)
Definition: Random.h:89
Definition: Random.h:49
virtual ~Random()
Definition: Random.cpp:45
virtual int integer(int min, int max)
Definition: Random.cpp:125
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
Definition: Random.h:45
A mutual exclusion lock that busy-waits when locking.
Definition: GMutex.h:36
Definition: Random.h:51
Definition: Random.h:47
Random(void *)
Definition: Random.cpp:22
virtual float gaussian(float mean, float stdev)
Definition: Random.cpp:139
Random & operator=(const Random &)
Definition: Random.h:79
virtual void sphere(float &x, float &y, float &z)
Definition: Random.cpp:211
Definition: Random.h:44
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
Definition: Random.h:48
virtual void reset(uint32 seed=0xF018A4D2, bool threadsafe=true)
Definition: Random.cpp:33
static Random & common()
Definition: Random.cpp:16
uint32 * state
Definition: Random.h:61
virtual void cosPowHemi(const float k, float &x, float &y, float &z)
Definition: Random.cpp:191
virtual void cosSphere(float &x, float &y, float &z)
Definition: Random.cpp:159
bool m_threadsafe
Definition: Random.h:66
Definition: Random.h:39