queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
RngGsl.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/Defines.h>
26 #include <queso/RngGsl.h>
27 #include <gsl/gsl_randist.h>
28 
29 namespace QUESO {
30 
32 RngGsl::RngGsl(int seed, int worldRank)
33  :
34  RngBase(seed,worldRank),
35  m_rng (NULL)
36 {
37  gsl_rng_default_seed = (unsigned long int) m_seed;
38  m_rng = gsl_rng_alloc(gsl_rng_ranlxd2);
39  queso_require_msg(m_rng, "null m_rng");
40 
41 //gsl_rng_set(m_rng, gsl_rng_default_seed);
42 #if 0
43  if (m_worldRank == 0) {
44  std::cout << "In RngGsl::constructor():"
45  << "\n m_seed = " << m_seed
46  << "\n internal seed = " << gsl_rng_default_seed
47  //<< "\n first generated sample from uniform distribution = " << gsl_rng_uniform(m_rng)
48  //<< "\n first generated sample from std normal distribution = " << gsl_ran_gaussian(m_rng,1.)
49  << std::endl;
50  }
51 #endif
52 }
53 
54 // Destructor ---------------------------------------
56 {
57  if (m_rng) gsl_rng_free(m_rng);
58 }
59 
60 // Sampling methods ---------------------------------
61 void
62 RngGsl::resetSeed(int newSeed)
63 {
64  RngBase::resetSeed(newSeed);
65  gsl_rng_free(m_rng);
66 
67  gsl_rng_default_seed = (unsigned long int) m_seed;
68  m_rng = gsl_rng_alloc(gsl_rng_ranlxd2);
69  queso_require_msg(m_rng, "null m_rng");
70  return;
71 }
72 
73 // --------------------------------------------------
74 double
76 {
77  return gsl_rng_uniform(m_rng);
78 }
79 
80 // --------------------------------------------------
81 double
82 RngGsl::gaussianSample(double stdDev) const
83 {
84  return gsl_ran_gaussian(m_rng,stdDev);
85 }
86 
87 // --------------------------------------------------
88 double
89 RngGsl::betaSample(double alpha, double beta) const
90 {
91  return gsl_ran_beta(m_rng,alpha,beta);
92 }
93 
94 // --------------------------------------------------
95 double
96 RngGsl::gammaSample(double a, double b) const
97 {
98  return gsl_ran_gamma(m_rng,a,b);
99 }
100 
101 const gsl_rng*
102 RngGsl::rng() const
103 {
104  return m_rng;
105 }
106 
107 } // End namespace QUESO
virtual void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngBase.C:48
void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngGsl.C:62
const gsl_rng * rng() const
GSL random number generator.
Definition: RngGsl.C:102
int m_seed
Seed.
Definition: RngBase.h:82
double uniformSample() const
Samples a value from a uniform distribution. Support: [0,1] or [a,b].
Definition: RngGsl.C:75
double gammaSample(double a, double b) const
Samples a value from a Gamma distribution. Support: [0,infinity).
Definition: RngGsl.C:96
gsl_rng * m_rng
GSL random number generator.
Definition: RngGsl.h:113
~RngGsl()
Destructor.
Definition: RngGsl.C:55
Class for random number generation (base class for either GSL or Boost RNG).
Definition: RngBase.h:45
double gaussianSample(double stdDev) const
Definition: RngGsl.C:82
int m_worldRank
Rank of processor.
Definition: RngBase.h:85
RngGsl()
Default Constructor: it should not be used.
double betaSample(double alpha, double beta) const
Samples a value from a Beta distribution. Support: [0,1].
Definition: RngGsl.C:89
unsigned long int gsl_rng_default_seed

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