Cuberite
A lightweight, fast and extensible game server for Minecraft
FastRandom.h
Go to the documentation of this file.
1 
2 // FastRandom.h
3 
4 // Declares the cFastRandom class representing a fast random number generator
5 
6 /*
7 The cFastRandom alias should be avoided in favor of the result of a call to GetRandomProvider().
8 The MTRand generator used is faster, has a better range and provides higher quality randomness.
9 Note that MTRand is relatively costly to construct and so instances should be long lived,
10 prefer calls to GetRandomProvider over creating new instances.
11 */
12 
13 
14 
15 
16 
17 #pragma once
18 
19 
20 
21 
22 
23 namespace Detail
24 {
27 
29  template <class Char>
30  using IsChar = typename std::is_same<typename std::make_signed<Char>::type, signed char>::type;
31 
32  template <class IntType>
33  struct cUniformImpl :
34  public std::conditional<
35  IsChar<IntType>::value,
36  typename std::conditional< // Match signed-ness of IntType
37  std::is_signed<IntType>::value,
38  std::uniform_int_distribution<short>,
39  std::uniform_int_distribution<unsigned short>
40  >::type,
41  std::uniform_int_distribution<IntType>
42  >
43  {
44  };
45 
47  template <class IntType>
49 }
50 
51 
52 
53 
54 
56 template <class RandomEngine>
58 {
59 public:
62  m_Engine(Detail::GetRandomSeed())
63  {
64  }
65 
66 
68  template <class SeedSeq>
69  cRandomWrapper(SeedSeq & a_SeedSeq):
70  m_Engine(a_SeedSeq)
71  {
72  }
73 
74 
75 
77  template <class IntType = int>
78  IntType RandInt(IntType a_Min, IntType a_Max)
79  {
80  ASSERT(
81  (a_Max >= a_Min) &&
82  (a_Max <= std::numeric_limits<IntType>::max()) &&
83  (a_Min >= std::numeric_limits<IntType>::min())
84  );
86  static_cast<IntType>(a_Min),
87  static_cast<IntType>(a_Max)
88  );
89  return static_cast<IntType>(dist(m_Engine));
90  }
91 
92 
93 
94 
95 
97  template <class IntType = int>
98  IntType RandInt(IntType a_Max)
99  {
100  ASSERT((a_Max >= 0) && (a_Max <= std::numeric_limits<IntType>::max()));
101  Detail::cUniform<IntType> dist(IntType(0), static_cast<IntType>(a_Max));
102  return static_cast<IntType>(dist(m_Engine));
103  }
104 
105 
106 
107 
108 
110  template <class IntType = int>
111  IntType RandInt()
112  {
113  Detail::cUniform<IntType> dist(IntType(0), std::numeric_limits<IntType>::max());
114  return static_cast<IntType>(dist(m_Engine));
115  }
116 
117 
118 
119 
120 
122  template <class RealType = float>
123  RealType RandReal(RealType a_Min, RealType a_Max)
124  {
125  std::uniform_real_distribution<RealType> dist(a_Min, a_Max);
126  return dist(m_Engine);
127  }
128 
129 
130 
131 
132 
134  template <class RealType = float>
135  RealType RandReal(RealType a_Max)
136  {
137  std::uniform_real_distribution<RealType> dist(RealType(0), a_Max);
138  return dist(m_Engine);
139  }
140 
141 
142 
143 
144 
146  template <class RealType = float>
147  RealType RandReal()
148  {
149  std::uniform_real_distribution<RealType> dist;
150  return dist(m_Engine);
151  }
152 
153 
154 
155 
156 
158  bool RandBool(double a_TrueProbability = 0.5)
159  {
160  std::bernoulli_distribution dist(a_TrueProbability);
161  return dist(m_Engine);
162  }
163 
164 
165 
166 
168  RandomEngine & Engine()
169  {
170  return m_Engine;
171  }
172 
173 private:
174 
175  RandomEngine m_Engine;
176 };
177 
178 
179 
180 
181 
184 {
185  using result_type = std::random_device::result_type;
186 
187  template <class Itr>
188  void generate(Itr first, Itr last)
189  {
190  std::random_device rd;
191  std::uniform_int_distribution<result_type> dist;
192  for (; first != last; ++first)
193  {
194  *first = dist(rd);
195  }
196  }
197 };
198 
199 
200 
201 
202 
205 
Class to wrap any random engine to provide a more convenient interface.
Definition: FastRandom.h:57
typename std::is_same< typename std::make_signed< Char >::type, signed char >::type IsChar
Aliases true_type if Char is any variant of char ignoring signed-ness.
Definition: FastRandom.h:30
RealType RandReal()
Return a random RealType in the range [0, 1).
Definition: FastRandom.h:147
Utility to seed a random engine with maximal entropy from random_device.
Definition: FastRandom.h:183
RealType RandReal(RealType a_Min, RealType a_Max)
Return a random RealType in the range [a_Min, a_Max).
Definition: FastRandom.h:123
IntType RandInt(IntType a_Min, IntType a_Max)
Return a random IntType in the range [a_Min, a_Max].
Definition: FastRandom.h:78
UInt32 GetRandomSeed()
Returns a low quality seed.
Definition: FastRandom.cpp:46
RealType RandReal(RealType a_Max)
Return a random RealType in the range [0, a_Max).
Definition: FastRandom.h:135
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
#define ASSERT(x)
Definition: Globals.h:335
void generate(Itr first, Itr last)
Definition: FastRandom.h:188
RandomEngine m_Engine
Definition: FastRandom.h:175
std::random_device::result_type result_type
Definition: FastRandom.h:185
cRandomWrapper(SeedSeq &a_SeedSeq)
Initialize with a SeedSequence.
Definition: FastRandom.h:69
bool RandBool(double a_TrueProbability=0.5)
Return a random bool with the given probability of being true.
Definition: FastRandom.h:158
unsigned int UInt32
Definition: Globals.h:113
RandomEngine & Engine()
Returns a reference to the underlying random engine.
Definition: FastRandom.h:168
typename cUniformImpl< IntType >::type cUniform
uniform_int_distribution<char> is undefined so this aliases a valid type.
Definition: FastRandom.h:48
IntType RandInt()
Return a random IntType in the range [0, std::numeric_limits<IntType>::max()].
Definition: FastRandom.h:111
IntType RandInt(IntType a_Max)
Return a random IntType in the range [0, a_Max].
Definition: FastRandom.h:98
cRandomWrapper()
Initialize with a low quality seed.
Definition: FastRandom.h:61