-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPcg.h
55 lines (43 loc) · 1.26 KB
/
Pcg.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
/*This bit of code was written by Jason Turner, without license as far as I can tell. You can find the video that this code is sourced from here: https://www.youtube.com/watch?v=rpn_5Mrrxf8*/
#ifndef PCG_H
#define PCG_H
#include <cstdint>
#include <numeric>
constexpr uint64_t seedPcg()
{
uint64_t shifted = 0;
for (const auto c: __TIME__)
{
shifted <<= 8;
shifted |= c;
}
return shifted;
}
struct PCG
{
struct pcg32_random_t { std::uint64_t state = 0; std::uint64_t inc = seedPcg(); };
pcg32_random_t rng;
typedef std::uint32_t result_type;
constexpr result_type operator()()
{
return pcg32_random_r();
}
static constexpr result_type min()
{
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max()
{
return std::numeric_limits<result_type>::max();
}
private:
constexpr std::uint32_t pcg32_random_r()
{
std::uint64_t oldState = rng.state;
rng.state = oldState * 6364136223846793005ULL + (rng.inc|1);
std::uint32_t xorshifted = ((oldState >> 18u) ^ oldState) >> 27u;
std::uint32_t rot = oldState >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
};
#endif // PCG_H