SST  12.0.0
StructuralSimulationToolkit
gaussian.h
1 // Copyright 2009-2022 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-2022, 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 SST_CORE_RNG_GAUSSIAN_H
13 #define SST_CORE_RNG_GAUSSIAN_H
14 
15 #include "distrib.h"
16 #include "math.h"
17 #include "mersenne.h"
18 #include "rng.h"
19 
20 using namespace SST::RNG;
21 
22 namespace SST {
23 namespace RNG {
24 
25 /**
26  \class GaussianDistribution gaussian.h "sst/core/rng/gaussian.h"
27 
28  Creates a Gaussian (normal) distribution for which to sample
29 */
31 {
32 
33 public:
34  /**
35  Creates a new distribution with a predefined random number generator with a specified mean and standard
36  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
37  distribution
38  */
39  GaussianDistribution(double mn, double sd) : RandomDistribution()
40  {
41 
42  mean = mn;
43  stddev = sd;
44 
45  baseDistrib = new MersenneRNG();
46  unusedPair = 0;
47  usePair = false;
48  deleteDistrib = true;
49  }
50 
51  /**
52  Creates a new distribution with a predefined random number generator with a specified mean and standard
53  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
54  distribution \param baseRNG The random number generator as the base of the distribution
55  */
56  GaussianDistribution(double mn, double sd, SST::RNG::Random* baseRNG) : RandomDistribution()
57  {
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  {
73  if ( deleteDistrib ) { delete baseDistrib; }
74  }
75 
76  /**
77  Gets the next double value in the distribution
78  \return The next double value of the distribution (in this case a Gaussian distribution)
79  */
80  double getNextDouble()
81  {
82  if ( usePair ) {
83  usePair = false;
84  return unusedPair;
85  }
86  else {
87  double gauss_u, gauss_v, sq_sum;
88 
89  do {
90  gauss_u = baseDistrib->nextUniform();
91  gauss_v = baseDistrib->nextUniform();
92  sq_sum = (gauss_u * gauss_u) + (gauss_v * gauss_v);
93  } while ( sq_sum >= 1 || sq_sum == 0 );
94 
95  if ( baseDistrib->nextUniform() < 0.5 ) { gauss_u *= -1.0; }
96 
97  if ( baseDistrib->nextUniform() < 0.5 ) { gauss_v *= -1.0; }
98 
99  double multiplier = sqrt(-2.0 * log(sq_sum) / sq_sum);
100  unusedPair = mean + stddev * gauss_v * multiplier;
101  usePair = true;
102 
103  return mean + stddev * gauss_u * multiplier;
104  }
105  }
106 
107  /**
108  Gets the mean of the distribution
109  \return The mean of the Guassian distribution
110  */
111  double getMean() { return mean; }
112 
113  /**
114  Gets the standard deviation of the distribution
115  \return The standard deviation of the Gaussian distribution
116  */
117  double getStandardDev() { return stddev; }
118 
119 protected:
120  /**
121  The mean of the Gaussian distribution
122  */
123  double mean;
124  /**
125  The standard deviation of the Gaussian distribution
126  */
127  double stddev;
128  /**
129  The base random number generator for the distribution
130  */
132  /**
133  Random numbers for the distribution are read in pairs, this stores the second of the pair
134  */
135  double unusedPair;
136  /**
137  Random numbers for the distribution are read in pairs, this tells the code to use the second of the pair
138  */
139  bool usePair;
140 
141  /**
142  Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created the
143  distribution)
144  */
146 };
147 
149 
150 } // namespace RNG
151 } // namespace SST
152 
153 #endif // SST_CORE_RNG_GAUSSIAN_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:27
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:139
bool deleteDistrib
Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created ...
Definition: gaussian.h:145
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
double stddev
The standard deviation of the Gaussian distribution.
Definition: gaussian.h:127
~GaussianDistribution()
Destroys the Gaussian distribution.
Definition: gaussian.h:71
SST::RNG::Random * baseDistrib
The base random number generator for the distribution.
Definition: gaussian.h:131
double getNextDouble()
Gets the next double value in the distribution.
Definition: gaussian.h:80
Creates a Gaussian (normal) distribution for which to sample.
Definition: gaussian.h:30
double mean
The mean of the Gaussian distribution.
Definition: gaussian.h:123
GaussianDistribution(double mn, double sd, SST::RNG::Random *baseRNG)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:56
double getMean()
Gets the mean of the distribution.
Definition: gaussian.h:111
double getStandardDev()
Gets the standard deviation of the distribution.
Definition: gaussian.h:117
double unusedPair
Random numbers for the distribution are read in pairs, this stores the second of the pair...
Definition: gaussian.h:135
Base class of statistical distributions in SST.
Definition: distrib.h:22
GaussianDistribution(double mn, double sd)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:39