PSCF v1.1
Public Member Functions | List of all members
Util::Random Class Reference

Random number generator. More...

#include <Random.h>

Inheritance diagram for Util::Random:
Util::ParamComposite Util::ParamComponent Util::Serializable Util::MpiFileIo

Public Member Functions

 Random ()
 Constructor. More...
 
virtual ~Random ()
 Destructor. More...
 
virtual void readParameters (std::istream &in)
 Read seed from file, initialize RNG. More...
 
virtual void loadParameters (Serializable::IArchive &ar)
 Load internal state from file. More...
 
virtual void save (Serializable::OArchive &ar)
 Save internal state to file. More...
 
void setSeed (SeedType seed)
 Sets of random seed, and initializes random number generator. More...
 
double uniform ()
 Return a random floating point number x, uniformly distributed in the range 0 <= x < 1. More...
 
double uniform (double range1, double range2)
 Return a random floating point number x, uniformly distributed in the range range1 <= x < range2. More...
 
long uniformInt (long range1, long range2)
 Return random long int x uniformly distributed in range1 <= x < range2. More...
 
void getPoint (double minR[], double maxR[], double r[])
 Generate a random point in a box. More...
 
double gaussian (void)
 Return a Gaussian random number with zero average and unit variance. More...
 
void unitVector (Vector &v)
 Generate unit vector with uniform probability over the unit sphere. More...
 
bool metropolis (double ratio)
 Metropolis algorithm for whether to accept a MC move. More...
 
long drawFrom (double probability[], long size)
 Choose one of several outcomes with a specified set of probabilities. More...
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize to/from an archive. More...
 
long seed ()
 Returns value of random seed (private member variable seed_). More...
 
- Public Member Functions inherited from Util::ParamComposite
 ParamComposite ()
 Constructor. More...
 
 ParamComposite (const ParamComposite &other)
 Copy constructor. More...
 
 ParamComposite (int capacity)
 Constructor. More...
 
virtual ~ParamComposite ()
 Virtual destructor. More...
 
void resetParam ()
 Resets ParamComposite to its empty state. More...
 
virtual void readParam (std::istream &in)
 Read the parameter file block. More...
 
virtual void readParamOptional (std::istream &in)
 Read optional parameter file block. More...
 
virtual void writeParam (std::ostream &out) const
 Write all parameters to an output stream. More...
 
virtual void load (Serializable::IArchive &ar)
 Load all parameters from an input archive. More...
 
virtual void loadOptional (Serializable::IArchive &ar)
 Load an optional ParamComposite. More...
 
void saveOptional (Serializable::OArchive &ar)
 Saves isActive flag, and then calls save() iff isActive is true. More...
 
void readParamComposite (std::istream &in, ParamComposite &child, bool next=true)
 Add and read a required child ParamComposite. More...
 
void readParamCompositeOptional (std::istream &in, ParamComposite &child, bool next=true)
 Add and attempt to read an optional child ParamComposite. More...
 
template<typename Type >
ScalarParam< Type > & read (std::istream &in, const char *label, Type &value)
 Add and read a new required ScalarParam < Type > object. More...
 
template<typename Type >
ScalarParam< Type > & readOptional (std::istream &in, const char *label, Type &value)
 Add and read a new optional ScalarParam < Type > object. More...
 
template<typename Type >
CArrayParam< Type > & readCArray (std::istream &in, const char *label, Type *value, int n)
 Add and read a required C array parameter. More...
 
template<typename Type >
CArrayParam< Type > & readOptionalCArray (std::istream &in, const char *label, Type *value, int n)
 Add and read an optional C array parameter. More...
 
template<typename Type >
DArrayParam< Type > & readDArray (std::istream &in, const char *label, DArray< Type > &array, int n)
 Add and read a required DArray < Type > parameter. More...
 
template<typename Type >
DArrayParam< Type > & readOptionalDArray (std::istream &in, const char *label, DArray< Type > &array, int n)
 Add and read an optional DArray < Type > parameter. More...
 
