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