16#include <unordered_map>
50 static inline Real
rand() {
return mt_ldrand(); }
60 static inline Real
randNormal(Real mean, Real sigma) {
return rd_normal(mean, sigma); }
71 static inline uint32_t
randl() {
return mt_lrand(); }
85 inline Real
rand(std::size_t i)
88 return mts_ldrand(&(
_states[i].first));
102 mooseAssert(
_states.find(i) !=
_states.end(),
"No random state initialized for id: " << i);
103 return rds_normal(&(
_states[i].first), mean, sigma);
116 inline uint32_t
randl(std::size_t i)
118 mooseAssert(
_states.find(i) !=
_states.end(),
"No random state initialized for id: " << i);
119 return mts_lrand(&(
_states[i].first));
131 inline uint32_t
randl(std::size_t i, uint32_t lower, uint32_t upper)
133 mooseAssert(
_states.find(i) !=
_states.end(),
"No random state initialized for id: " << i);
134 return rds_iuniform(&(
_states[i].first), lower, upper);
146 [](std::pair<
const std::size_t, std::pair<mt_state, mt_state>> & pair)
147 { pair.second.second = pair.second.first; });
155 mooseAssert(
_saved,
"saveState() must be called prior to restoreState().");
158 [](std::pair<
const std::size_t, std::pair<mt_state, mt_state>> & pair)
159 { pair.second.first = pair.second.second; });
173 std::unordered_map<std::size_t, std::pair<mt_state, mt_state>>
_states;
176 friend void dataStore<MooseRandom>(std::ostream & stream,
MooseRandom & v,
void * context);
177 friend void dataLoad<MooseRandom>(std::istream & stream,
MooseRandom & v,
void * context);
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
void dataStore(std::ostream &stream, MooseRandom &v, void *context)
void dataLoad(std::istream &stream, MooseRandom &v, void *context)
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
uint32_t randl(std::size_t i, uint32_t lower, uint32_t upper)
This method returns the next random number (long format) from the specified generator within the supp...
static Real rand()
This method returns the next random number (Real format) from the generator.
static uint32_t randl()
This method returns the next random number (long format) from the generator.
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
static Real randNormal()
Return next random number drawn from a standard distribution.
void restoreState()
This method restores the last saved generator state.
void saveState()
This method saves the current state of all generators which can be restored at a later time (i....
void seed(std::size_t i, unsigned int seed)
The method seeds one of the independent random number generators.
static void seed(unsigned int seed)
The method seeds the random number generator.
Real rand(std::size_t i)
This method returns the next random number (Real format) from the specified generator.
static Real randNormal(Real mean, Real sigma)
This method returns the next random number (Real format) from the generator, drawn from a normal dist...
Real randNormal(std::size_t i)
Return next random number drawn from a standard distribution.
Real randNormal(std::size_t i, Real mean, Real sigma)
This method returns the next random number (Real format) from the specified generator,...
bool _saved
Flag to make certain that saveState is called prior to restoreState.
std::size_t size()
Return the number of states.
uint32_t randl(std::size_t i)
This method returns the next random number (long format) from the specified generator.