template<typename Type , int N>
FArrayParam< Type, N > & readFArray (std::istream &in, const char *label, FArray< Type, N > &array)
 Add and read a required FArray < Type, N > array parameter. More...
 
template<typename Type , int N>
FArrayParam< Type, N > & readOptionalFArray (std::istream &in, const char *label, FArray< Type, N > &array)
 Add and read an optional FArray < Type, N > array parameter. More...
 
template<typename Type , int N>
FSArrayParam< Type, N > & readFSArray (std::istream &in, const char *label, FSArray< Type, N > &array, int size)
 Add and read a required FSArray < Type, N > array parameter. More...
 
template<typename Type , int N>
FSArrayParam< Type, N > & readOptionalFSArray (std::istream &in, const char *label, FSArray< Type, N > &array, int size)
 Add and read an optional FSArray < Type, N > array parameter. More...
 
template<typename Type >
CArray2DParam< Type > & readCArray2D (std::istream &in, const char *label, Type *value, int m, int n, int np)
 Add and read a required CArray2DParam < Type > 2D C-array. More...
 
template<typename Type >
CArray2DParam< Type > & readOptionalCArray2D (std::istream &in, const char *label, Type *value, int m, int n, int np)
 Add and read an optional CArray2DParam < Type > 2D C-array parameter. More...
 
template<typename Type >
DMatrixParam< Type > & readDMatrix (std::istream &in, const char *label, DMatrix< Type > &matrix, int m, int n)
 Add and read a required DMatrix < Type > matrix parameter. More...
 
template<typename Type >
DMatrixParam< Type > & readOptionalDMatrix (std::istream &in, const char *label, DMatrix< Type > &matrix, int m, int n)
 Add and read an optional DMatrix < Type > matrix parameter. More...
 
template<typename Type >
DSymmMatrixParam< Type > & readDSymmMatrix (std::istream &in, const char *label, DMatrix< Type > &matrix, int n)
 Add and read a required symmetrix DMatrix. More...
 
template<typename Type >
DSymmMatrixParam< Type > & readOptionalDSymmMatrix (std::istream &in, const char *label, DMatrix< Type > &matrix, int n)
 Add and read an optional DMatrix matrix parameter. More...
 
BeginreadBegin (std::istream &in, const char *label, bool isRequired=true)
 Add and read a class label and opening bracket. More...
 
EndreadEnd (std::istream &in)
 Add and read the closing bracket. More...
 
BlankreadBlank (std::istream &in)
 Add and read a new Blank object, representing a blank line. More...
 
void loadParamComposite (Serializable::IArchive &ar, ParamComposite &child, bool next=true)
 Add and load a required child ParamComposite. More...
 
void loadParamCompositeOptional (Serializable::IArchive &ar, ParamComposite &child, bool next=true)
 Add and load an optional child ParamComposite if isActive. More...
 
