SST  9.0.0
StructuralSimulationToolkit
gaussian.h
1 // Copyright 2009-2019 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2019, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef _H_SST_CORE_RNG_GAUSSIAN
13 #define _H_SST_CORE_RNG_GAUSSIAN
14 
15 #include "math.h"
16 
17 #include "distrib.h"
18 #include "mersenne.h"
19 
20 using namespace SST::RNG;
21 
22 namespace SST {
23 namespace RNG {
24 
25 /**
26  \class SSTGaussianDistribution gaussian.h "sst/core/rng/gaussian.h"
27 
28  Creates a Gaussian (normal) distribution for which to sample
29 */
31 
32  public:
33  /**
34  Creates a new distribution with a predefined random number generator with a specified mean and standard deviation.
35  \param mn The mean of the Gaussian distribution
36  \param sd The standard deviation of the Gaussian distribution
37  */
38  SSTGaussianDistribution(double mn, double sd) :
40 
41  mean = mn;
42  stddev = sd;
43 
44  baseDistrib = new MersenneRNG();
45  unusedPair = 0;
46  usePair = false;
47  deleteDistrib = true;
48  }
49 
50  /**
51  Creates a new distribution with a predefined random number generator with a specified mean and standard deviation.
52  \param mn The mean of the Gaussian distribution
53  \param sd The standard deviation of the Gaussian distribution
54  \param baseRNG The random number generator as the base of the distribution
55  */
56  SSTGaussianDistribution(double mn, double sd, SSTRandom* baseRNG) :
58 
59  mean = mn;
60  stddev = sd;
61 
62  baseDistrib = baseRNG;
63  unusedPair = 0;
64  usePair = false;
65  deleteDistrib = false;
66  }
67 
68  /**
69  Destroys the Gaussian distribution.
70  */
72  if(deleteDistrib) {
73  delete baseDistrib;
74  }
75  }
76 
77  /**
78  Gets the next double value in the distribution
79  \return The next double value of the distribution (in this case a Gaussian distribution)
80  */
81  double getNextDouble() {
82  if(usePair) {
83  usePair = false;
84  return unusedPair;
85  } else {
86  double gauss_u, gauss_v, sq_sum;
87 
88  do {
89  gauss_u = baseDistrib->nextUniform();
90  gauss_v = baseDistrib->nextUniform();
91  sq_sum = (gauss_u * gauss_u) + (gauss_v * gauss_v);
92  } while(sq_sum >= 1 || sq_sum == 0);
93 
94  if(baseDistrib->nextUniform() < 0.5) {
95  gauss_u *= -1.0;
96  }
97 
98  if(baseDistrib->nextUniform() < 0.5) {
99  gauss_v *= -1.0;
100  }
101 
102  double multiplier = sqrt(-2.0 * log(sq_sum) / sq_sum);
103  unusedPair = mean + stddev * gauss_v * multiplier;
104  usePair = true;
105 
106  return mean + stddev * gauss_u * multiplier;
107  }
108  }
109 
110  /**
111  Gets the mean of the distribution
112  \return The mean of the Guassian distribution
113  */
114  double getMean() {
115  return mean;
116  }
117 
118  /**
119  Gets the standard deviation of the distribution
120  \return The standard deviation of the Gaussian distribution
121  */
122  double getStandardDev() {
123  return stddev;
124  }
125 
126  protected:
127  /**
128  The mean of the Gaussian distribution
129  */
130  double mean;
131  /**
132  The standard deviation of the Gaussian distribution
133  */
134  double stddev;
135  /**
136  The base random number generator for the distribution
137  */
139  /**
140  Random numbers for the distribution are read in pairs, this stores the second of the pair
141  */
142  double unusedPair;
143  /**
144  Random numbers for the distribution are read in pairs, this tells the code to use the second of the pair
145  */
146  bool usePair;
147 
148  /**
149  Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created the distribution)
150  */
152 };
153 
154 }
155 }
156 
157 #endif
Base class of statistical distributions in SST.
Definition: distrib.h:24
double getStandardDev()
Gets the standard deviation of the distribution.
Definition: gaussian.h:122
double getMean()
Gets the mean of the distribution.
Definition: gaussian.h:114
~SSTGaussianDistribution()
Destroys the Gaussian distribution.
Definition: gaussian.h:71
bool deleteDistrib
Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created ...
Definition: gaussian.h:151
double stddev
The standard deviation of the Gaussian distribution.
Definition: gaussian.h:134
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
Creates a Gaussian (normal) distribution for which to sample.
Definition: gaussian.h:30
bool usePair
Random numbers for the distribution are read in pairs, this tells the code to use the second of the p...
Definition: gaussian.h:146
SSTGaussianDistribution(double mn, double sd)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:38
SSTGaussianDistribution(double mn, double sd, SSTRandom *baseRNG)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:56
double getNextDouble()
Gets the next double value in the distribution.
Definition: gaussian.h:81
SSTRandom * baseDistrib
The base random number generator for the distribution.
Definition: gaussian.h:138
double unusedPair
Random numbers for the distribution are read in pairs, this stores the second of the pair...
Definition: gaussian.h:142
Implements the base class for random number generators for the SST core.
Definition: sstrng.h:27
double mean
The mean of the Gaussian distribution.
Definition: gaussian.h:130