-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_romu_64
138 lines (114 loc) · 5.82 KB
/
random_romu_64
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_RANDOM_RANDOM_ROMU_64_HPP
#define GTL_RANDOM_RANDOM_ROMU_64_HPP
// Summary: Romu 64 bit pseudo-random number generator.
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the random_romu_64 is misused.
# define GTL_RANDOM_ROMU_64_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_RANDOM_ROMU_64_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
namespace gtl {
class random_romu_64 final {
private:
unsigned long long int state[4];
private:
constexpr static unsigned long long int rotate_left(unsigned long long int value, unsigned long long int shift) {
constexpr const unsigned long long int mask = (8 * sizeof(unsigned long long int) - 1);
shift &= mask;
return (value << shift) | (value >> ((static_cast<unsigned long long int>(-static_cast<signed long long int>(shift))) & mask));
}
public:
/// @brief Defaulted destructor.
~random_romu_64() = default;
/// @brief Empty constructor.
random_romu_64()
: state{ 0x853C49E6748FEA9Bull, 0xDA3E39CB94B95BDBull, 0xC1F651C67C62C6E0ull, 0xBF58476D1CE4E5B9ull } {
}
/// @brief Defaulted copy constructor.
random_romu_64(const random_romu_64&) = default;
/// @brief Defaulted move constructor.
random_romu_64(random_romu_64&&) = default;
/// @brief Defaulted copy assignment.
random_romu_64& operator=(const random_romu_64&) = default;
/// @brief Defaulted move assignment.
random_romu_64& operator=(random_romu_64&&) = default;
/// @brief Constructor with 64 bit seed.
/// @param seed_value Seed value used to initialise the state.
random_romu_64(unsigned long long int seed_value) {
this->seed(seed_value);
}
/// @brief Initialise the state from a 64 bit seed.
/// @param seed_value Seed value used to initialise the state.
void seed(unsigned long long int seed_value) {
this->state[0] = seed_value ^ 0x5555555555555555ull;
this->state[1] = seed_value ^ 0x3333333333333333ull;
this->state[2] = seed_value ^ 0x0F0F0F0F0F0F0F0Full;
this->state[3] = seed_value ^ 0x00FF00FF00FF00FFull;
}
/// @brief Get the next random number from the generator.
/// @return A pseudo-random number.
unsigned long long int get_random_raw() {
const unsigned long long int wp = this->state[0];
const unsigned long long int xp = this->state[1];
const unsigned long long int yp = this->state[2];
const unsigned long long int zp = this->state[3];
this->state[0] = 15241094284759029579ull * zp;
this->state[1] = zp + rotate_left(wp, 52);
this->state[2] = yp - xp;
this->state[3] = yp + wp;
this->state[3] = rotate_left(this->state[3], 19);
return xp;
}
public:
/// @brief Get a random number in the open interval 0 < value < 1.
/// @return A pseudo-random number.
double get_random_exclusive() {
return (static_cast<double>(this->get_random_raw()) + 0.5) * (0.5 / static_cast<double>(1ull << 63));
}
/// @brief Get a random number in the half-open interval 0 <= value < 1.
/// @return A pseudo-random number.
double get_random_exclusive_top() {
return static_cast<double>(this->get_random_raw()) * (0.5 / static_cast<double>(1ull << 63));
}
/// @brief Get a random number in the closed interval 0 <= value <= 1.
/// @return A pseudo-random number.
double get_random_inclusive() {
return static_cast<double>(this->get_random_raw()) * (0.5 / static_cast<double>((1ull << 63) - 1));
}
/// @brief Get a random number between two bounds.
/// @param inclusive_min Minimum number that can be returned.
/// @param inclusive_max Max number than can be returned.
/// @return A pseudo-random number.
unsigned long long int get_random(unsigned long long int inclusive_min, unsigned long long int inclusive_max) {
GTL_RANDOM_ROMU_64_ASSERT(inclusive_min < inclusive_max, "Minimum bound must be lower than maximum bound.");
return (this->get_random_raw() % (1 + inclusive_max - inclusive_min)) + inclusive_min;
}
/// @brief Get a random number between two bounds.
/// @param inclusive_min Minimum number that can be returned.
/// @param inclusive_max Max number than can be returned.
/// @return A pseudo-random number.
double get_random(double inclusive_min, double inclusive_max) {
GTL_RANDOM_ROMU_64_ASSERT(inclusive_min < inclusive_max, "Minimum bound must be lower than maximum bound.");
return (this->get_random_inclusive() * (inclusive_max - inclusive_min)) + inclusive_min;
}
};
}
#undef GTL_RANDOM_ROMU_64_ASSERT
#endif // GTL_RANDOM_RANDOM_ROMU_64_HPP