template<typename Type >
ScalarParam< Type > & loadParameter (Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
 Add and load a new ScalarParam < Type > object. More...
 
template<typename Type >
ScalarParam< Type > & loadParameter (Serializable::IArchive &ar, const char *label, Type &value)
 Add and load new required ScalarParam < Type > object. More...
 
template<typename Type >
CArrayParam< Type > & loadCArray (Serializable::IArchive &ar, const char *label, Type *value, int n, bool isRequired)
 Add a C array parameter and load its elements. More...
 
template<typename Type >
CArrayParam< Type > & loadCArray (Serializable::IArchive &ar, const char *label, Type *value, int n)
 Add and load a required CArrayParam< Type > array parameter. More...
 
template<typename Type >
DArrayParam< Type > & loadDArray (Serializable::IArchive &ar, const char *label, DArray< Type > &array, int n, bool isRequired)
 Add an load a DArray < Type > array parameter. More...
 
template<typename Type >
DArrayParam< Type > & loadDArray (Serializable::IArchive &ar, const char *label, DArray< Type > &array, int n)
 Add and load a required DArray< Type > array parameter. More...
 
template<typename Type , int N>
FArrayParam< Type, N > & loadFArray (Serializable::IArchive &ar, const char *label, FArray< Type, N > &array, bool isRequired)
 Add and load an FArray < Type, N > fixed-size array parameter. More...
 
template<typename Type , int N>
FArrayParam< Type, N > & loadFArray (Serializable::IArchive &ar, const char *label, FArray< Type, N > &array)
 Add and load a required FArray < Type > array parameter. More...
 
template<typename Type , int N>
FSArrayParam< Type, N > & loadFSArray (Serializable::IArchive &ar, const char *label, FSArray< Type, N > &array, int size, bool isRequired)
 Add and load an FSArray < Type, N > array parameter. More...
 
template<typename Type , int N>
FSArrayParam< Type, N > & loadFSArray (Serializable::IArchive &ar, const char *label, FSArray< Type, N > &array, int size)
 Add and load a required FSArray < Type > array parameter. More...
 
template<typename Type >
CArray2DParam< Type > & loadCArray2D (Serializable::IArchive &ar, const char *label, Type *value, int m, int n, int np, bool isRequired)
 Add and load a CArray2DParam < Type > C 2D array parameter. More...
 
template<typename Type >
CArray2DParam< Type > & loadCArray2D (Serializable::IArchive &ar, const char *label, Type *value, int m, int n, int np)
 Add and load a required < Type > matrix parameter. More...
 
template<typename Type >
DMatrixParam< Type > & loadDMatrix (Serializable::IArchive &ar, const char *label, DMatrix< Type > &matrix, int m, int n, bool isRequired)
 Add and load a DMatrixParam < Type > matrix parameter. More...
 
template<typename Type >
DMatrixParam< Type > & loadDMatrix (Serializable::IArchive &ar, const char *label, DMatrix< Type > &matrix, int m, int n)
 Add and load a required DMatrixParam < Type > matrix parameter. More...
 
template<typename Type >
DSymmMatrixParam< Type > & loadDSymmMatrix (Serializable::IArchive &ar, const char *label, DMatrix< Type > &matrix, int n, bool isRequired)
 Add and load a symmetric DSymmMatrixParam < Type > matrix parameter. More...
 
template<typename Type >
DSymmMatrixParam< Type > & loadDSymmMatrix (Serializable::IArchive &ar, const char *label, DMatrix< Type > &matrix, int n)
 Add and load a required DSymmMatrixParam < Type > matrix parameter. More...
 
void addParamComposite (ParamComposite &child, bool next=true)
 Add a child ParamComposite object to the format array. More...
 
BeginaddBegin (const char *label)
 Add a Begin object representing a class name and bracket. More...
 
EndaddEnd ()
 Add a closing bracket. More...
 
BlankaddBlank ()
 Create and add a new Blank object, representing a blank line. More...
 
std::string className () const
 Get class name string. More...
 
bool isRequired () const
 Is this ParamComposite required in the input file? More...
 
bool isActive () const
 Is this parameter active? More...
 
- Public Member Functions inherited from Util::ParamComponent
virtual ~ParamComponent ()
 Destructor. More...
 
virtual void readParam (std::istream &in)=0
 Read parameter(s) from file. More...
 
virtual void writeParam (std::ostream &out) const =0
 Read parameter(s) to file. More...
 
virtual void load (Serializable::IArchive &ar)
 Load internal state from an archive. More...
 
virtual void save (Serializable::OArchive &ar)
 Save internal state to an archive. More...
 
virtual void resetParam ()
 Nontrivial implementation provided by ParamComposite subclass. More...
 
void setIndent (const ParamComponent &parent, bool next=true)
 Set indent level. More...
 
std::string indent () const
 Return indent string for this object (string of spaces). More...
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize this ParamComponent as a string. More...
 
- Public Member Functions inherited from Util::Serializable
virtual ~Serializable ()
 Destructor. More...
 
virtual void save (OArchive &ar)=0
 Save to an archive. More...
 
virtual void load (IArchive &ar)=0
 Load from an archive. More...
 
- Public Member Functions inherited from Util::MpiFileIo
 MpiFileIo ()
 Constructor. More...
 
 MpiFileIo (const MpiFileIo &other)
 Copy constructor. More...
 
bool isIoProcessor () const
 Can this processor do file I/O ? More...
 
void setIoCommunicator (MPI::Intracomm &communicator)
 Set the communicator. More...
 
void clearCommunicator ()
 Clear (nullify) the communicator. More...
 
bool hasIoCommunicator () const
 Does this object have an associated MPI communicator? More...
 
MPI::Intracomm & ioCommunicator () const
 Get the MPI communicator by reference. More...
 

Additional Inherited Members

- Public Types inherited from Util::Serializable
typedef BinaryFileOArchive OArchive
 Type of output archive used by save method. More...
 
typedef BinaryFileIArchive IArchive
 Type of input archive used by load method. More...
 
- Static Public Member Functions inherited from Util::ParamComponent
static void initStatic ()
 Initialize static echo member to false. More...
 
static void setEcho (bool echo=true)
 Enable or disable echoing for all subclasses of ParamComponent. More...
 
static bool echo ()
 Get echo parameter. More...
 
- Protected Member Functions inherited from Util::ParamComposite
void setClassName (const char *className)
 Set class name string. More...
 
void setIsRequired (bool isRequired)
 Set or unset the isActive flag. More...
 
void setIsActive (bool isActive)
 Set or unset the isActive flag. More...
 
void setParent (ParamComponent &param, bool next=true)
 Set this to the parent of a child component. More...
 
void addComponent (ParamComponent &param, bool isLeaf=true)
 Add a new ParamComponent object to the format array. More...
 
template<typename Type >
ScalarParam< Type > & add (std::istream &in, const char *label, Type &value, bool isRequired=true)
 Add a new required ScalarParam < Type > object. More...
 
template<typename Type >
CArrayParam< Type > & addCArray (std::istream &in, const char *label, Type *value, int n, bool isRequired=true)
 Add (but do not read) a required C array parameter. More...
 
template<typename Type >
DArrayParam< Type > & addDArray (std::istream &in, const char *label, DArray< Type > &array, int n, bool isRequired=true)
 Add (but do not read) a DArray < Type > parameter. More...
 
template<typename Type , int N>
FArrayParam< Type, N > & addFArray (std::istream &in, const char *label, FArray< Type, N > &array, bool isRequired=true)
 Add (but do not read) a FArray < Type, N > array parameter. More...
 
template<typename Type , int N>
FSArrayParam< Type, N > & addFSArray (std::istream &in, const char *label, FSArray< Type, N > &array, int size, bool isRequired=true)
 Add (but do not read) a FSArray < Type, N > array parameter. More...
 
template<typename Type >
CArray2DParam< Type > & addCArray2D (std::istream &in, const char *label, Type *value, int m, int n, int np, bool isRequired=true)
 Add (but do not read) a CArray2DParam < Type > 2D C-array. More...
 
template<typename Type >
DMatrixParam< Type > & addDMatrix (std::istream &in, const char *label, DMatrix< Type > &matrix, int m, int n, bool isRequired=true)
 Add and read a required DMatrix < Type > matrix parameter. More...
 
- Protected Member Functions inherited from Util::ParamComponent
 ParamComponent ()
 Constructor. More...
 
 ParamComponent (const ParamComponent &other)
 Copy constructor. More...
 

Detailed Description

Random number generator.

This class provides functions that return several forms of random numbers, using an internal Mersenne-Twister random number generator.

The generator may be seeded either by reading a seed from file, using the readParam() method, or by using setSeed() to set or reset it explicitly. In either case, inputting a positive integer causes that value to be used as a seed, but inputting a value of 0 causes the use of a seed that is generated from the system clock.

If the program is compiled with MPI, and MPI is initialized, then any automatically generated seed is also offset by a value that depends on the rank of the processor within the MPI world communicator, so that different processor use different seeds.

Definition at line 46 of file Random.h.

Constructor & Destructor Documentation

◆ Random()

Util::Random::Random ( )

Constructor.

Definition at line 13 of file Random.cpp.

References Util::ParamComposite::setClassName().

◆ ~Random()

Util::Random::~Random ( )
virtual

Destructor.

Definition at line 22 of file Random.cpp.

Member Function Documentation

◆ readParameters()

void Util::Random::readParameters ( std::istream &  in)
virtual

Read seed from file, initialize RNG.

Parameters
ininput stream.

Reimplemented from Util::ParamComposite.

Definition at line 28 of file Random.cpp.

References setSeed().

◆ loadParameters()

void Util::Random::loadParameters ( Serializable::IArchive ar)
virtual

Load internal state from file.

Parameters
arinput/loading archive

Reimplemented from Util::ParamComposite.

Definition at line 37 of file Random.cpp.

◆ save()

void Util::Random::save ( Serializable::OArchive ar)
virtual

Save internal state to file.

Parameters
aroutput/saving archive

Reimplemented from Util::ParamComposite.

Definition at line 46 of file Random.cpp.

◆ setSeed()

void Util::Random::setSeed ( Random::SeedType  seed)

Sets of random seed, and initializes random number generator.

Parameters
seedvalue for random seed (private member variable idum)

Definition at line 57 of file Random.cpp.

References seed(), and setSeed().

Referenced by readParameters(), and setSeed().

◆ uniform() [1/2]

double Util::Random::uniform ( )
inline

Return a random floating point number x, uniformly distributed in the range 0 <= x < 1.

Returns
random double precision number

Definition at line 203 of file Random.h.

Referenced by drawFrom(), gaussian(), getPoint(), metropolis(), uniformInt(), and unitVector().

◆ uniform() [2/2]

double Util::Random::uniform ( double  range1,
double  range2 
)
inline

Return a random floating point number x, uniformly distributed in the range range1 <= x < range2.

Returns
random double precision number

Definition at line 212 of file Random.h.

◆ uniformInt()

long Util::Random::uniformInt ( long  range1,
long  range2 
)
inline

Return random long int x uniformly distributed in range1 <= x < range2.

Parameters range1 and range2 must be within the range of long integers.

Returns
random integer

Definition at line 224 of file Random.h.

References uniform().

◆ getPoint()

void Util::Random::getPoint ( double  minR[],
double  maxR[],
double  r[] 
)
inline

Generate a random point in a box.

Parameters
minR[]array of minimum coordinate values along three axes
maxR[]array of maximum coordinate values along three axes
r[]random position such that minR[axis] < r[axis] < maxR[axis]

Definition at line 251 of file Random.h.

References uniform().

◆ gaussian()

double Util::Random::gaussian ( void  )

Return a Gaussian random number with zero average and unit variance.

Returns
Gaussian distributed random number.

Definition at line 92 of file Random.cpp.

References uniform().

Referenced by Util::Ar1Process::init(), and Util::Ar1Process::operator()().

◆ unitVector()

void Util::Random::unitVector ( Vector v)

Generate unit vector with uniform probability over the unit sphere.

Parameters
vrandom unit vector (upon return)

Definition at line 122 of file Random.cpp.

References uniform().

◆ metropolis()

bool Util::Random::metropolis ( double  ratio)
inline

Metropolis algorithm for whether to accept a MC move.

If ratio > 1, this function return true. If 0 < ratio < 1, this function returns true with probability ratio, and false with probability 1 - ratio.

Parameters
ratioratio of old to new equilibrium weights
Returns
true if accepted, false if rejected

Definition at line 260 of file Random.h.

References uniform().

◆ drawFrom()

long Util::Random::drawFrom ( double  probability[],
long  size 
)
inline

Choose one of several outcomes with a specified set of probabilities.

Precondition: Elements of probability array must add to 1.0

Parameters
probability[]array of probabilities, for indices 0,...,size-1
sizenumber of options
Returns
random integer index of element of probability[] array

Definition at line 237 of file Random.h.

References uniform().

◆ serialize()

template<class Archive >
void Util::Random::serialize ( Archive &  ar,
const unsigned int  version 
)

Serialize to/from an archive.

Definition at line 284 of file Random.h.

◆ seed()

long Util::Random::seed ( )
inline

Returns value of random seed (private member variable seed_).

Returns
value of random number generator seed.

Definition at line 277 of file Random.h.

Referenced by setSeed().


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