Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cRandomFloat.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cRandomFloat_H
8 #define _INC_cRandomFloat_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "../GrayLibBase.h"
15 
16 namespace GrayLib
17 {
19 
21  {
26 
27  public:
28  virtual double get_RandDouble(); // 0 to < 1.0
29  virtual float get_RandFloat(); // 0 to < 1.0
30 
31  double GetRandDX(double nScale)
32  {
35  if (nScale <= 0)
36  return 0;
37  return get_RandDouble() * nScale;
38  }
39  float GetRandFX(float nScale)
40  {
43  if (nScale <= 0)
44  return 0;
45  return get_RandFloat() * nScale;
46  }
47 
48  float GetRandFRange(float fRangeLo, float fRangeHi); // output random float
49 
50  // implement shaped random distributions.
51  // int GetBellInt( int iCenter, int iVariance25 );
52  // int GetParabolicInt( int iVariance25 );
53  // int GetHyperbolicInt( int iVariance25 );
54 
56  };
57 
59  {
63 
64  cRandomDef& m_def;
65  public:
66  cRandomFloatDef(cRandomDef& def) noexcept
67  : m_def(def)
68  {
69  }
70 
71  virtual void InitSeed(const void* pData, size_t iSize) override; // Start a repeatable seeded series
72  virtual UINT GetRandUX(UINT nScale) override; // cRandomDef::k_RAND_MAX
73  };
74 
76  {
85 #if 1
86  // define constants for MT11213A
87  static const int k_N = 351;
88  static const int k_M = 175;
89  static const int k_R = 19;
90  static const int k_TEML = 17;
91 
92  static const DWORD k_RANDOM_MATRIX_A = 0xE4BD75F5;
93  static const DWORD k_RANDOM_TEMB = 0x655E5280;
94  static const DWORD k_RANDOM_TEMC = 0xFFD58000;
95 #else
96  // or constants for MT19937
97  static const int k_N = 624;
98  static const int k_M = 397;
99  static const int k_R = 31;
100  static const int k_TEML = 18;
101 
102  static const DWORD k_RANDOM_MATRIX_A = 0x9908B0DF;
103  static const DWORD k_RANDOM_TEMB = 0x9D2C5680;
104  static const DWORD k_RANDOM_TEMC = 0xEFC60000;
105 #endif
106  static const int k_TEMU = 11;
107  static const int k_TEMS = 7;
108  static const int k_TEMT = 15;
109 
110  private:
111  UINT m_mt[k_N];
112  int m_mti;
113 
114  public:
115  cRandomMersenne(const void* pData, size_t iSize);
117  {}
118 
119  virtual void InitSeed(const void* pData, size_t iSize) override; // re-seed
120  virtual UINT get_RandUns() override; // output random bits
121  };
122 
124  {
128 
129  static const int k_KK = 17;
130  static const int k_JJ = 10;
131  static const int k_R1 = 13;
132  static const int k_R2 = 9;
133 
134  protected:
135  int m_p1;
136  int m_p2;
137  UINT m_randbuffer[k_KK];
138 
139  public:
140  cRandomBGenerator(const void* pData, size_t iSize); // constructor
142  {}
143 
144  virtual void InitSeed(const void* pData, size_t iSize) override;
145  virtual UINT get_RandUns() override;
146  };
147 
148 #ifdef USE_LONG_DOUBLE
149  typedef long double LONG_DOUBLE;
150 #else
151  typedef double LONG_DOUBLE;
152 #endif
153 
155  {
158 
159  static const int k_KK = 17;
160  static const int k_JJ = 10;
161  static const int k_R1 = 19;
162  static const int k_R2 = 27;
163 
164  protected:
165  int m_p1;
166  int m_p2;
167  union
168  {
170  UINT m_randbits[3];
171  } m_u;
172  UINT m_randbuffer[k_KK][2];
173 
174  public:
175  cRandomWGenerator(const void* pData, size_t iSize); // constructor
177  {}
178 
179  virtual void InitSeed(const void* pData, size_t iSize) override;
180  virtual UINT get_RandUns() override;
181  };
182 
183 #ifdef USE_LONG_DOUBLE
184 
185  class GRAYLIB_LINK cRandomMotherOfAll : public cRandomFloat
186  {
190  protected:
191  double m_x[5];
192  public:
193  cRandomMotherOfAll(const void* pData, size_t iSize); // constructor
194  virtual ~cRandomMotherOfAll()
195  {}
196 
197  virtual void InitSeed(const void* pData, size_t iSize) override;
198  virtual double get_RandDouble() override;
199  virtual UINT GetRandUX(UINT nScale) override;
200  };
201 
202 #endif
203 
204  extern GRAYLIB_LINK cRandomFloatDef g_RandFloat;
205 };
206 #endif // _INC_cRandom_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cRandomFloat.h:124
virtual ~cRandomBGenerator()
Definition: cRandomFloat.h:141
int m_p2
indexes into buffer
Definition: cRandomFloat.h:136
int m_p1
Definition: cRandomFloat.h:135
Definition: cRandomFloat.h:59
cRandomFloatDef(cRandomDef &def) noexcept
Definition: cRandomFloat.h:66
Definition: cRandomFloat.h:21
double GetRandDX(double nScale)
Definition: cRandomFloat.h:31
UNITTEST_FRIEND(cRandomFloat)
float GetRandFX(float nScale)
Definition: cRandomFloat.h:39
Definition: cRandomFloat.h:76
virtual ~cRandomMersenne()
Definition: cRandomFloat.h:116
Definition: cRandomFloat.h:155
LONG_DOUBLE m_randp1
< used for conversion to float
Definition: cRandomFloat.h:169
virtual ~cRandomWGenerator()
Definition: cRandomFloat.h:176
int m_p2
indexes into buffer
Definition: cRandomFloat.h:166
int m_p1
Definition: cRandomFloat.h:165
Definition: cRandom.h:27
Definition: cRandom.h:143
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
__DECL_IMPORT cRandomFloatDef g_RandFloat
the global default floating point random number generator.
double LONG_DOUBLE
use double precision
Definition: cRandomFloat.h:151