https://mooseframework.inl.gov
MooseRandom.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "MooseError.h"
14 #include "DataIO.h"
15 
16 #include <unordered_map>
17 
18 // External library includes
19 #include "randistrs.h"
20 
38 {
39 public:
44  static inline void seed(unsigned int seed) { mt_seed32new(seed); }
45 
50  static inline Real rand() { return mt_ldrand(); }
51 
60  static inline Real randNormal(Real mean, Real sigma) { return rd_normal(mean, sigma); }
61 
65  static inline Real randNormal() { return randNormal(0.0, 1.0); }
66 
71  static inline uint32_t randl() { return mt_lrand(); }
72 
78  inline void seed(std::size_t i, unsigned int seed) { mts_seed32new(&(_states[i].first), seed); }
79 
85  inline Real rand(std::size_t i)
86  {
87  // mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
88  return mts_ldrand(&(_states[i].first));
89  }
90 
100  inline Real randNormal(std::size_t i, Real mean, Real sigma)
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  }
105 
109  inline Real randNormal(std::size_t i) { return randNormal(i, 0.0, 1.0); }
110 
116  inline uint32_t randl(std::size_t i)
117  {
118  mooseAssert(_states.find(i) != _states.end(), "No random state initialized for id: " << i);
119  return mts_lrand(&(_states[i].first));
120  }
121 
131  inline uint32_t randl(std::size_t i, uint32_t lower, uint32_t upper)
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  }
136 
141  void saveState()
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  }
149 
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  }
161 
165  inline std::size_t size() { return _states.size(); }
166 
167 private:
173  std::unordered_map<std::size_t, std::pair<mt_state, mt_state>> _states;
174 
175  // for restart capability
176  friend void dataStore<MooseRandom>(std::ostream & stream, MooseRandom & v, void * context);
177  friend void dataLoad<MooseRandom>(std::istream & stream, MooseRandom & v, void * context);
178 
180  bool _saved = false;
181 };
182 
183 template <>
184 inline void
185 dataStore(std::ostream & stream, MooseRandom & v, void * context)
186 {
187  storeHelper(stream, v._states, context);
188 }
189 template <>
190 inline void
191 dataLoad(std::istream & stream, MooseRandom & v, void * context)
192 {
193  loadHelper(stream, v._states, context);
194 }
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:173
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:71
void dataStore(std::ostream &stream, MooseRandom &v, void *context)
Definition: MooseRandom.h:185
bool _saved
Flag to make certain that saveState is called prior to restoreState.
Definition: MooseRandom.h:180
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:141
static Real randNormal()
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:65
void seed(std::size_t i, unsigned int seed)
The method seeds one of the independent random number generators.
Definition: MooseRandom.h:78
void dataLoad(std::istream &stream, MooseRandom &v, void *context)
Definition: MooseRandom.h:191
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.
Definition: MooseRandom.h:100
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:893
uint32_t randl(std::size_t i)
This method returns the next random number (long format) from the specified generator.
Definition: MooseRandom.h:116
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:153
std::size_t size()
Return the number of states.
Definition: MooseRandom.h:165
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:44
static Real rand()
This method returns the next random number (Real format) from the generator.
Definition: MooseRandom.h:50
Real randNormal(std::size_t i)
Return next random number drawn from a standard distribution.
Definition: MooseRandom.h:109
Real rand(std::size_t i)
This method returns the next random number (Real format) from the specified generator.
Definition: MooseRandom.h:85
static Real randNormal(Real mean, Real sigma)
This method returns the next random number (Real format) from the generator, drawn from a normal dist...
Definition: MooseRandom.h:60
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:985
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 supp...
Definition: MooseRandom.h:131
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition: MooseRandom.h:37