-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
53 lines (43 loc) · 1.22 KB
/
random.cpp
File metadata and controls
53 lines (43 loc) · 1.22 KB
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
#include "random.hpp"
namespace TerraNova {
namespace Random {
// anonymous namespace containing PRNG variables
namespace {
uint64_t boolSources[2];
uint64_t currentBoolSource;
uint64_t currentBoolBit;
std::mt19937 intGenerator;
}
void Initialize() {
std::random_device randomDevice;
// bools
boolSources[0] = (uint64_t(randomDevice()) << 32) ^ randomDevice();
boolSources[1] = (uint64_t(randomDevice()) << 32) ^ randomDevice();
currentBoolSource = 0; // this is irrelevant because of the next line
currentBoolBit = 63; // the generator is reset when first called
// ints
intGenerator = std::mt19937(randomDevice());
}
uint64_t XorShiftPlus(uint64_t& source1, uint64_t& source2) {
uint64_t x = source1;
const uint64_t y = source2;
source1 = y;
x ^= x << 23;
source2 = x ^ y ^ (x >> 17) ^ (y >> 26);
return source2 + y;
}
bool Bool() {
if(currentBoolBit >= 63){
currentBoolSource = XorShiftPlus(boolSources[0], boolSources[1]);
currentBoolBit = 0;
} else {
++currentBoolBit;
}
return currentBoolSource & (1 << currentBoolBit);
}
int Int(const int min, const int max) {
std::uniform_int_distribution<int> distribution(min, max);
return distribution(intGenerator);
}
} // namespace Random
} // namespace TerraNova