10 #ifndef RANDOM_FUNC_HPP
11 #define RANDOM_FUNC_HPP
19 static constexpr uint32_t
ScaleToLimit(uint32_t value, uint32_t limit)
21 return ((uint64_t)value * (uint64_t)limit) >> 32;
73 uint32_t Random(
const std::source_location location = std::source_location::current());
75 inline uint32_t Random([[maybe_unused]]
const std::source_location location = std::source_location::current())
88 inline uint32_t
RandomRange(uint32_t limit,
const std::source_location location = std::source_location::current())
93 inline uint32_t InteractiveRandom()
98 inline uint32_t InteractiveRandomRange(uint32_t limit)
118 inline bool Chance16I(
const uint32_t a,
const uint32_t b,
const uint32_t r)
121 return (((uint16_t)r * b + b / 2) >> 16) < a;
134 inline bool Chance16(
const uint32_t a,
const uint32_t b,
const std::source_location location = std::source_location::current())
136 return Chance16I(a, b, Random(location));
154 inline bool Chance16R(
const uint32_t a,
const uint32_t b, uint32_t &r,
const std::source_location location = std::source_location::current())
156 r = Random(location);
Randomizer _random
Random used in the game state calculations.
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
static constexpr uint32_t ScaleToLimit(uint32_t value, uint32_t limit)
Scale a uint32_t number to be within the range [0,limit).
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
bool Chance16I(const uint32_t a, const uint32_t b, const uint32_t r)
Checks if a given randomize-number is below a given probability.
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
Randomizer _interactive_random
Random used everywhere else, where it does not (directly) influence the game state.
bool Chance16R(const uint32_t a, const uint32_t b, uint32_t &r, const std::source_location location=std::source_location::current())
Flips a coin with a given probability and saves the randomize-number in a variable.
void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
void SetRandomSeed(uint32_t seed)
(Re)set the state of the random number generators.
void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Structure to encapsulate the pseudo random number generators.
void SetSeed(uint32_t seed)
(Re)set the state of the random number generator.
uint32_t state[2]
The state of the randomizer.
uint32_t Next()
Generate the next pseudo random number.
uint32_t Next(uint32_t limit)
Generate the next pseudo random number scaled to limit, excluding limit itself.
Stores the state of all random number generators.