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