https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
Real rand (std::size_t i)
 This method returns the next random number (Real format) from the specified generator.
 
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.
 
Real randNormal (std::size_t i)
 Return next random number drawn from a standard distribution.
 
uint32_t randl (std::size_t i)
 This method returns the next random number (long format) from the specified generator.
 
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.
 
void saveState ()
 This method saves the current state of all generators which can be restored at a later time (i.e.
 
void restoreState ()
 This method restores the last saved generator state.
 
std::size_t size ()
 Return the number of states.
 

Static Public Member Functions

static void seed (unsigned int seed)
 The method seeds the random number generator.
 
static Real rand ()
 This method returns the next random number (Real format) from the generator.
 
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.
 
static Real randNormal ()
 Return next random number drawn from a standard distribution.
 
static uint32_t randl ()
 This method returns the next random number (long format) from the generator.
 

Private Attributes

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

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. This interface DOES NOT SUPPORT restart/recover
  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 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 50 of file MooseRandom.h.

50{ return mt_ldrand(); }

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

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

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

71{ return mt_lrand(); }

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

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

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

132 {
133 mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
134 return rds_iuniform(&(_states[i].first), lower, upper);
135 }

◆ randNormal() [1/4]

static Real MooseRandom::randNormal ( )
inlinestatic

Return next random number drawn from a standard distribution.

Definition at line 65 of file MooseRandom.h.

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

Referenced by randNormal().

◆ randNormal() [2/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 60 of file MooseRandom.h.

60{ return rd_normal(mean, sigma); }

◆ randNormal() [3/4]

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

Return next random number drawn from a standard distribution.

Definition at line 109 of file MooseRandom.h.

109{ return randNormal(i, 0.0, 1.0); }

Referenced by randNormal().

◆ randNormal() [4/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 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 }

◆ restoreState()

void MooseRandom::restoreState ( )
inline

This method restores the last saved generator state.

Definition at line 153 of file MooseRandom.h.

154 {
155 mooseAssert(_saved, "saveState() must be called prior to restoreState().");
156 std::for_each(_states.begin(),
157 _states.end(),
158 [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair)
159 { pair.second.first = pair.second.second; });
160 }
bool _saved
Flag to make certain that saveState is called prior to restoreState.

Referenced by RandomData::updateSeeds().

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

142 {
143 _saved = true;
144 std::for_each(_states.begin(),
145 _states.end(),
146 [](std::pair<const std::size_t, std::pair<mt_state, mt_state>> & pair)
147 { pair.second.second = pair.second.first; });
148 }

Referenced by RandomData::updateSeeds().

◆ seed() [1/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); }
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition MooseRandom.h:44

◆ seed() [2/2]

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

◆ size()

std::size_t MooseRandom::size ( )
inline

Return the number of states.

Definition at line 165 of file MooseRandom.h.

165{ return _states.size(); }

Friends And Related Symbol Documentation

◆ dataLoad

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

Definition at line 191 of file MooseRandom.h.

192{
193 loadHelper(stream, v._states, context);
194}
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:1081

◆ dataStore

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

Definition at line 185 of file MooseRandom.h.

186{
187 storeHelper(stream, v._states, context);
188}
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:989

Member Data Documentation

◆ _saved

bool MooseRandom::_saved = false
private

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

Definition at line 180 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 173 of file MooseRandom.h.

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


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