MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RNG.h
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: RNG.h
5  *
6  * Description: Random Number Generator class
7  *
8  * Created: 05/09/2016 12:00:05 PM
9  * Revision: none
10  * Compiler: gcc
11  *
12  * Author: Dilawar Singh (), dilawars@ncbs.res.in
13  * Organization: NCBS Bangalore
14  *
15  * =====================================================================================
16  */
17 
18 
19 #ifndef __RNG_INC
20 #define __RNG_INC
21 
22 #include <limits>
23 #include <iostream>
24 #include <random>
25 
26 #include "Definitions.h"
27 #include "Distributions.h"
28 
29 using namespace std;
30 
31 namespace moose
32 {
33 
34 /*
35  * =====================================================================================
36  * Class: RNG
37  * Description: Random number generator class.
38  * =====================================================================================
39  */
40 
41 template < typename T >
42 class RNG
43 {
44  public:
45  RNG () /* constructor */
46  {
47  // Setup a random seed if possible.
48  setRandomSeed( );
49  }
50 
51  ~RNG () /* destructor */
52  { ; }
53 
54  void setRandomSeed( )
55  {
57  setSeed( rd_() );
58  }
59 
60  /* ==================== ACCESSORS ======================================= */
61  T getSeed( void )
62  {
63  return seed_;
64  }
65 
66  /* ==================== MUTATORS ======================================= */
73  void setSeed( const unsigned long seed )
74  {
75  seed_ = seed;
76  if( seed == 0 )
77  {
79  seed_ = rd_();
80  }
81  rng_.seed( seed_ );
82  }
83 
90  T uniform( const T a, const T b)
91  {
92  return ( b - a ) * dist_( rng_ ) + a;
93  }
94 
101  T uniform( void )
102  {
103  return dist_( rng_ );
104  }
105 
106 
107  private:
108  /* ==================== DATA MEMBERS ======================================= */
109  T res_;
110  T seed_;
111 
114 
115 }; /* ----- end of template class RNG ----- */
116 
117 } /* namespace moose ends */
118 
119 #endif /* ----- #ifndef __RNG_INC ----- */
T uniform(void)
Return a uniformly distributed random number between 0 and 1 (exclusive).
Definition: RNG.h:101
void setRandomSeed()
Definition: RNG.h:54
moose::MOOSE_RNG_DEFAULT_ENGINE rng_
Definition: RNG.h:112
std::mersenne_twister_engine< std::uint_fast32_t, 32, 624, 397, 31, 0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18, 1812433253 > MOOSE_RNG_DEFAULT_ENGINE
Definition: Definitions.h:42
T getSeed(void)
Definition: RNG.h:61
void setSeed(const unsigned long seed)
If seed if 0 then set seed to a random number else set seed to the given number.
Definition: RNG.h:73
RNG()
Definition: RNG.h:45
~RNG()
Definition: RNG.h:51
T res_
Definition: RNG.h:109
std::uniform_real_distribution< T > MOOSE_UNIFORM_DISTRIBUTION
Definition: Distributions.h:26
T seed_
Definition: RNG.h:110
moose::MOOSE_UNIFORM_DISTRIBUTION< double > dist_
Definition: RNG.h:113
T uniform(const T a, const T b)
Generate a uniformly distributed random number between a and b.
Definition: RNG.h:90
std::random_device MOOSE_RANDOM_DEVICE
Definition: Definitions.h:29
Definition: RNG.h:42