Player12
An open source football management simulator
Loading...
Searching...
No Matches
world_rng.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <limits>
15#include <span>
16#include <utility>
17
18#include "model/gamedate.h"
19
29enum class RngDomain : std::uint64_t
30{
31 Generation = 1,
41 Staff,
44 Stories,
46};
47
57{
58 public:
59 using result_type = std::uint64_t;
60
62 explicit WorldRng(std::uint64_t seed);
63
65 static WorldRng stream(std::uint64_t world_seed, RngDomain domain,
66 std::uint64_t key_a = 0, std::uint64_t key_b = 0);
67
69 static double hashUniform(std::uint64_t world_seed, RngDomain domain,
70 std::uint64_t key_a, std::uint64_t key_b = 0);
71
72 static constexpr result_type min() { return 0; }
73 static constexpr result_type max()
74 {
75 return std::numeric_limits<result_type>::max();
76 }
77 result_type operator()() { return next(); }
78
80 std::uint64_t next();
81
83 double uniform01();
84
86 float uniform(float lo, float hi);
87
89 int uniformInt(int lo, int hi);
90
92 float normal(float mean, float stddev);
93
95 float lognormal(float median, float sigma);
96
98 bool chance(double p);
99
101 std::size_t weightedIndex(std::span<const float> weights);
102
104 template <typename T>
105 void shuffle(std::span<T> values)
106 {
107 for (std::size_t i = values.size(); i > 1; --i)
108 {
109 const auto j =
110 static_cast<std::size_t>(uniformInt(0, static_cast<int>(i - 1)));
111 std::swap(values[i - 1], values[j]);
112 }
113 }
114
115 private:
116 std::array<std::uint64_t, 4> state{};
117};
118
127{
129double log(double x);
131double exp(double x);
133double cosTwoPi(double turns);
134} // namespace PortableMath
135
143{
144template <typename Engine>
146 Engine::min() == 0 &&
147 (Engine::max() == std::numeric_limits<std::uint32_t>::max() ||
148 Engine::max() == std::numeric_limits<std::uint64_t>::max());
149
151template <FullRangeEngine Engine>
152std::uint32_t bits32(Engine& engine)
153{
154 if constexpr (Engine::max() == std::numeric_limits<std::uint32_t>::max())
155 return static_cast<std::uint32_t>(engine());
156 else
157 return static_cast<std::uint32_t>(static_cast<std::uint64_t>(engine()) >>
158 32U);
159}
160
162template <FullRangeEngine Engine>
163std::uint64_t bits64(Engine& engine)
164{
165 if constexpr (Engine::max() == std::numeric_limits<std::uint32_t>::max())
166 {
167 const auto high = static_cast<std::uint64_t>(engine());
168 return (high << 32U) | static_cast<std::uint64_t>(engine());
169 }
170 else
171 {
172 return static_cast<std::uint64_t>(engine());
173 }
174}
175
177float unitFloat(std::uint32_t bits);
179double unitDouble(std::uint64_t bits);
181float lerp(float lo, float hi, float unit);
182double lerp(double lo, double hi, double unit);
184double affine(double offset, double scale, double value);
186double standardNormal(double u1, double u2);
188int poissonFromUnit(double unit, double mean);
189
191template <FullRangeEngine Engine>
192float uniformReal(Engine& engine, float lo, float hi)
193{
194 return lerp(lo, hi, unitFloat(bits32(engine)));
195}
196
198template <FullRangeEngine Engine>
199double uniformReal(Engine& engine, double lo, double hi)
200{
201 return lerp(lo, hi, unitDouble(bits64(engine)));
202}
203
205template <FullRangeEngine Engine>
206std::uint64_t below(Engine& engine, std::uint64_t count)
207{
208 if (count <= 1) return 0;
209 // Rejection sampling: values under the threshold would favour the low
210 // residues.
211 const std::uint64_t threshold = (0U - count) % count;
212 std::uint64_t value = bits64(engine);
213 while (value < threshold) value = bits64(engine);
214 return value % count;
215}
216
218template <FullRangeEngine Engine>
219int uniformInt(Engine& engine, int lo, int hi)
220{
221 if (hi <= lo) return lo;
222 const auto count =
223 static_cast<std::uint64_t>(static_cast<std::int64_t>(hi) - lo) + 1U;
224 return static_cast<int>(static_cast<std::int64_t>(lo) +
225 static_cast<std::int64_t>(below(engine, count)));
226}
227
229template <FullRangeEngine Engine>
230double normal(Engine& engine, double mean, double stddev)
231{
232 // 1 - [0, 1) is in (0, 1], so the logarithm is always finite.
233 const double u1 = 1.0 - unitDouble(bits64(engine));
234 const double u2 = unitDouble(bits64(engine));
235 return affine(mean, stddev, standardNormal(u1, u2));
236}
237
239template <FullRangeEngine Engine>
240int poisson(Engine& engine, double mean)
241{
242 // A sum of independent Poisson counts is a Poisson count of the summed
243 // means, so large means are split into parts inversion handles well.
244 constexpr double PART = 64.0;
245 int count = 0;
246 while (mean > PART)
247 {
248 count += poissonFromUnit(unitDouble(bits64(engine)), PART);
249 mean -= PART;
250 }
251 return count + poissonFromUnit(unitDouble(bits64(engine)), mean);
252}
253} // namespace PortableRandom
254
256std::uint64_t mixHash(std::uint64_t a, std::uint64_t b);
257
259std::int32_t dayOrdinal(const GameDateValue& date);
260
262std::int32_t dateToInt(const GameDateValue& date);
263
265GameDateValue dateFromInt(std::int32_t value);
Deterministic, portable pseudo random generator (xoshiro256**).
Definition world_rng.h:57
int uniformInt(int lo, int hi)
Definition world_rng.cpp:296
static constexpr result_type max()
Definition world_rng.h:73
std::size_t weightedIndex(std::span< const float > weights)
Definition world_rng.cpp:327
double uniform01()
Definition world_rng.cpp:289
void shuffle(std::span< T > values)
Definition world_rng.h:105
std::uint64_t result_type
Definition world_rng.h:59
result_type operator()()
Definition world_rng.h:77
static WorldRng stream(std::uint64_t world_seed, RngDomain domain, std::uint64_t key_a=0, std::uint64_t key_b=0)
Definition world_rng.cpp:260
float uniform(float lo, float hi)
Definition world_rng.cpp:291
static constexpr result_type min()
Definition world_rng.h:72
float normal(float mean, float stddev)
Definition world_rng.cpp:309
std::array< std::uint64_t, 4 > state
Definition world_rng.h:116
float lognormal(float median, float sigma)
Definition world_rng.cpp:319
std::uint64_t next()
Definition world_rng.cpp:276
bool chance(double p)
Definition world_rng.cpp:325
static double hashUniform(std::uint64_t world_seed, RngDomain domain, std::uint64_t key_a, std::uint64_t key_b=0)
Definition world_rng.cpp:268
Definition world_rng.h:145
Definition world_rng.h:127
double cosTwoPi(double turns)
Definition world_rng.cpp:161
double exp(double x)
Definition world_rng.cpp:149
double log(double x)
Definition world_rng.cpp:125
Definition world_rng.h:143
float unitFloat(std::uint32_t bits)
Definition world_rng.cpp:196
float uniformReal(Engine &engine, float lo, float hi)
Definition world_rng.h:192
int uniformInt(Engine &engine, int lo, int hi)
Definition world_rng.h:219
int poissonFromUnit(double unit, double mean)
Definition world_rng.cpp:229
double unitDouble(std::uint64_t bits)
Definition world_rng.cpp:201
std::uint32_t bits32(Engine &engine)
Definition world_rng.h:152
float lerp(float lo, float hi, float unit)
Definition world_rng.cpp:203
std::uint64_t bits64(Engine &engine)
Definition world_rng.h:163
double affine(double offset, double scale, double value)
Definition world_rng.cpp:217
double standardNormal(double u1, double u2)
Definition world_rng.cpp:223
std::uint64_t below(Engine &engine, std::uint64_t count)
Definition world_rng.h:206
double normal(Engine &engine, double mean, double stddev)
Definition world_rng.h:230
int poisson(Engine &engine, double mean)
Definition world_rng.h:240
Represents a specific date in the game world.
Definition gamedate.h:20
std::uint64_t mixHash(std::uint64_t a, std::uint64_t b)
Definition world_rng.cpp:248
RngDomain
Independent random streams of the world simulation.
Definition world_rng.h:30
std::int32_t dateToInt(const GameDateValue &date)
Definition world_rng.cpp:356
GameDateValue dateFromInt(std::int32_t value)
Definition world_rng.cpp:362
std::int32_t dayOrdinal(const GameDateValue &date)
Definition world_rng.cpp:342