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...
 
Real rand (std::size_t i)
 This method returns the next random number (Real format) from the specified generator. More...
 
Real randNormal (std::size_t i, Real mean, Real sigma)
 This method returns the next random number (Real format) from the specified generator, drawn from a normal distribution centered around mean, with a width of sigma. More...
 
Real 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...
 
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 supplied range. 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 Real rand ()
 This method returns the next random number (Real format) from the generator. More...
 
static Real randNormal (Real mean, Real sigma)
 This method returns the next random number (Real format) from the generator, drawn from a normal distribution centered around mean, with a width of sigma. More...
 
static Real 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 36 of file MooseRandom.h.

Member Function Documentation

◆ rand() [1/2]

static Real MooseRandom::rand ( )
inlinestatic

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

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

Definition at line 49 of file MooseRandom.h.

Referenced by RandomPartitioner::_do_partition(), RandomICBase::generateRandom(), SymmetricRankTwoTensorTempl< T >::genRandomSymmTensor(), RankTwoTensorTempl< Real >::genRandomSymmTensor(), RankTwoTensorTempl< Real >::genRandomTensor(), Sampler::getRand(), RandomInterface::getRandomReal(), and PropertyReadFile::initVoronoiCenterPoints().

49 { return mt_ldrand(); }

◆ rand() [2/2]

Real MooseRandom::rand ( std::size_t  i)
inline

This method returns the next random number (Real 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 84 of file MooseRandom.h.

85  {
86  // mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
87  return mts_ldrand(&(_states[i].first));
88  }
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:172

◆ randl() [1/3]

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

Referenced by Sampler::getRandl(), RandomInterface::getRandomLong(), RandomICBase::RandomICBase(), MooseUtils::resample(), MooseUtils::resampleWithFunctor(), MooseUtils::shuffle(), and RandomData::updateGenerators().

70 { return mt_lrand(); }

◆ randl() [2/3]

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

116  {
117  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
118  return mts_lrand(&(_states[i].first));
119  }
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:172

◆ randl() [3/3]

uint32_t MooseRandom::randl ( std::size_t  i,
uint32_t  lower,
uint32_t  upper 
)
inline

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

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

Definition at line 130 of file MooseRandom.h.

131  {
132  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
133  return rds_iuniform(&(_states[i].first), lower, upper);
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:172

◆ randNormal() [1/4]

static Real MooseRandom::randNormal ( Real  mean,
Real  sigma 
)
inlinestatic

This method returns the next random number (Real 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 59 of file MooseRandom.h.

59 { return rd_normal(mean, sigma); }

◆ randNormal() [2/4]

static Real MooseRandom::randNormal ( )
inlinestatic

Return next random number drawn from a standard distribution.

Definition at line 64 of file MooseRandom.h.

Referenced by randNormal().

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

◆ randNormal() [3/4]

Real MooseRandom::randNormal ( std::size_t  i,
Real  mean,
Real  sigma 
)
inline

This method returns the next random number (Real 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 99 of file MooseRandom.h.

100  {
101  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
102  return rds_normal(&(_states[i].first), mean, sigma);
103  }
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:172

◆ randNormal() [4/4]

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

Return next random number drawn from a standard distribution.

Definition at line 108 of file MooseRandom.h.

Referenced by randNormal().

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

◆ restoreState()

void MooseRandom::restoreState ( )
inline

This method restores the last saved generator state.

Definition at line 152 of file MooseRandom.h.

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

153  {
154  mooseAssert(_saved, "saveState() must be called prior to restoreState().");
155  std::for_each(_states.begin(),
156  _states.end(),
157  [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair)
158  { pair.second.first = pair.second.second; });
159  }
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:172
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:179

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

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

141  {
142  _saved = true;
143  std::for_each(_states.begin(),
144  _states.end(),
145  [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair)
146  { pair.second.second = pair.second.first; });
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:172
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:179

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

Referenced by Sampler::init(), RankTwoTensorTempl< Real >::initRandom(), SymmetricRankTwoTensorTempl< T >::initRandom(), PropertyReadFile::initVoronoiCenterPoints(), MooseInit::MooseInit(), RandomICBase::RandomICBase(), RandomPartitioner::RandomPartitioner(), seed(), and RandomData::updateGenerators().

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

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

77 { 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:172
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:43

◆ size()

std::size_t MooseRandom::size ( )
inline

Return the number of states.

Definition at line 164 of file MooseRandom.h.

Referenced by Sampler::advanceGenerators(), Sampler::getRand(), and Sampler::getRandl().

164 { 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:172

Friends And Related Function Documentation

◆ dataLoad

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

Definition at line 190 of file MooseRandom.h.

191 {
192  loadHelper(stream, v._states, context);
193 }
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:172
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:948

◆ dataStore

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

Definition at line 184 of file MooseRandom.h.

185 {
186  storeHelper(stream, v._states, context);
187 }
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:172
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:856

Member Data Documentation

◆ _saved

bool MooseRandom::_saved = false
private

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

Definition at line 179 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 172 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: