www.mooseframework.org
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
MooseRandom Class Reference

This class encapsulates a useful, consistent, cross-platform random number generator with multiple utilities. More...

#include <MooseRandom.h>

Public Member Functions

void seed (unsigned int i, unsigned int seed)
 The method seeds one of the independent random number generators. More...
 
double rand (unsigned int i)
 This method returns the next random number (double format) from the specified generator. More...
 
double randNormal (unsigned int i, double mean, double sigma)
 This method returns the next random number (double format) from the specified generator, drawn from a normal distribution centered around mean, with a width of sigma. More...
 
double randNormal (unsigned int i)
 Return next random number drawn from a standard distribution. More...
 
uint32_t randl (unsigned int i)
 This method returns the next random number (long format) from the specified generator. More...
 
void saveState ()
 This method saves the current state of all generators which can be restored at a later time (i.e. More...
 
void restoreState ()
 This method restores the last saved generator state. More...
 
unsigned int size ()
 Return the number of states. More...
 

Static Public Member Functions

static void seed (unsigned int seed)
 The method seeds the random number generator. More...
 
static double rand ()
 This method returns the next random number (double format) from the generator. More...
 
static double randNormal (double mean, double sigma)
 This method returns the next random number (double format) from the generator, drawn from a normal distribution centered around mean, with a width of sigma. More...
 
static double randNormal ()
 Return next random number drawn from a standard distribution. More...
 
static uint32_t randl ()
 This method returns the next random number (long format) from the generator. More...
 

Private Attributes

std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
 We store a pair of states in this map. More...
 
bool _saved = false
 Flag to make certain that saveState is called prior to restoreState. More...
 

Friends

void dataStore (std::ostream &stream, MooseRandom &v, void *context)
 
void dataLoad (std::istream &stream, MooseRandom &v, void *context)
 

Detailed Description

This class encapsulates a useful, consistent, cross-platform random number generator with multiple utilities.

  1. SIMPLE INTERFACE: There are three static functions that are suitable as a drop in replacement for the random number capabilities available in the standard C++ library.
  2. ADVANCED INTERFACE: When creating an instance of this class, one can maintain an arbitrary number of multiple independent streams of random numbers. Furthermore, the state of these generators can be saved and restored for all streams by using the "saveState" and "restoreState" methods. Finally, this class uses a fast hash map so that indexes for the generators are not required to be contiguous.

Definition at line 42 of file MooseRandom.h.

Member Function Documentation

static double MooseRandom::rand ( )
inlinestatic

This method returns the next random number (double format) from the generator.

Returns
the next random number in the range [0,1) with 64-bit precision

Definition at line 55 of file MooseRandom.h.

Referenced by RankTwoTensor::genRandomSymmTensor(), RankTwoTensor::genRandomTensor(), RandomInterface::getRandomReal(), Sampler::rand(), and RandomIC::value().

55 { return mt_ldrand(); }
double MooseRandom::rand ( unsigned int  i)
inline

This method returns the next random number (double format) from the specified generator.

Parameters
ithe index of the generator
Returns
the next random number in the range [0,1) with 64-bit precision

Definition at line 90 of file MooseRandom.h.

91  {
92  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
93  return mts_ldrand(&(_states[i].first));
94  }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
static uint32_t MooseRandom::randl ( )
inlinestatic

This method returns the next random number (long format) from the generator.

Returns
the next random number in the range [0,max(uinit32_t)) with 32-bit number

Definition at line 76 of file MooseRandom.h.

Referenced by RandomInterface::getRandomLong(), Sampler::setNumberOfRequiedRandomSeeds(), RandomData::updateGeneratorHelper(), and RandomData::updateGenerators().

76 { return mt_lrand(); }
uint32_t MooseRandom::randl ( unsigned int  i)
inline

This method returns the next random number (long format) from the specified generator.

Parameters
ithe index of the generator
Returns
the next random number in the range [0,max(uinit32_t)) with 32-bit number

Definition at line 121 of file MooseRandom.h.

122  {
123  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
124  return mts_lrand(&(_states[i].first));
125  }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
static double MooseRandom::randNormal ( double  mean,
double  sigma 
)
inlinestatic

This method returns the next random number (double format) from the generator, drawn from a normal distribution centered around mean, with a width of sigma.

Parameters
meancenter of the random number distribution
sigmawidth of the random number distribution
Returns
the next random number following a normal distribution of width sigma around mean with 64-bit precision

Definition at line 65 of file MooseRandom.h.

65 { return rd_normal(mean, sigma); }
static double MooseRandom::randNormal ( )
inlinestatic

Return next random number drawn from a standard distribution.

Definition at line 70 of file MooseRandom.h.

Referenced by randNormal().

70 { return randNormal(0.0, 1.0); }
static double randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:70
double MooseRandom::randNormal ( unsigned int  i,
double  mean,
double  sigma 
)
inline

This method returns the next random number (double format) from the specified generator, drawn from a normal distribution centered around mean, with a width of sigma.

Parameters
ithe index of the generator
meancenter of the random number distribution
sigmawidth of the random number distribution
Returns
the next random number following a normal distribution of width sigma around mean with 64-bit precision

Definition at line 105 of file MooseRandom.h.

106  {
107  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
108  return rds_normal(&(_states[i].first), mean, sigma);
109  }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
double MooseRandom::randNormal ( unsigned int  i)
inline

Return next random number drawn from a standard distribution.

Definition at line 114 of file MooseRandom.h.

Referenced by randNormal().

114 { return randNormal(i, 0.0, 1.0); }
static double randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:70
void MooseRandom::restoreState ( )
inline

This method restores the last saved generator state.

Definition at line 144 of file MooseRandom.h.

Referenced by Sampler::getSamples(), and RandomData::updateSeeds().

145  {
146  mooseAssert(_saved, "saveState() must be called prior to restoreState().");
147  std::for_each(_states.begin(),
148  _states.end(),
149  [](std::pair<const unsigned int, std::pair<mt_state, mt_state>> & pair) {
150  pair.second.first = pair.second.second;
151  });
152  }
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:172
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
void MooseRandom::saveState ( )
inline

This method saves the current state of all generators which can be restored at a later time (i.e.

re-generate the same sequence of random numbers of this generator

Definition at line 131 of file MooseRandom.h.

Referenced by Sampler::execute(), Sampler::setNumberOfRequiedRandomSeeds(), and RandomData::updateSeeds().

132  {
133  _saved = true;
134  std::for_each(_states.begin(),
135  _states.end(),
136  [](std::pair<const unsigned int, std::pair<mt_state, mt_state>> & pair) {
137  pair.second.second = pair.second.first;
138  });
139  }
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:172
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
static void MooseRandom::seed ( unsigned int  seed)
inlinestatic

The method seeds the random number generator.

Parameters
seedthe seed number

Definition at line 49 of file MooseRandom.h.

Referenced by RankTwoTensor::initRandom(), MooseInit::MooseInit(), RandomIC::RandomIC(), Sampler::setNumberOfRequiedRandomSeeds(), RandomData::updateGeneratorHelper(), and RandomData::updateGenerators().

49 { mt_seed32new(seed); }
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
void MooseRandom::seed ( unsigned int  i,
unsigned int  seed 
)
inline

The method seeds one of the independent random number generators.

Parameters
ithe index of the generator
seedthe seed number

Definition at line 83 of file MooseRandom.h.

83 { mts_seed32new(&(_states[i].first), seed); }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
unsigned int MooseRandom::size ( )
inline

Return the number of states.

Definition at line 157 of file MooseRandom.h.

Referenced by Sampler::rand().

157 { return _states.size(); }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165

Friends And Related Function Documentation

void dataLoad ( std::istream &  stream,
MooseRandom v,
void *  context 
)
friend

Definition at line 183 of file MooseRandom.h.

184 {
185  loadHelper(stream, v._states, context);
186 }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:592
void dataStore ( std::ostream &  stream,
MooseRandom v,
void *  context 
)
friend

Definition at line 177 of file MooseRandom.h.

178 {
179  storeHelper(stream, v._states, context);
180 }
std::unordered_map< unsigned int, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:165
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:528

Member Data Documentation

bool MooseRandom::_saved = false
private

Flag to make certain that saveState is called prior to restoreState.

Definition at line 172 of file MooseRandom.h.

Referenced by restoreState(), and saveState().

std::unordered_map<unsigned int, std::pair<mt_state, mt_state> > MooseRandom::_states
private

We store a pair of states in this map.

The first one is the active state, the second is the backup state. It is used to restore state at a later time to the active state.

Definition at line 165 of file MooseRandom.h.

Referenced by dataLoad(), dataStore(), rand(), randl(), randNormal(), restoreState(), saveState(), seed(), and size().


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