A deterministic, indexable random number generator built on top of the randistrs library.
More...
#include <MooseRandomStateless.h>
Classes | |
| class | Generator |
| Template class wrapping a single random number generator function. More... | |
Public Member Functions | |
| MooseRandomStateless (const MooseRandomStateless &)=delete | |
| Deleted copy constructor and assignment to prevent deep-copy of mutable state. More... | |
| MooseRandomStateless & | operator= (const MooseRandomStateless &)=delete |
| MooseRandomStateless (unsigned int seed) | |
| Construct a new MooseRandomStateless with a given seed. More... | |
| unsigned int | getSeed () const |
| Get the seed value used to initialize this handler's RNGs. More... | |
| std::size_t | getAdvanceCount () const |
| Get the amount that the RNGs have advanced by advance(count) calls. More... | |
| Real | rand (std::size_t n) const |
| Return the n-th uniform Real random number in [0, 1). More... | |
| unsigned int | randl (std::size_t n) const |
| Return the n-th unsigned integer from the full 32-bit uniform range. More... | |
| unsigned int | randl (std::size_t n, unsigned int lower, unsigned int upper) const |
| Return the n-th bounded integer random number in [lower, upper). More... | |
| void | advance (std::size_t count) |
| Advance all generators by the specified number. More... | |
Private Attributes | |
| const unsigned int | _seed |
| Seed shared by all internal generators. More... | |
| Generator< Real > | _rand_generator |
| Uniform Real [0, 1) More... | |
| Generator< unsigned int > | _randl_generator |
| Uniform uint32. More... | |
| std::unordered_map< unsigned int, Generator< unsigned int > > | _randlb_generators |
| Bounded uniform uint32 (indexed based on bounding range) More... | |
| std::size_t | _advance_count = 0 |
| The number of counts the generators have advanced This needs to be kept around for generation of new randlb generators. More... | |
A deterministic, indexable random number generator built on top of the randistrs library.
This class provides seed-able random generators that can produce the n-th sample in a reproducible sequence without maintaining large generator states between calls. It achieves this by keeping a small cached generator state for efficient sequential access, while still allowing deterministic random access to any index n via recomputation.
Usage Example:
Thread Safety:
evaluate(). If you need concurrency, create separate objects per thread.Performance:
n = k, k+1, k+2, ...) is O(1) per call.n, which is O(n) in the distance skipped.Design Overview:
MooseRandomStatless owns several Generator<T> instances, each parameterized by a distribution:mts_ldrand: Uniform real [0, 1)mts_lrand: Uniform unsigned intrds_iuniform: Uniform integer [lower, upper)Generator<T> encapsulates:mt_state seeded by mts_seed32new()evaluate(n)MooseRandom class.Implementation Notes:
evaluate() is declared const but mutates internal state marked as mutable to allow transparent caching; this is a deliberate trade-off for performance.mt_state; there is no global RNG state shared across indices.rds_iuniform with unique ranges (upper - lower). This is because rds_iuniform increments its state differently for each of these ranges. Creating separate generators for each input range is unfortunate, but necessary for stateless-ness. Definition at line 81 of file MooseRandomStateless.h.
|
delete |
Deleted copy constructor and assignment to prevent deep-copy of mutable state.
|
inline |
Construct a new MooseRandomStateless with a given seed.
Initializes internal generators for real and integer distributions.
| seed | Base seed value for all RNGs in this handler |
Definition at line 95 of file MooseRandomStateless.h.
|
inline |
Advance all generators by the specified number.
| count | The number of calls to advance. |
Definition at line 162 of file MooseRandomStateless.h.
Referenced by dataLoad().
|
inline |
Get the amount that the RNGs have advanced by advance(count) calls.
Definition at line 112 of file MooseRandomStateless.h.
Referenced by dataStore().
|
inline |
Get the seed value used to initialize this handler's RNGs.
Definition at line 105 of file MooseRandomStateless.h.
|
delete |
|
inline |
Return the n-th uniform Real random number in [0, 1).
Uses the cached state for sequential access; otherwise, recomputes from seed.
| n | 0-based index of the random number to generate |
Definition at line 122 of file MooseRandomStateless.h.
|
inline |
Return the n-th unsigned integer from the full 32-bit uniform range.
This is useful for generating seeds for new generators.
| n | 0-based index of the random number to generate |
Definition at line 131 of file MooseRandomStateless.h.
|
inline |
Return the n-th bounded integer random number in [lower, upper).
Uses a cached generator per unique range.
| n | 0-based index of the random number to generate |
| lower | Lower bound (inclusive) |
| upper | Upper bound (exclusive) |
Definition at line 143 of file MooseRandomStateless.h.
|
private |
The number of counts the generators have advanced This needs to be kept around for generation of new randlb generators.
Definition at line 268 of file MooseRandomStateless.h.
Referenced by advance(), getAdvanceCount(), and randl().
Uniform Real [0, 1)
Definition at line 260 of file MooseRandomStateless.h.
Uniform uint32.
Definition at line 262 of file MooseRandomStateless.h.
|
mutableprivate |
Bounded uniform uint32 (indexed based on bounding range)
Definition at line 264 of file MooseRandomStateless.h.
|
private |
Seed shared by all internal generators.
Definition at line 258 of file MooseRandomStateless.h.
1.8.14