queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
RngCXX11.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/RngCXX11.h>
26 
27 #ifdef QUESO_HAVE_CXX11
28 
29 namespace QUESO {
30 
31 RngCXX11::RngCXX11(int seed, int worldRank)
32  :
33  RngBase(seed,worldRank),
34  m_rng((unsigned int)m_seed) // m_seed set in the base class. It is always
35  // positive
36 {
37 }
38 
40 {
41 }
42 
43 void
44 RngCXX11::resetSeed(int newSeed)
45 {
46  RngBase::resetSeed(newSeed);
47  m_rng.seed(m_seed); // m_seed was set by the previous line
48 }
49 
50 double
52 {
53  std::uniform_real_distribution<double> d(0.0, 1.0);
54  return d(m_rng);
55 }
56 
57 double
58 RngCXX11::gaussianSample(double stdDev) const
59 {
60  std::normal_distribution<double> d(0.0, stdDev);
61  return d(m_rng);
62 }
63 
64 double
65 RngCXX11::betaSample(double alpha, double beta) const
66 {
67  std::gamma_distribution<double> d1(alpha, 1.0);
68  std::gamma_distribution<double> d2(beta, 1.0);
69 
70  double x = d1(m_rng); // x ~ \Gamma(alpha, 1)
71  double y = d2(m_rng); // y ~ \Gamma(beta, 1)
72 
73  // x / (x + y) ~ Beta(alpha, beta)
74  // See https://en.wikipedia.org/wiki/Beta_distribution#Generating_beta-distributed_random_variates
75  return x / (x + y);
76 }
77 
78 double
79 RngCXX11::gammaSample(double a, double b) const
80 {
81  std::gamma_distribution<double> d(a, b);
82  return d(m_rng);
83 }
84 
85 } // End namespace QUESO
86 
87 #endif // QUESO_HAVE_CXX11
virtual void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngBase.C:48
double gaussianSample(double stdDev) const
Definition: RngCXX11.C:58
int m_seed
Seed.
Definition: RngBase.h:82
RngCXX11()
Default Constructor: it should not be used.
Class for random number generation (base class for either GSL or Boost RNG).
Definition: RngBase.h:45
double gammaSample(double a, double b) const
Samples a value from a Gamma distribution. Support: [0,infinity).
Definition: RngCXX11.C:79
double uniformSample() const
Samples a value from a uniform distribution. Support: [0,1) or [a,b).
Definition: RngCXX11.C:51
void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngCXX11.C:44
std::mt19937 m_rng
The internal random number generator. Mersenne Twister generator.
Definition: RngCXX11.h:117
~RngCXX11()
Destructor.
Definition: RngCXX11.C:39
double betaSample(double alpha, double beta) const
Samples a value from a Beta distribution. Support: [0,1].
Definition: RngCXX11.C:65

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5