SST  9.0.0
StructuralSimulationToolkit
uniform.h
1 // Copyright 2009-2019 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-2019, 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) :
40  probCount(probsCount) {
41 
42  baseDistrib = new MersenneRNG();
43  deleteDistrib = true;
44  }
45 
46  /**
47  Creates a Uniform distribution with a specific number of bins and user supplied
48  random number generator
49  \param probsCount Number of probability bins in the distribution
50  \param baseDist The base random number generator to take the distribution from.
51  */
52  SSTUniformDistribution(const uint32_t probsCount, SSTRandom* baseDist) :
54  probCount(probsCount) {
55 
56  baseDistrib = baseDist;
57  deleteDistrib = false;
58  }
59 
60  /**
61  Destroys the distribution and will delete locally allocated RNGs
62  */
64  if(deleteDistrib) {
65  delete baseDistrib;
66  }
67  }
68 
69  /**
70  Gets the next (random) double value in the distribution
71  \return The next random double from the distribution, this is the double converted of the index where the probability is located
72  */
73  double getNextDouble() {
74  const double nextD = baseDistrib->nextUniform();
75  const double probPerBin = 1.0 / (double)(probCount);
76 
77  for(uint32_t i = 0; i < probCount; ++i) {
78  if(nextD < ( ((double) i) * probPerBin )) {
79  return i;
80  }
81  }
82 
83  return probCount;
84  }
85 
86  protected:
87  /**
88  Sets the base random number generator for the distribution.
89  */
91 
92  /**
93  Controls whether the base distribution should be deleted when this class is destructed.
94  */
96 
97  /**
98  Count of discrete probabilities
99  */
100  uint32_t probCount;
101 
102 };
103 
104 }
105 }
106 
107 #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:63
SSTRandom * baseDistrib
Sets the base random number generator for the distribution.
Definition: uniform.h:90
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
uint32_t probCount
Count of discrete probabilities.
Definition: uniform.h:100
Implements the base class for random number generators for the SST core.
Definition: sstrng.h:27
bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: uniform.h:95
double getNextDouble()
Gets the next (random) double value in the distribution.
Definition: uniform.h:73
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:52