GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
randomness.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_RANDOMNESS_HPP
5#define GREM_CORE_RANDOMNESS_HPP
6
8
12
13#include <istream> // std::basic_istream
14#include <limits> // std::numeric_limits
15#include <ostream> // std::basic_ostream
16#include <random> // std::random_device, std::..._distribution, std::generate_canonical
17
18namespace grem::randomness {
19
35public:
36 using result_type = uint64_t;
37
38 static constexpr result_type default_seed = 0;
39
40 [[nodiscard]] static constexpr result_type min() {
41 return 0;
42 }
43
44 [[nodiscard]] static constexpr result_type max() {
46 }
47
48 constexpr SplitMix64Engine() noexcept
50
51 constexpr explicit SplitMix64Engine(result_type seedValue) noexcept
52 : state(seedValue) {}
53
54 constexpr void seed(result_type seedValue = default_seed) noexcept {
55 state = seedValue;
56 }
57
58 constexpr result_type operator()() noexcept {
59 state += 0x9E3779B97F4A7C15ull;
60 uint64_t z = state;
61 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
62 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
63 return z ^ (z >> 31);
64 }
65
66 constexpr void discard(unsigned long long z) noexcept {
67 while (z-- > 0) {
68 (*this)();
69 }
70 }
71
72 [[nodiscard]] constexpr bool operator==(const SplitMix64Engine& other) const noexcept {
73 return state == other.state;
74 }
75
76 template <typename CharT, typename Traits>
77 friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& stream, const SplitMix64Engine& engine) {
78 return stream << engine.state;
79 }
80
81 template <typename CharT, typename Traits>
82 friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& stream, SplitMix64Engine& engine) {
83 return stream >> engine.state;
84 }
85
86private:
87 uint64_t state;
88};
89
105public:
106 using result_type = uint64_t;
108
110
111 [[nodiscard]] static constexpr result_type min() {
112 return 0;
113 }
114
115 [[nodiscard]] static constexpr result_type max() {
117 }
118
121
122 constexpr explicit Xoroshiro128PlusPlusEngine(result_type seedValue) noexcept {
123 seed(seedValue);
124 }
125
126 constexpr explicit Xoroshiro128PlusPlusEngine(const State& state) noexcept
127 : state(state) {}
128
129 constexpr void seed(result_type seedValue = default_seed) noexcept {
130 SplitMix64Engine stateGenerator{seedValue};
131 setState({stateGenerator(), stateGenerator()});
132 }
133
134 constexpr result_type operator()() noexcept {
135 const uint64_t s0 = state[0];
136 uint64_t s1 = state[1];
137 const uint64_t result = rotateBitsLeft(s0 + s1, 17) + s0;
138 s1 ^= s0;
139 state[0] = rotateBitsLeft(s0, 49) ^ s1 ^ (s1 << 21);
140 state[1] = rotateBitsLeft(s1, 28);
141 return result;
142 }
143
144 constexpr void discard(unsigned long long z) noexcept {
145 while (z-- > 0) {
146 (*this)();
147 }
148 }
149
153 constexpr void jump() noexcept {
154 uint64_t s0 = 0;
155 uint64_t s1 = 0;
156 for (const uint64_t c : {0x2BD7A6A6E99C2DDCull, 0x0992CCAF6A6FCA05ull}) {
157 for (int b = 0; b < 64; ++b) {
158 if ((c & (uint64_t{1} << b)) != 0) {
159 s0 ^= state[0];
160 s1 ^= state[1];
161 }
162 (*this)();
163 }
164 }
165 state[0] = s0;
166 state[1] = s1;
167 }
168
172 constexpr void longJump() noexcept {
173 uint64_t s0 = 0;
174 uint64_t s1 = 0;
175 for (const uint64_t c : {0x360FD5F2CF8D5D99ull, 0x9C6E6877736C46E3ull}) {
176 for (int b = 0; b < 64; ++b) {
177 if ((c & (uint64_t{1} << b)) != 0) {
178 s0 ^= state[0];
179 s1 ^= state[1];
180 }
181 (*this)();
182 }
183 }
184 state[0] = s0;
185 state[1] = s1;
186 }
187
197 constexpr void setState(const State& newState) noexcept {
198 state = newState;
199 }
200
210 [[nodiscard]] constexpr State getState() const noexcept {
211 return state;
212 }
213
214 [[nodiscard]] constexpr bool operator==(const Xoroshiro128PlusPlusEngine& other) const noexcept {
215 return state == other.state;
216 }
217
218 template <typename CharT, typename Traits>
219 friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& stream, const Xoroshiro128PlusPlusEngine& engine) {
220 return stream << engine.state[0] << ' ' << engine.state[1];
221 }
222
223 template <typename CharT, typename Traits>
224 friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& stream, Xoroshiro128PlusPlusEngine& engine) {
225 return stream >> engine.state[0] >> engine.state[1];
226 }
227
228private:
229 State state{};
230};
231
240using NonDeterministicRandomEngine = std::random_device;
241
252
267template <typename Integer>
268using UniformIntegerDistribution = std::uniform_int_distribution<Integer>;
269
283template <typename Float>
284using UniformRealDistribution = std::uniform_real_distribution<Float>;
285
290using BernoulliDistribution = std::bernoulli_distribution;
291
307template <typename Integer>
308using BinomialDistribution = std::binomial_distribution<Integer>;
309
325template <typename Integer>
326using NegativeBinomialDistribution = std::negative_binomial_distribution<Integer>;
327
346template <typename Integer>
347using GeometricDistribution = std::geometric_distribution<Integer>;
348
365template <typename Integer>
366using PoissonDistribution = std::poisson_distribution<Integer>;
367
381template <typename Float>
382using ExponentialDistribution = std::exponential_distribution<Float>;
383
394template <typename Float>
395using NormalDistribution = std::normal_distribution<Float>;
396
412template <typename Integer>
413using DiscreteDistribution = std::discrete_distribution<Integer>;
414
430template <floating_point T, size_t EntropyBits = std::numeric_limits<T>::digits>
431[[nodiscard]] inline T generateCanonical(auto& generator) {
432 return std::generate_canonical<T, EntropyBits>(generator);
433}
434
435} // namespace grem::randomness
436
437#endif
Implementation of the SplitMix64 pseudorandom number generator that provides the API required for a s...
Definition randomness.hpp:34
constexpr SplitMix64Engine() noexcept
Definition randomness.hpp:48
constexpr void discard(unsigned long long z) noexcept
Definition randomness.hpp:66
static constexpr result_type min()
Definition randomness.hpp:40
constexpr bool operator==(const SplitMix64Engine &other) const noexcept
Definition randomness.hpp:72
uint64_t result_type
Definition randomness.hpp:36
static constexpr result_type max()
Definition randomness.hpp:44
static constexpr result_type default_seed
Definition randomness.hpp:38
constexpr result_type operator()() noexcept
Definition randomness.hpp:58
constexpr SplitMix64Engine(result_type seedValue) noexcept
Definition randomness.hpp:51
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &stream, SplitMix64Engine &engine)
Definition randomness.hpp:82
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &stream, const SplitMix64Engine &engine)
Definition randomness.hpp:77
constexpr void seed(result_type seedValue=default_seed) noexcept
Definition randomness.hpp:54
Implementation of the xoroshiro128++ pseudorandom number generator that provides the API required for...
Definition randomness.hpp:104
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &stream, Xoroshiro128PlusPlusEngine &engine)
Definition randomness.hpp:224
constexpr State getState() const noexcept
Get the value representation of the internal state.
Definition randomness.hpp:210
constexpr Xoroshiro128PlusPlusEngine() noexcept
Definition randomness.hpp:119
constexpr bool operator==(const Xoroshiro128PlusPlusEngine &other) const noexcept
Definition randomness.hpp:214
static constexpr result_type max()
Definition randomness.hpp:115
uint64_t result_type
Definition randomness.hpp:106
Array< uint64_t, 2 > State
Definition randomness.hpp:107
static constexpr result_type default_seed
Definition randomness.hpp:109
constexpr void longJump() noexcept
Advance the internal state 2^96 times.
Definition randomness.hpp:172
constexpr result_type operator()() noexcept
Definition randomness.hpp:134
static constexpr result_type min()
Definition randomness.hpp:111
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &stream, const Xoroshiro128PlusPlusEngine &engine)
Definition randomness.hpp:219
constexpr void seed(result_type seedValue=default_seed) noexcept
Definition randomness.hpp:129
constexpr void jump() noexcept
Advance the internal state 2^64 times.
Definition randomness.hpp:153
constexpr void discard(unsigned long long z) noexcept
Definition randomness.hpp:144
constexpr Xoroshiro128PlusPlusEngine(result_type seedValue) noexcept
Definition randomness.hpp:122
constexpr void setState(const State &newState) noexcept
Set the value representation of the internal state.
Definition randomness.hpp:197
constexpr Xoroshiro128PlusPlusEngine(const State &state) noexcept
Definition randomness.hpp:126
Definition aliases.hpp:19
T generateCanonical(auto &generator)
Generate a random floating-point number uniformly distributed on the half-open interval [0,...
Definition randomness.hpp:431
std::random_device NonDeterministicRandomEngine
Uniformly-destributed random number generator that provides non-deterministic random numbers if suppo...
Definition randomness.hpp:240
std::uniform_real_distribution< Float > UniformRealDistribution
Random number distribution that produces floating-point values uniformly distributed on the half-open...
Definition randomness.hpp:284
std::binomial_distribution< Integer > BinomialDistribution
Random number distribution that produces non-negative integer values that correspond to the number of...
Definition randomness.hpp:308
std::exponential_distribution< Float > ExponentialDistribution
Random number distribution that produces non-negative floating-point values that correspond to the ti...
Definition randomness.hpp:382
std::normal_distribution< Float > NormalDistribution
Random number distribution that produces floating-point values according to a Normal/Gaussian distrib...
Definition randomness.hpp:395
std::negative_binomial_distribution< Integer > NegativeBinomialDistribution
Random number distribution that produces non-negative integer values that correspond to the number of...
Definition randomness.hpp:326
std::geometric_distribution< Integer > GeometricDistribution
Random number distribution that produces non-negative integer values that correspond to the number of...
Definition randomness.hpp:347
Xoroshiro128PlusPlusEngine DefaultRandomEngine
Default pseudorandom number generator.
Definition randomness.hpp:251
std::poisson_distribution< Integer > PoissonDistribution
Random number distribution that produces non-negative integer values that correspond to the number of...
Definition randomness.hpp:366
std::discrete_distribution< Integer > DiscreteDistribution
Random number distribution that, given a list of weights w, produces integer values in the interval [...
Definition randomness.hpp:413
std::uniform_int_distribution< Integer > UniformIntegerDistribution
Random number distribution that produces integer values uniformly distributed on the closed interval ...
Definition randomness.hpp:268
std::bernoulli_distribution BernoulliDistribution
Random number distribution that produces boolean values with probability p of being true.
Definition randomness.hpp:290
GREM_ALWAYS_INLINE constexpr UnsignedInteger rotateBitsLeft(UnsignedInteger value, int shift) noexcept
Get the result of bitwise left-rotating a value by a number of bits.
Definition fundamentals.hpp:645
Definition Array.hpp:20
static constexpr T MAX
Definition fundamentals.hpp:29