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 (std::size_t i, unsigned int seed)
 The method seeds one of the independent random number generators. More...
 
double rand (std::size_t i)
 This method returns the next random number (double format) from the specified generator. More...
 
double randNormal (std::size_t 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 (std::size_t i)
 Return next random number drawn from a standard distribution. More...
 
uint32_t randl (std::size_t 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...
 
std::size_t 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< std::size_t, 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 37 of file MooseRandom.h.

Member Function Documentation

◆ rand() [1/2]

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 50 of file MooseRandom.h.

Referenced by RandomICBase::generateRandom(), RankTwoTensor::genRandomSymmTensor(), RankTwoTensor::genRandomTensor(), RandomInterface::getRandomReal(), and Sampler::rand().

50 { return mt_ldrand(); }

◆ rand() [2/2]

double MooseRandom::rand ( std::size_t  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 85 of file MooseRandom.h.

86  {
87  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
88  return mts_ldrand(&(_states[i].first));
89  }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160

◆ randl() [1/2]

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 71 of file MooseRandom.h.

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

71 { return mt_lrand(); }

◆ randl() [2/2]

uint32_t MooseRandom::randl ( std::size_t  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 116 of file MooseRandom.h.

117  {
118  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
119  return mts_lrand(&(_states[i].first));
120  }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160

◆ randNormal() [1/4]

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 60 of file MooseRandom.h.

60 { return rd_normal(mean, sigma); }

◆ randNormal() [2/4]

static double MooseRandom::randNormal ( )
inlinestatic

Return next random number drawn from a standard distribution.

Definition at line 65 of file MooseRandom.h.

Referenced by randNormal().

65 { return randNormal(0.0, 1.0); }
static double randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:65

◆ randNormal() [3/4]

double MooseRandom::randNormal ( std::size_t  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 100 of file MooseRandom.h.

101  {
102  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
103  return rds_normal(&(_states[i].first), mean, sigma);
104  }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160

◆ randNormal() [4/4]

double MooseRandom::randNormal ( std::size_t  i)
inline

Return next random number drawn from a standard distribution.

Definition at line 109 of file MooseRandom.h.

Referenced by randNormal().

109 { return randNormal(i, 0.0, 1.0); }
static double randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:65

◆ restoreState()

void MooseRandom::restoreState ( )
inline

This method restores the last saved generator state.

Definition at line 139 of file MooseRandom.h.

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

140  {
141  mooseAssert(_saved, "saveState() must be called prior to restoreState().");
142  std::for_each(_states.begin(),
143  _states.end(),
144  [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair) {
145  pair.second.first = pair.second.second;
146  });
147  }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:167

◆ saveState()

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 126 of file MooseRandom.h.

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

127  {
128  _saved = true;
129  std::for_each(_states.begin(),
130  _states.end(),
131  [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair) {
132  pair.second.second = pair.second.first;
133  });
134  }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:167

◆ seed() [1/2]

static void MooseRandom::seed ( unsigned int  seed)
inlinestatic

The method seeds the random number generator.

Parameters
seedthe seed number

Definition at line 44 of file MooseRandom.h.

Referenced by RankTwoTensor::initRandom(), MooseInit::MooseInit(), RandomICBase::RandomICBase(), seed(), Sampler::setNumberOfRequiedRandomSeeds(), and RandomData::updateGenerators().

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

◆ seed() [2/2]

void MooseRandom::seed ( std::size_t  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 78 of file MooseRandom.h.

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

◆ size()

std::size_t MooseRandom::size ( )
inline

Return the number of states.

Definition at line 152 of file MooseRandom.h.

Referenced by Sampler::rand().

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

Friends And Related Function Documentation

◆ dataLoad

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

Definition at line 178 of file MooseRandom.h.

179 {
180  loadHelper(stream, v._states, context);
181 }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:731

◆ dataStore

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

Definition at line 172 of file MooseRandom.h.

173 {
174  storeHelper(stream, v._states, context);
175 }
std::unordered_map< std::size_t, std::pair< mt_state, mt_state > > _states
We store a pair of states in this map.
Definition: MooseRandom.h:160
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:667

Member Data Documentation

◆ _saved

bool MooseRandom::_saved = false
private

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

Definition at line 167 of file MooseRandom.h.

Referenced by restoreState(), and saveState().

◆ _states

std::unordered_map<std::size_t, 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 160 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: