-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPRNG.h
512 lines (456 loc) · 16.1 KB
/
PRNG.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
Copyright 2015 Matthew Leadbetter
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
If you have any queries I can be contacted by e-mail: [email protected]
*/
#ifndef PRNG_H
#define PRNG_H
#ifdef TEST
#define TEST_VIRTUAL virtual
#else
#define TEST_VIRTUAL
#endif
#include <mutex>
#include <random>
#include <array>
#include <cmath>
#include <cassert>
#include <limits>
class PRNG
{
public:
PRNG() : position(0)
{
static std::random_device rd;
static std::mutex rngMutex;
std::lock_guard<std::mutex> lock(rngMutex);
for(uint64_t &s : state)
{
// Unfortunately random_device returns unsigned ints rather than uint64_ts
if(std::numeric_limits<unsigned int>::digits >= 64)
{
s = rd();
}
else if(std::numeric_limits<unsigned int>::digits >= 32) {
s = rd() & 0xffffffff;
s |= static_cast<uint64_t>(rd()) << 32;
}
else // Not sure what hardware/compiler you're coding for but I guess it's in the standards
{
s = rd() & 0xffff;
s |= (static_cast<uint64_t>(rd()) << 16) & 0xffffffff;
s |= (static_cast<uint64_t>(rd()) << 32) & 0xffffffffffff;
s |= (static_cast<uint64_t>(rd()) << 48);
}
}
}
TEST_VIRTUAL ~PRNG() {}
/**
* @brief Constructs a new PRNG with a set seed
* @param seed - the value seed it should start with
*/
PRNG(const std::array<uint64_t, 16> &seed) : state(seed), position(0) {}
/**
* @brief Sets the internal state to a new seed.
*
* PRNG will produce the same pseudo-random results every time after being provided with a fixed seed.
*
* @param seed - the value to change the internal state to
*/
TEST_VIRTUAL void setSeed(const std::array<uint64_t, 16> &seed)
{
state = seed;
}
/**
* @brief Returns the internal state.
*
* You might want to do this to find out what the current state is so you can seed with it again
* to reproduce your results.
*
* @param seed - the value to change the internal state to
*/
TEST_VIRTUAL const std::array<uint64_t, 16> &getState() const
{
return state;
}
/**
* @brief Generates a random char
* @return a char containing a random number
*/
TEST_VIRTUAL char getRandomChar()
{
return static_cast<char>(xorshift1024());
}
/**
* @brief Generates a random unsigned char
* @return an unsigned char containing a random number
*/
TEST_VIRTUAL unsigned char getRandomUnsignedChar()
{
return static_cast<unsigned char>(xorshift1024());
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return a signed char containing a random number
*/
TEST_VIRTUAL char getRandomChar(const char &minValue, const char &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return an unsigned char containing a random number
*/
TEST_VIRTUAL unsigned char getRandomUnsignedChar(const unsigned char &maxValue)
{
return getRandomIntType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return an unsigned char containing a random number
*/
TEST_VIRTUAL unsigned char getRandomUnsignedChar(const unsigned char &minValue, const unsigned char &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random int
* @return an int containing a random number
*/
TEST_VIRTUAL int getRandomInt()
{
return static_cast<int>(xorshift1024());
}
/**
* @brief Generates a random unsigned int
* @return an unsigned int containing a random number
*/
TEST_VIRTUAL unsigned int getRandomUnsignedInt()
{
return static_cast<unsigned int>(xorshift1024());
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return a signed integer containing a random number
*/
TEST_VIRTUAL int getRandomInt(const int &minValue, const int &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return an unsigned integer containing a random number
*/
TEST_VIRTUAL unsigned int getRandomUnsignedInt(const unsigned int &maxValue)
{
return getRandomIntType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return an unsigned integer containing a random number
*/
TEST_VIRTUAL unsigned int getRandomUnsignedInt(const unsigned int &minValue, const unsigned int &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random long
* @return a long containing a random number
*/
TEST_VIRTUAL long getRandomLong()
{
return static_cast<long>(xorshift1024());
}
/**
* @brief Generates a random unsigned long
* @return an unsigned long containing a random number
*/
TEST_VIRTUAL unsigned long getRandomUnsignedLong()
{
return static_cast<unsigned long>(xorshift1024());
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return a signed long containing a random number
*/
TEST_VIRTUAL long getRandomLong(const long &minValue, const long &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return an unsigned long containing a random number
*/
TEST_VIRTUAL unsigned long getRandomUnsignedLong(const unsigned long &maxValue)
{
return getRandomIntType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return an unsigned long containing a random number
*/
TEST_VIRTUAL unsigned long getRandomUnsignedLong(const unsigned long &minValue, const unsigned long &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random long long
* @return a long long containing a random number
*/
TEST_VIRTUAL long long getRandomLongLong()
{
return static_cast<long long>(xorshift1024());
}
/**
* @brief Generates a random unsigned long long
* @return an unsigned long long containing a random number
*/
TEST_VIRTUAL unsigned long long getRandomUnsignedLongLong()
{
return static_cast<unsigned long long>(xorshift1024());
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return a signed long long containing a random number
*/
TEST_VIRTUAL long long getRandomLongLong(const long long &minValue, const long long &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return an unsigned long long containing a random number
*/
TEST_VIRTUAL unsigned long long getRandomUnsignedLongLong(const unsigned long long &maxValue)
{
return getRandomIntType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @throw cassert ensuring minValue < maxValue
* @return an unsigned long long containing a random number
*/
TEST_VIRTUAL unsigned long long getRandomUnsignedLongLong(const unsigned long long &minValue, const unsigned long long &maxValue)
{
return getRandomIntType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and 1
* @return a float containing a random number
*/
TEST_VIRTUAL float getRandomFloat()
{
return getRandomFloatType<float>();
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return a float containing a random number
*/
TEST_VIRTUAL float getRandomFloat(float maxValue)
{
return getRandomFloatType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @return a float containing a random number
*/
TEST_VIRTUAL float getRandomFloat(float minValue, float maxValue)
{
return getRandomFloatType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and 1
* @return a double containing a random number
*/
TEST_VIRTUAL double getRandomDouble()
{
return getRandomFloatType<double>();
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return a double containing a random number
*/
TEST_VIRTUAL double getRandomDouble(double maxValue)
{
return getRandomFloatType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @return a double containing a random number
*/
TEST_VIRTUAL double getRandomDouble(double minValue, double maxValue)
{
return getRandomFloatType(minValue, maxValue);
}
/**
* @brief Generates a random number between 0 and 1
* @return a long double containing a random number
*/
TEST_VIRTUAL long double getRandomLongDouble()
{
return getRandomFloatType<long double>();
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return a long double containing a random number
*/
TEST_VIRTUAL long double getRandomLongDouble(long double maxValue)
{
return getRandomFloatType(maxValue);
}
/**
* @brief Generates a random number between minValue and maxValue (inclusive)
* @param minValue - The lowest value this should return
* @param maxValue - The highest value this should return
* @return a long double containing a random number
*/
TEST_VIRTUAL long double getRandomLongDouble(long double minValue, long double maxValue)
{
return getRandomFloatType(minValue, maxValue);
}
/**
* @brief Generates a random uint64_t
* @return a uint64_t containing a random number
*/
TEST_VIRTUAL uint64_t getRandomUint64()
{
return xorshift1024();
}
/**
* @brief Generates a random number between 0 and maxValue (inclusive)
* @param maxValue - The highest value this should return
* @return a uint64_t containing a random number
*/
TEST_VIRTUAL uint64_t getRandomUint64(const uint64_t &maxValue)
{
assert(maxValue != 0);
int leadingZeros = countLeadingZeros64(maxValue);
uint64_t randomInt;
do
{
randomInt = xorshift1024() >> leadingZeros;
} while(randomInt > maxValue);
return randomInt;
}
private:
std::array<uint64_t, 16> state;
unsigned long position;
template <class T>
T getRandomIntType(const T &minValue, const T &maxValue)
{
assert(minValue < maxValue);
uint64_t range = static_cast<uint64_t>(maxValue - minValue);
T rand = static_cast<T>(getRandomUint64(range));
return rand + minValue;
}
template <class T>
T getRandomIntType(const T &maxValue)
{
assert(maxValue <= std::numeric_limits<uint64_t>::max());
return static_cast<T>(getRandomUint64(maxValue));
}
template <class T>
T getRandomFloatType(const T &minValue, const T &maxValue)
{
T range = std::abs(maxValue - minValue);
return getRandomFloatType<T>(range) + minValue;
}
template <class T>
T getRandomFloatType(const T &maxValue)
{
return getRandomFloatType<T>() * maxValue;
}
template <class T>
T getRandomFloatType()
{
constexpr T epsilon = std::numeric_limits<T>::epsilon();
constexpr int digitsToKeep = std::numeric_limits<T>::digits-1;
constexpr int digitsToLose = std::numeric_limits<uint64_t>::digits - digitsToKeep;
T rand = (xorshift1024() >> digitsToLose) * epsilon;
return rand;
}
uint64_t xorshift1024()
{
uint64_t state0 = state[position];
position = (position + 1) % 16;
uint64_t state1 = state[position];
state1 ^= state1 << 31;
state1 ^= state1 >> 11;
state0 ^= state0 >> 30;
state[position] = state0 ^ state1;
return state[position] * 1181783497276652981LL;
}
int countLeadingZeros64(const uint64_t &toCount)
{
#ifdef __GNUC__
return __builtin_clzll(toCount) - (std::numeric_limits<unsigned long long>::digits - 64);
#elif defined(_MSC_VER)
return __lzcnt(toCount);
#else
static const int clz64Index[64] = {
63, 16, 62, 7, 15, 36, 61, 3,
6, 14, 22, 26, 35, 47, 60, 2,
9, 5, 28, 11, 13, 21, 42, 19,
25, 31, 34, 40, 46, 52, 59, 1,
17, 8, 37, 4, 23, 27, 48, 10,
29, 12, 43, 20, 32, 41, 53, 18,
38, 24, 49, 30, 44, 33, 54, 39,
50, 45, 55, 51, 56, 57, 58, 0
};
uint64_t bitset = toCount;
bitset |= bitset >> 1;
bitset |= bitset >> 2;
bitset |= bitset >> 4;
bitset |= bitset >> 8;
bitset |= bitset >> 16;
bitset |= bitset >> 32;
return clz64Index[(bitset * 0x03f79d71b4cb0a89ULL) >> 58];
#endif
}
// You really shouldn't copy this class.
PRNG(PRNG const&) = delete;
};
#undef TEST_VIRTUAL
#endif // PRNG_H