summaryrefslogtreecommitdiffstats
path: root/src/FastRandom.h
blob: 797eacb6302862da54ae94b65a64f7b810ad28f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

// FastRandom.h

// Declares the cFastRandom class representing a fast random number generator

/*
The cFastRandom alias should be avoided in favor of the result of a call to GetRandomProvider().
The MTRand generator used is faster, has a better range and provides higher quality randomness.
Note that MTRand is relatively costly to construct and so instances should be long lived,
prefer calls to GetRandomProvider over creating new instances.
*/





#pragma once





namespace Detail
{
/** Returns a low quality seed. */
UInt32 GetRandomSeed();

/** Aliases true_type if Char is any variant of char ignoring signed-ness. */
template <class Char> using IsChar = typename std::is_same<typename std::make_signed<Char>::type, signed char>::type;

template <class IntType>
struct cUniformImpl : public std::conditional<
						  IsChar<IntType>::value,
						  typename std::conditional<  // Match signed-ness of IntType
							  std::is_signed<IntType>::value,
							  std::uniform_int_distribution<short>,
							  std::uniform_int_distribution<unsigned short>>::type,
						  std::uniform_int_distribution<IntType>>
{
};

/** uniform_int_distribution<char> is undefined so this aliases a valid type. */
template <class IntType> using cUniform = typename cUniformImpl<IntType>::type;
}  // namespace Detail





/** Class to wrap any random engine to provide a more convenient interface. */
template <class RandomEngine> class cRandomWrapper
{
  public:
	/** Initialize with a low quality seed. */
	cRandomWrapper() :
		m_Engine(Detail::GetRandomSeed())
	{
	}


	/** Initialize with a SeedSequence. */
	template <class SeedSeq>
	cRandomWrapper(SeedSeq & a_SeedSeq) :
		m_Engine(a_SeedSeq)
	{
	}



	/** Return a random IntType in the range [a_Min, a_Max]. */
	template <class IntType = int> IntType RandInt(IntType a_Min, IntType a_Max)
	{
		ASSERT(
			(a_Max >= a_Min) && (a_Max <= std::numeric_limits<IntType>::max()) &&
			(a_Min >= std::numeric_limits<IntType>::min())
		);
		Detail::cUniform<IntType> dist(static_cast<IntType>(a_Min), static_cast<IntType>(a_Max));
		return static_cast<IntType>(dist(m_Engine));
	}





	/** Return a random IntType in the range [0, a_Max]. */
	template <class IntType = int> IntType RandInt(IntType a_Max)
	{
		ASSERT((a_Max >= 0) && (a_Max <= std::numeric_limits<IntType>::max()));
		Detail::cUniform<IntType> dist(IntType(0), static_cast<IntType>(a_Max));
		return static_cast<IntType>(dist(m_Engine));
	}





	/** Return a random IntType in the range [0, std::numeric_limits<IntType>::max()]. */
	template <class IntType = int> IntType RandInt()
	{
		Detail::cUniform<IntType> dist(IntType(0), std::numeric_limits<IntType>::max());
		return static_cast<IntType>(dist(m_Engine));
	}





	/** Return a random RealType in the range [a_Min, a_Max). */
	template <class RealType = float> RealType RandReal(RealType a_Min, RealType a_Max)
	{
		std::uniform_real_distribution<RealType> dist(a_Min, a_Max);
		return dist(m_Engine);
	}





	/** Return a random RealType in the range [0, a_Max). */
	template <class RealType = float> RealType RandReal(RealType a_Max)
	{
		std::uniform_real_distribution<RealType> dist(RealType(0), a_Max);
		return dist(m_Engine);
	}





	/** Return a random RealType in the range [0, 1). */
	template <class RealType = float> RealType RandReal()
	{
		std::uniform_real_distribution<RealType> dist;
		return dist(m_Engine);
	}





	/** Return a random bool with the given probability of being true. */
	bool RandBool(double a_TrueProbability = 0.5)
	{
		std::bernoulli_distribution dist(a_TrueProbability);
		return dist(m_Engine);
	}




	/** Returns a reference to the underlying random engine. */
	RandomEngine & Engine() { return m_Engine; }

  private:
	RandomEngine m_Engine;
};





/** Utility to seed a random engine with maximal entropy from random_device. */
struct cRandomDeviceSeeder
{
	using result_type = std::random_device::result_type;

	template <class Itr> void generate(Itr first, Itr last)
	{
		std::random_device rd;
		std::uniform_int_distribution<result_type> dist;
		for (; first != last; ++first)
		{
			*first = dist(rd);
		}
	}
};





using cFastRandom = cRandomWrapper<std::minstd_rand>;
using MTRand = cRandomWrapper<std::mt19937>;

/** Returns the current thread's random number source. */
MTRand & GetRandomProvider();