https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MooseRandomStateless::Generator< T > Class Template Reference

Template class wrapping a single random number generator function. More...

#include <MooseRandomStateless.h>

Public Member Functions

 Generator (std::function< T(mt_state *)> rng_func, unsigned int seed)
 Construct a new Generator with a function and seed.
 
evaluate (std::size_t n) const
 Evaluate the n-th random number in the sequence.
 
void advance (std::size_t count)
 Advance the internal RNG state by a fixed count.
 

Private Member Functions

void advance (mt_state &state, std::size_t count)
 Advance a given RNG state count times.
 

Private Attributes

const std::function< T(mt_state *)> _rng_func
 RNG function pointer.
 
mt_state _initial_state
 Base RNG state (seeded)
 
mt_state _current_state
 Cached working RNG state.
 
std::size_t _current_index
 Index of next number to be generated.
 

Detailed Description

template<typename T>
class MooseRandomStateless::Generator< T >

Template class wrapping a single random number generator function.

Template Parameters
TOutput type of the RNG (e.g., Real or unsigned int)

Each Generator stores:

Definition at line 182 of file MooseRandomStateless.h.

Constructor & Destructor Documentation

◆ Generator()

template<typename T >
MooseRandomStateless::Generator< T >::Generator ( std::function< T(mt_state *)>  rng_func,
unsigned int  seed 
)
inline

Construct a new Generator with a function and seed.

Parameters
rng_funcRNG function to apply to mt_state (e.g., mts_ldrand)
seedSeed used to initialize the base state

Definition at line 191 of file MooseRandomStateless.h.

192 : _rng_func(std::move(rng_func))
193 {
194 // Create a new state with the given seed
195 mts_seed32new(&_initial_state, seed);
197 _current_index = 0;
198 }
mt_state _initial_state
Base RNG state (seeded)
const std::function< T(mt_state *)> _rng_func
RNG function pointer.
mt_state _current_state
Cached working RNG state.
std::size_t _current_index
Index of next number to be generated.

Member Function Documentation

◆ advance() [1/2]

template<typename T >
void MooseRandomStateless::Generator< T >::advance ( mt_state &  state,
std::size_t  count 
)
inlineprivate

Advance a given RNG state count times.

Definition at line 240 of file MooseRandomStateless.h.

241 {
242 for (std::size_t i = 0; i < count; ++i)
243 _rng_func(&state);
244 }
unsigned int count
Definition MortarUtils.C:53

◆ advance() [2/2]

template<typename T >
void MooseRandomStateless::Generator< T >::advance ( std::size_t  count)
inline

Advance the internal RNG state by a fixed count.

This resets _current_state to _initial_state after advancing.

Parameters
countNumber of RNG calls to skip

Definition at line 231 of file MooseRandomStateless.h.

232 {
235 _current_index = 0;
236 }
void advance(std::size_t count)
Advance the internal RNG state by a fixed count.

Referenced by MooseRandomStateless::advance(), and MooseRandomStateless::Generator< T >::advance().

◆ evaluate()

template<typename T >
T MooseRandomStateless::Generator< T >::evaluate ( std::size_t  n) const
inline

Evaluate the n-th random number in the sequence.

Uses cached state when n >= _current_index for sequential access. If n < _current_index, it resets to the initial seed and recomputes.

Parameters
n0-based index in the random sequence
Returns
T The n-th random value

Definition at line 209 of file MooseRandomStateless.h.

210 {
211 if (n < _current_index)
212 {
214 _current_index = 0;
215 }
216
217 T val = T();
218 for (; _current_index <= n; ++_current_index)
220
221 return val;
222 }

Referenced by MooseRandomStateless::rand(), and MooseRandomStateless::randl().

Member Data Documentation

◆ _current_index

template<typename T >
std::size_t MooseRandomStateless::Generator< T >::_current_index
mutableprivate

◆ _current_state

template<typename T >
mt_state MooseRandomStateless::Generator< T >::_current_state
mutableprivate

◆ _initial_state

template<typename T >
mt_state MooseRandomStateless::Generator< T >::_initial_state
private

◆ _rng_func

template<typename T >
const std::function<T(mt_state *)> MooseRandomStateless::Generator< T >::_rng_func
private

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