SST  15.1.0
StructuralSimulationToolkit
gaussian.h
1 // Copyright 2009-2025 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-2025, 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 namespace SST::RNG {
21 
22 /**
23  \class GaussianDistribution gaussian.h "sst/core/rng/gaussian.h"
24 
25  Creates a Gaussian (normal) distribution for which to sample
26 */
28 {
29 
30 public:
31  /**
32  Creates a new distribution with a predefined random number generator with a specified mean and standard
33  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
34  distribution
35  */
36  GaussianDistribution(double mn, double sd) :
38  {
39 
40  mean = mn;
41  stddev = sd;
42 
43  baseDistrib = new MersenneRNG();
44  unusedPair = 0;
45  usePair = false;
46  deleteDistrib = true;
47  }
48 
49  /**
50  Creates a new distribution with a predefined random number generator with a specified mean and standard
51  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
52  distribution \param baseRNG The random number generator as the base of the distribution
53  */
54  GaussianDistribution(double mn, double sd, Random* baseRNG) :
56  {
57 
58  mean = mn;
59  stddev = sd;
60 
61  baseDistrib = baseRNG;
62  unusedPair = 0;
63  usePair = false;
64  deleteDistrib = false;
65  }
66 
67  /**
68  Destroys the Gaussian distribution.
69  */
71  {
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() override
82  {
83  if ( usePair ) {
84  usePair = false;
85  return unusedPair;
86  }
87  else {
88  double gauss_u, gauss_v, sq_sum;
89 
90  do {
91  gauss_u = baseDistrib->nextUniform();
92  gauss_v = baseDistrib->nextUniform();
93  sq_sum = (gauss_u * gauss_u) + (gauss_v * gauss_v);
94  } while ( sq_sum >= 1 || sq_sum == 0 );
95 
96  if ( baseDistrib->nextUniform() < 0.5 ) {
97  gauss_u *= -1.0;
98  }
99 
100  if ( baseDistrib->nextUniform() < 0.5 ) {
101  gauss_v *= -1.0;
102  }
103 
104  double multiplier = sqrt(-2.0 * log(sq_sum) / sq_sum);
105  unusedPair = mean + stddev * gauss_v * multiplier;
106  usePair = true;
107 
108  return mean + stddev * gauss_u * multiplier;
109  }
110  }
111 
112  /**
113  Gets the mean of the distribution
114  \return The mean of the Guassian distribution
115  */
116  double getMean() { return mean; }
117 
118  /**
119  Gets the standard deviation of the distribution
120  \return The standard deviation of the Gaussian distribution
121  */
122  double getStandardDev() { return stddev; }
123 
124  /**
125  Default constructor. FOR SERIALIZATION ONLY.
126  */
129  {}
130 
131  /**
132  Serialization function for checkpoint
133  */
135  {
136  SST_SER(mean);
137  SST_SER(stddev);
138  SST_SER(baseDistrib);
139  SST_SER(unusedPair);
140  SST_SER(usePair);
141  SST_SER(deleteDistrib);
142  }
143 
144  /**
145  Serialization macro
146  */
147  ImplementSerializable(SST::RNG::GaussianDistribution)
148 
149 protected:
150  /**
151  The mean of the Gaussian distribution
152  */
153  double mean;
154  /**
155  The standard deviation of the Gaussian distribution
156  */
157  double stddev;
158  /**
159  The base random number generator for the distribution
160  */
162  /**
163  Random numbers for the distribution are read in pairs, this stores the second of the pair
164  */
165  double unusedPair;
166  /**
167  Random numbers for the distribution are read in pairs, this tells the code to use the second of the pair
168  */
169  bool usePair;
170 
171  /**
172  Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created the
173  distribution)
174  */
176 };
177 
178 } // namespace SST::RNG
179 
181 
182 #endif // SST_CORE_RNG_GAUSSIAN_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:29
GaussianDistribution(double mn, double sd, Random *baseRNG)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:54
Definition: constant.h:18
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
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:169
bool deleteDistrib
Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created ...
Definition: gaussian.h:175
ImplementSerializable(SST::RNG::GaussianDistribution) protected double stddev
Serialization macro.
Definition: gaussian.h:147
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
Random * baseDistrib
The base random number generator for the distribution.
Definition: gaussian.h:161
~GaussianDistribution()
Destroys the Gaussian distribution.
Definition: gaussian.h:70
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition: gaussian.h:134
double getNextDouble() override
Gets the next double value in the distribution.
Definition: gaussian.h:81
GaussianDistribution()
Default constructor.
Definition: gaussian.h:127
Creates a Gaussian (normal) distribution for which to sample.
Definition: gaussian.h:27
double getMean()
Gets the mean of the distribution.
Definition: gaussian.h:116
double getStandardDev()
Gets the standard deviation of the distribution.
Definition: gaussian.h:122
double unusedPair
Random numbers for the distribution are read in pairs, this stores the second of the pair...
Definition: gaussian.h:165
Base class of statistical distributions in SST.
Definition: distrib.h:23
virtual double nextUniform()=0
Generates the next random number in the range [0,1).
GaussianDistribution(double mn, double sd)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:36