62 explicit WorldRng(std::uint64_t seed);
66 std::uint64_t key_a = 0, std::uint64_t key_b = 0);
70 std::uint64_t key_a, std::uint64_t key_b = 0);
75 return std::numeric_limits<result_type>::max();
86 float uniform(
float lo,
float hi);
92 float normal(
float mean,
float stddev);
95 float lognormal(
float median,
float sigma);
104 template <
typename T>
107 for (std::size_t i = values.size(); i > 1; --i)
110 static_cast<std::size_t
>(
uniformInt(0,
static_cast<int>(i - 1)));
111 std::swap(values[i - 1], values[j]);
116 std::array<std::uint64_t, 4>
state{};
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());
151template <FullRangeEngine Engine>
154 if constexpr (Engine::max() == std::numeric_limits<std::uint32_t>::max())
155 return static_cast<std::uint32_t
>(engine());
157 return static_cast<std::uint32_t
>(
static_cast<std::uint64_t
>(engine()) >>
162template <FullRangeEngine Engine>
165 if constexpr (Engine::max() == std::numeric_limits<std::uint32_t>::max())
167 const auto high =
static_cast<std::uint64_t
>(engine());
168 return (high << 32U) |
static_cast<std::uint64_t
>(engine());
172 return static_cast<std::uint64_t
>(engine());
181float lerp(
float lo,
float hi,
float unit);
182double lerp(
double lo,
double hi,
double unit);
184double affine(
double offset,
double scale,
double value);
191template <FullRangeEngine Engine>
198template <FullRangeEngine Engine>
205template <FullRangeEngine Engine>
206std::uint64_t
below(Engine& engine, std::uint64_t count)
208 if (count <= 1)
return 0;
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;
218template <FullRangeEngine Engine>
221 if (hi <= lo)
return lo;
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)));
229template <FullRangeEngine Engine>
230double normal(Engine& engine,
double mean,
double stddev)
239template <FullRangeEngine Engine>
244 constexpr double PART = 64.0;
256std::uint64_t
mixHash(std::uint64_t a, std::uint64_t b);
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