-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandomness.h
86 lines (75 loc) · 2.09 KB
/
randomness.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
#pragma once
#include <random>
/**
* A unified RNG capable of generating random floating-point and integer
* values. Not thread-safe; use a separate randomness object for each thread.
*/
class Randomness {
/**
* Uniform [0, 1) floating-point distribution.
*/
std::uniform_real_distribution<float> unitDist;
/**
* Uniform distribution over all possible int's.
*/
std::uniform_int_distribution<int> intDist;
/**
* Uniform distribution over all possible unsigned's.
*/
std::uniform_int_distribution<unsigned> unsignedDist;
/**
* Standard Gaussian (normal) distribution.
*/
std::normal_distribution<float> normalDist;
/**
* The engine used internally for the RNG.
*/
std::mt19937 rng;
/**
* Returns a seed based on true device randomness.
*/
static inline unsigned createSeed() {
std::uniform_int_distribution<unsigned> seedDist;
std::random_device randDevice;
return seedDist(randDevice);
}
public:
/**
* Constructs a randomness object from a truly random seed.
*/
Randomness()
: unitDist(), intDist(), unsignedDist(), normalDist(), rng(createSeed()) {}
/**
* Constructs a randomness object from the given seed.
*/
Randomness(unsigned seed)
: unitDist(), intDist(), unsignedDist(), rng(seed) {}
/**
* Samples a random int.
*/
inline int nextInt() { return intDist(rng); }
/**
* Samples a random unsigned.
*/
inline unsigned nextUnsigned() { return unsignedDist(rng); }
/**
* Samples a random float between 0 (inclusive) and 1 (exclusive).
*/
inline float nextUnitFloat() { return unitDist(rng); }
/**
* Samples a random float between 0 (inclusive) and max (exclusive).
*/
inline float nextFloat(float max) {
return max * unitDist(rng);
}
/**
* Samples a random float between min (inclusive) and max (exclusive).
*/
inline float nextFloat(float min, float max) {
return min + (max - min) * unitDist(rng);
}
/**
* Samples a normally-distributed float with mean 0 and standard deviation 1.
*/
inline float nextNormalFloat() { return normalDist(rng); }
};