Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cRandom.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cRandom_H
8 #define _INC_cRandom_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cSingleton.h"
14 #include "IUnknown.h"
15 
16 namespace Gray
17 {
19  {
23  virtual HRESULT GetNoise(void* pData, size_t iSize) = 0;
24  };
25 
27  {
32 
33  public:
34  typedef UINT SEED_t;
35 
36  cRandomBase() noexcept
37  {
38  }
39  virtual ~cRandomBase()
40  {
41  }
42 
43  virtual void InitSeed(const void* pData, size_t iSize) = 0; // all implementations must support this.
44  void InitSeed(IRandomNoise* pSrc, size_t iSize);
45  void InitSeedDefault(size_t iSize = sizeof(int));
46  void InitSeedUns(UINT iSeed); // SEED_t
47 
48  virtual HRESULT GetNoise(void* pData, size_t iSize) override;
49 
50  // Type helpers.
51  virtual UINT get_RandUns(); // UINT_MAX // SEED_t
52  bool GetRandBool()
53  {
55  return GetRandUX(2) == 1 ;
56  }
57 
60  virtual UINT GetRandUX(UINT nScale); // get integer random number in desired interval. (Non inclusive)
61  int GetRandIRange(int iRangeLo, int iRangeHi); // output random int
62 
63  UNITTEST_FRIEND(cRandom);
64  };
65 
66  class GRAYCORE_LINK cRandomPerf : public IRandomNoise, public cSingleton < cRandomPerf >
67  {
70 
71  public:
72  cRandomPerf();
73  static void GRAYCALL GetNoisePerf(void* pData, size_t iSize);
74  virtual HRESULT GetNoise(void* pData, size_t iSize) override // fill array with random. return # filled.
75  {
76  GetNoisePerf(pData, iSize);
77  return (HRESULT)iSize;
78  }
79  };
80 
81  class GRAYCORE_LINK cRandomOS : public cRandomBase, public cSingleton < cRandomOS >
82  {
86  private:
87  virtual void InitSeed(const void* pData, size_t iSize)
88  {
89  // No way to seed this.
92  }
93 
94  public:
95  cRandomOS();
96  virtual ~cRandomOS();
97 
98  static HRESULT GRAYCALL GetNoiseOS(void* pData, size_t iSize);
99 
100  virtual HRESULT GetNoise(void* pData, size_t iSize) override; // fill array with random. return # filled.
101  virtual UINT get_RandUns(); // UINT_MAX
102 
104  };
105 
107  {
111 
112  public:
113  cMemBlock m_Src; // a block of 'random' test data.
114  size_t m_nOffset; // How far have we read in m_Src? recycle when at end ?
115 
116  public:
117  cRandomBlock(const void* pData, size_t nSize) noexcept
118  : m_Src(pData, nSize)
119  , m_nOffset(0)
120  {
121  }
122  virtual HRESULT GetNoise(void* pData, size_t len) override // IRandomNoise
123  {
125  if (m_Src.get_Data() == nullptr)
126  {
127  // No m_Src supplied so fill with fixed data.
128  cMem::Fill(pData, len, 0x2a);
129  m_nOffset += len;
130  }
131  else
132  {
133  // todo repeat like cMem::CopyRepeat() ?
134  ::memcpy(pData, m_Src.GetOffset(m_nOffset), len);
135  m_nOffset += len;
136  ASSERT(m_Src.IsValidIndex2(m_nOffset)); // Don't overflow!
137  }
138  return (HRESULT)len;
139  }
140  };
141 
143  {
149 
150  public:
151  static const SEED_t k_RAND_MAX = 0x7fff;
152 
153  private:
154  SEED_t m_nSeed;
155 
156  public:
157  cRandomDef(SEED_t nSeed = 1);
158  virtual ~cRandomDef();
159 
160  SEED_t GetRandNext();
161 
162  virtual void InitSeed(const void* pData, size_t iSize) override; // Start a repeatable seeded series
163  virtual UINT GetRandUX(UINT nScale) override; // k_RAND_MAX is not always the same as UINT.
164  };
165 
167 };
168 #endif
#define IGNORE_WARN_INTERFACE(c)
Definition: GrayCore.h:79
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cMem.h:311
BYTE * GetOffset(size_t nOffset) const
Definition: cMem.h:427
bool IsValidIndex2(size_t i) const noexcept
Definition: cMem.h:382
void * get_Data() const noexcept
Definition: cMem.h:349
Definition: cRandom.h:27
virtual void InitSeed(const void *pData, size_t iSize)=0
bool GetRandBool()
Definition: cRandom.h:52
UINT SEED_t
default seed size might be 32 or 64 bit depending on k_RAND_MAX.
Definition: cRandom.h:34
virtual ~cRandomBase()
Definition: cRandom.h:39
cRandomBase() noexcept
Definition: cRandom.h:36
Definition: cRandom.h:107
virtual HRESULT GetNoise(void *pData, size_t len) override
fill array with random bytes. return # bytes filled.
Definition: cRandom.h:122
cMemBlock m_Src
Definition: cRandom.h:113
cRandomBlock(const void *pData, size_t nSize) noexcept
Definition: cRandom.h:117
size_t m_nOffset
Definition: cRandom.h:114
Definition: cRandom.h:143
Definition: cRandom.h:82
Definition: cRandom.h:67
virtual HRESULT GetNoise(void *pData, size_t iSize) override
fill array with random bytes. return # bytes filled.
Definition: cRandom.h:74
Definition: cSingleton.h:127
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
DECLARE_INTERFACE(IRandomNoise)
__DECL_IMPORT cRandomDef g_Rand
the global random number generator. NOT thread safe. but does that matter?
Definition: cRandom.cpp:15
Definition: cRandom.h:19
virtual HRESULT GetNoise(void *pData, size_t iSize)=0
fill array with random bytes. return # bytes filled.
static void Fill(void *pDst, size_t nSize, BYTE bVal) noexcept
Definition: cMem.h:174