19#ifndef MTCORE_RANDOM_H
20#define MTCORE_RANDOM_H
127 ensure(
vtable->next1_0,
"MISSING next1_0 IN PRNG VTABLE");
137 ensure(
vtable->nextUint,
"MISSING nextUint IN PRNG VTABLE");
149 ensure(min < max,
"INVALID RANGE PROVIDED");
152 const u64 count = max - min;
158 const auto offset = std::numeric_limits<u64>::max() % count;
163 }
while (res >= (std::numeric_limits<u64>::max() - offset));
168 const long double v =
next1_0();
169 return static_cast<u64>(floor(v *
static_cast<long double>(count))) + min;
181 ensure(min < max,
"INVALID RANGE PROVIDED");
184 const auto count = max - min;
192 return v * count + min;
195 constexpr auto m = 10000000000;
197 const long double rng =
static_cast<long double>(n) /
static_cast<long double>(m);
198 return static_cast<f64>(rng * count) + min;
int SeedError
Seeding errors can vary differently, using an int to indicate the error code.
Mt19937_64Error
Errors that can happen when making a Mersenne Twister.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Prng lcg_init(uint64_t seed) noexcept
Initializes a Linear Congruential Generator NOTE: THIS IS NOT A GOOD PRNG (it's essentially rand())
Result< Prng, Mt19937_64Error > mt19937_64(Allocator *alloc, uint64_t seed) noexcept
Initializes a Mersenne Twister 19937-64 algorithm Note: This does require allocated memory.
Prng tinymt_init(uint64_t seed) noexcept
Initializes a PRNG based on the TinyMT variant of Mersenne Twister.
uint64_t u64
Alias for 64-bit unsigned ints.
uint8_t u8
Alias for 8-bit unsigned ints.
double f64
Alias for 64-bit floats.
Core library for C++ with Zig-related functionality.
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
u8 data[32]
Stack data, a 32-byte chunk of data for what a small PRNG may need How you use it is up to you If you...
V-Table for PRNG Designed for having a simple PRNG implementation.
double(* next1_0)(PrngState &self)
Optional Gets a double d, such that 0 <= d < 1.
Result< void, SeedError >(* seed)(PrngState &self, uint64_t seed)
Optional Re-seeds the PRNG.
uint64_t(* nextUint)(PrngState &self)
Optional Gets the next unsigned int.
void(* deinit)(PrngState &self)
Optional Deinitializes the PRNG and frees up any allocated state The PRNG is NOT safe to use after ca...
Represents a Pseudo Random Number Generator.
bool can_get_uint() const noexcept
Result< void, SeedError > seed(const u64 seed) noexcept
Reseeds the PRNG.
u64 next_uint() noexcept
Gets the next unsigned 64-bit integer from the PRNG Will abort of canGetUInt is false!
void deinit() noexcept
Deinitializes a PRNG NOTE: It is NOT safe to use a PRNG after it has been deinitialized!
u64 in_range(const u64 min, const u64 max) noexcept
Gets number between min (inclusive) and max value (exclusive) Prioritizes nextUInt over next1_0.
double next1_0() noexcept
Gets a number n such that $0 <= n < 1$ Will abort of canGet1_0 is false!
f64 in_range_f64(const f64 min, const f64 max) noexcept
Gets number between min (inclusive) and max value (exclusive) Prioritizes next1_0 over nextUInt.
bool can_reseed() const noexcept
bool can_get1_0() const noexcept
const PrngVTable * vtable
Represents a Result that may have an error (error code) or a success value A type of "void" means the...