SST  12.0.0
StructuralSimulationToolkit
uniform.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_UNIFORM_H
13 #define SST_CORE_RNG_UNIFORM_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 UniformDistribution uniform.h "sst/core/rng/uniform.h"
27 
28  Creates a Uniform distribution for use within SST. This distribution is the same across
29  platforms and compilers.
30 */
32 {
33 
34 public:
35  /**
36  Creates an uniform distribution with a specific number of bins
37  \param probsCount Number of probability bins in this distribution
38  */
39  UniformDistribution(const uint32_t probsCount) : RandomDistribution(), probCount(probsCount), deleteDistrib(true)
40  {
41 
42  if ( probCount > 0 ) { probPerBin = 1.0 / static_cast<double>(probCount); }
43 
44  baseDistrib = new MersenneRNG();
45  }
46 
47  /**
48  Creates a Uniform distribution with a specific number of bins and user supplied
49  random number generator
50  \param probsCount Number of probability bins in the distribution
51  \param baseDist The base random number generator to take the distribution from.
52  */
53  UniformDistribution(const uint32_t probsCount, SST::RNG::Random* baseDist) :
55  probCount(probsCount),
56  deleteDistrib(false)
57  {
58 
59  if ( probCount > 0 ) { probPerBin = 1.0 / static_cast<double>(probCount); }
60 
61  baseDistrib = baseDist;
62  }
63 
64  /**
65  Destroys the distribution and will delete locally allocated RNGs
66  */
68  {
69  if ( deleteDistrib ) { delete baseDistrib; }
70  }
71 
72  /**
73  Gets the next (random) double value in the distribution
74  \return The next random double from the distribution, this is the double converted of the index where the
75  probability is located
76  */
77  double getNextDouble()
78  {
79  const double nextD = baseDistrib->nextUniform();
80  uint32_t current_bin = 1;
81 
82  while ( nextD > (static_cast<double>(current_bin) * probPerBin) ) {
83  current_bin++;
84  }
85 
86  return static_cast<double>(current_bin - 1);
87  }
88 
89 protected:
90  /**
91  Sets the base random number generator for the distribution.
92  */
94 
95  /**
96  Controls whether the base distribution should be deleted when this class is destructed.
97  */
98  const bool deleteDistrib;
99 
100  /**
101  Count of discrete probabilities
102  */
103  const uint32_t probCount;
104 
105  /**
106  Range 0..1 split into discrete bins
107  */
108  double probPerBin;
109 };
110 
112 
113 } // namespace RNG
114 } // namespace SST
115 
116 #endif // SST_CORE_RNG_UNIFORM_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:27
double getNextDouble()
Gets the next (random) double value in the distribution.
Definition: uniform.h:77
double probPerBin
Range 0..1 split into discrete bins.
Definition: uniform.h:108
Creates a Uniform distribution for use within SST.
Definition: uniform.h:31
SST::RNG::Random * baseDistrib
Sets the base random number generator for the distribution.
Definition: uniform.h:93
UniformDistribution(const uint32_t probsCount)
Creates an uniform distribution with a specific number of bins.
Definition: uniform.h:39
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
~UniformDistribution()
Destroys the distribution and will delete locally allocated RNGs.
Definition: uniform.h:67
const uint32_t probCount
Count of discrete probabilities.
Definition: uniform.h:103
UniformDistribution(const uint32_t probsCount, SST::RNG::Random *baseDist)
Creates a Uniform distribution with a specific number of bins and user supplied random number generat...
Definition: uniform.h:53
Base class of statistical distributions in SST.
Definition: distrib.h:22
const bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: uniform.h:98