https://mooseframework.inl.gov
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. More...
 
evaluate (std::size_t n) const
 Evaluate the n-th random number in the sequence. More...
 
void advance (std::size_t count)
 Advance the internal RNG state by a fixed count. More...
 

Private Member Functions

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

Private Attributes

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

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

Member Function Documentation

◆ advance() [1/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.

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

232  {
233  advance(_initial_state, count);
235  _current_index = 0;
236  }
void advance(std::size_t count)
Advance the internal RNG state by a fixed count.
std::size_t _current_index
Index of next number to be generated.
mt_state _current_state
Cached working RNG state.
mt_state _initial_state
Base RNG state (seeded)

◆ advance() [2/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  }
const std::function< T(mt_state *)> _rng_func
RNG function pointer.

◆ 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.

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

210  {
211  if (n < _current_index)
212  {
214  _current_index = 0;
215  }
216 
217  T val = T();
218  for (; _current_index <= n; ++_current_index)
219  val = _rng_func(&_current_state);
220 
221  return val;
222  }
const std::function< T(mt_state *)> _rng_func
RNG function pointer.
std::size_t _current_index
Index of next number to be generated.
mt_state _current_state
Cached working RNG state.
mt_state _initial_state
Base RNG state (seeded)

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: