MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Prng Struct Reference

Represents a Pseudo Random Number Generator. More...

#include <random.hpp>

Collaboration diagram for mtcore::Prng:

Public Member Functions

bool can_get1_0 () const noexcept
 
bool can_get_uint () const noexcept
 
bool can_reseed () const noexcept
 
double next1_0 () noexcept
 Gets a number n such that $0 <= n < 1$ Will abort of canGet1_0 is false!
 
u64 next_uint () noexcept
 Gets the next unsigned 64-bit integer from the PRNG Will abort of canGetUInt is false!
 
u64 in_range (const u64 min, const u64 max) noexcept
 Gets number between min (inclusive) and max value (exclusive) Prioritizes nextUInt over next1_0.
 
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.
 
Result< void, SeedErrorseed (const u64 seed) noexcept
 Reseeds the PRNG.
 
void deinit () noexcept
 Deinitializes a PRNG NOTE: It is NOT safe to use a PRNG after it has been deinitialized!
 

Public Attributes

PrngState state
 
const PrngVTablevtable
 

Detailed Description

Represents a Pseudo Random Number Generator.

Definition at line 100 of file random.hpp.

Member Function Documentation

◆ can_get1_0()

bool mtcore::Prng::can_get1_0 ( ) const
inlinenodiscardnoexcept
Returns
Returns true if can safely call next1_0

Definition at line 109 of file random.hpp.

109{ return vtable && vtable->next1_0 != nullptr; }
const PrngVTable * vtable
Definition random.hpp:104
Here is the caller graph for this function:

◆ can_get_uint()

bool mtcore::Prng::can_get_uint ( ) const
inlinenodiscardnoexcept
Returns
Returns true if can safely call nextUInt

Definition at line 114 of file random.hpp.

114{ return vtable && vtable->nextUint != nullptr; }
Here is the caller graph for this function:

◆ can_reseed()

bool mtcore::Prng::can_reseed ( ) const
inlinenodiscardnoexcept
Returns
Returns true if can safely call seed

Definition at line 119 of file random.hpp.

119{ return vtable && vtable->seed != nullptr; }

◆ deinit()

void mtcore::Prng::deinit ( )
inlinenoexcept

Deinitializes a PRNG NOTE: It is NOT safe to use a PRNG after it has been deinitialized!

Definition at line 214 of file random.hpp.

214 {
215 mtdefer { vtable = nullptr; };
216 if (vtable && vtable->deinit) {
217 vtable->deinit(state);
218 }
219 }
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
PrngState state
Definition random.hpp:102

◆ in_range()

u64 mtcore::Prng::in_range ( const u64 min,
const u64 max )
inlinenodiscardnoexcept

Gets number between min (inclusive) and max value (exclusive) Prioritizes nextUInt over next1_0.

Parameters
minMinimum value
maxMaximum value
Returns
number between value

Definition at line 148 of file random.hpp.

148 {
149 ensure(min < max, "INVALID RANGE PROVIDED");
150 ensure(can_get_uint() || can_get1_0(), "INVALID PRNG - MUST HAVE next1_0 OR nextUint IN VTABLE");
151
152 const u64 count = max - min;
153
154 if (count == 1) {
155 return min;
156 }
157
158 const auto offset = std::numeric_limits<u64>::max() % count;
159 if (can_get_uint()) {
160 u64 res;
161 do {
162 res = next_uint();
163 } while (res >= (std::numeric_limits<u64>::max() - offset));
164 res %= count;
165 return res + min;
166 }
167 else {
168 const long double v = next1_0();
169 return static_cast<u64>(floor(v * static_cast<long double>(count))) + min;
170 }
171 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
uint64_t u64
Alias for 64-bit unsigned ints.
bool can_get_uint() const noexcept
Definition random.hpp:114
u64 next_uint() noexcept
Gets the next unsigned 64-bit integer from the PRNG Will abort of canGetUInt is false!
Definition random.hpp:135
double next1_0() noexcept
Gets a number n such that $0 <= n < 1$ Will abort of canGet1_0 is false!
Definition random.hpp:125
bool can_get1_0() const noexcept
Definition random.hpp:109
Here is the call graph for this function:
Here is the caller graph for this function:

◆ in_range_f64()

f64 mtcore::Prng::in_range_f64 ( const f64 min,
const f64 max )
inlinenodiscardnoexcept

Gets number between min (inclusive) and max value (exclusive) Prioritizes next1_0 over nextUInt.

Parameters
minMinimum value
maxMaximum value
Returns
number between value

Definition at line 180 of file random.hpp.

180 {
181 ensure(min < max, "INVALID RANGE PROVIDED");
182 ensure(can_get_uint() || can_get1_0(), "INVALID PRNG - MUST HAVE next1_0 OR nextUint IN VTABLE");
183
184 const auto count = max - min;
185
186 if (count == 1) {
187 return min;
188 }
189
190 if (can_get1_0()) {
191 const double v = next1_0();
192 return v * count + min;
193 }
194 else {
195 constexpr auto m = 10000000000;
196 const auto n = in_range(0, m);
197 const long double rng = static_cast<long double>(n) / static_cast<long double>(m);
198 return static_cast<f64>(rng * count) + min;
199 }
200 }
double f64
Alias for 64-bit floats.
u64 in_range(const u64 min, const u64 max) noexcept
Gets number between min (inclusive) and max value (exclusive) Prioritizes nextUInt over next1_0.
Definition random.hpp:148
Here is the call graph for this function:

◆ next1_0()

double mtcore::Prng::next1_0 ( )
inlinenodiscardnoexcept

Gets a number n such that $0 <= n < 1$ Will abort of canGet1_0 is false!

Definition at line 125 of file random.hpp.

125 {
126 ensure(vtable, "MISSING VTABLE FOR PRNG");
127 ensure(vtable->next1_0, "MISSING next1_0 IN PRNG VTABLE");
128 return vtable->next1_0(state);
129 }
Here is the caller graph for this function:

◆ next_uint()

u64 mtcore::Prng::next_uint ( )
inlinenodiscardnoexcept

Gets the next unsigned 64-bit integer from the PRNG Will abort of canGetUInt is false!

Definition at line 135 of file random.hpp.

135 {
136 ensure(vtable, "MISSING VTABLE FOR PRNG");
137 ensure(vtable->nextUint, "MISSING nextUint IN PRNG VTABLE");
138 return vtable->nextUint(state);
139 }
Here is the caller graph for this function:

◆ seed()

Result< void, SeedError > mtcore::Prng::seed ( const u64 seed)
inlinenodiscardnoexcept

Reseeds the PRNG.

Definition at line 205 of file random.hpp.

205 {
206 ensure(vtable && vtable->seed, "INVALID VTABLE!");
207 return vtable->seed(state, seed);
208 }
Result< void, SeedError > seed(const u64 seed) noexcept
Reseeds the PRNG.
Definition random.hpp:205
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ state

PrngState mtcore::Prng::state

Definition at line 102 of file random.hpp.

◆ vtable

const PrngVTable* mtcore::Prng::vtable

Definition at line 104 of file random.hpp.


The documentation for this struct was generated from the following file: