This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprng.h
62 lines (48 loc) · 1.56 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
#pragma once
#include <limits>
#include <cstdint>
namespace yzw2v {
namespace sampling {
class PRNG {
public:
using result_type = uint64_t;
explicit PRNG(const uint64_t state)
: state_{state}
{
}
uint64_t next() const noexcept {
return state_ * uint64_t{25214903917} + uint64_t{11};
}
uint64_t next(const uint32_t steps) const noexcept {
auto state = state_;
for (auto i = uint32_t{}; i < steps; ++i) {
state = state * uint64_t{25214903917} + uint64_t{11};
}
return state;
}
uint64_t operator()() noexcept {
return state_ = next();
}
double real_0_inc_1_inc() noexcept {
return (operator()() >> 11) * (1.0 / 9007199254740991.0);
}
double real_0_inc_1_exc() noexcept {
return (operator()() >> 11) * (1.0 / 9007199254740992.0);
}
static constexpr uint64_t min() noexcept {
return 0;
}
static constexpr uint64_t max() noexcept {
return std::numeric_limits<uint64_t>::max();
}
void discard(uint64_t n) noexcept {
for (; n; --n) {
// may be done in a more efficient manner
operator()();
}
}
private:
uint64_t state_;
};
}
}