Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <cstdint>
5#include <limits>
6
7namespace aegis2d::core {
8
16class Random {
17public:
19 using result_type = std::uint32_t;
20
25 constexpr explicit Random(std::uint64_t seed, std::uint64_t stream = 0) noexcept
26 : increment_{(stream << 1U) | 1U} {
27 next();
28 state_ += seed;
29 next();
30 }
31
34 [[nodiscard]] static constexpr result_type min() noexcept { return 0; }
35
38 [[nodiscard]] static constexpr result_type max() noexcept {
39 return std::numeric_limits<result_type>::max();
40 }
41
44 constexpr result_type next() noexcept {
45 const std::uint64_t old = state_;
46 state_ = (old * multiplier) + increment_;
47 const auto shifted = static_cast<std::uint32_t>(((old >> 18U) ^ old) >> 27U);
48 const auto rotation = static_cast<std::uint32_t>(old >> 59U);
49 return (shifted >> rotation) | (shifted << ((32U - rotation) & 31U));
50 }
51
53 constexpr result_type operator()() noexcept { return next(); }
54
58 constexpr std::uint32_t below(std::uint32_t bound) noexcept {
59 if (bound == 0) {
60 return 0;
61 }
62 // Lemire's method: multiply, and redraw the rare values that would favor some
63 // results.
64 const std::uint32_t threshold = (0U - bound) % bound;
65 while (true) {
66 const std::uint64_t product = std::uint64_t{next()} * bound;
67 if (static_cast<std::uint32_t>(product) >= threshold) {
68 return static_cast<std::uint32_t>(product >> 32U);
69 }
70 }
71 }
72
75 constexpr float uniform() noexcept {
76 return static_cast<float>(next() >> 8U) * (1.0F / static_cast<float>(1U << 24U));
77 }
78
84 constexpr float uniform(float low, float high) noexcept {
85 return low + ((high - low) * uniform());
86 }
87
88private:
89 static constexpr std::uint64_t multiplier = 6364136223846793005U;
90
91 std::uint64_t state_ = 0;
92 std::uint64_t increment_;
93};
94
95} // namespace aegis2d::core
Seeded pseudo-random generator (PCG32), for gameplay the server must replay.
Definition random.hpp:16
constexpr std::uint32_t below(std::uint32_t bound) noexcept
Whole number below a bound, every value equally likely.
Definition random.hpp:58
constexpr Random(std::uint64_t seed, std::uint64_t stream=0) noexcept
Starts a sequence.
Definition random.hpp:25
static constexpr result_type max() noexcept
Largest raw number.
Definition random.hpp:38
constexpr float uniform(float low, float high) noexcept
Real number between two bounds.
Definition random.hpp:84
static constexpr result_type min() noexcept
Smallest raw number.
Definition random.hpp:34
constexpr float uniform() noexcept
Real number between 0 and 1.
Definition random.hpp:75
constexpr result_type operator()() noexcept
Next raw number.
Definition random.hpp:53
constexpr result_type next() noexcept
Next raw number.
Definition random.hpp:44
std::uint32_t result_type
Type of the raw numbers.
Definition random.hpp:19
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11