SST 15.0
Structural Simulation Toolkit
uniform.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_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#include <cstdint>
21
22namespace SST::RNG {
23
24/**
25 \class UniformDistribution uniform.h "sst/core/rng/uniform.h"
26
27 Creates a Uniform distribution for use within SST. This distribution is the same across
28 platforms and compilers.
29*/
31{
32
33public:
34 /**
35 Creates an uniform distribution with a specific number of bins
36 \param probsCount Number of probability bins in this distribution
37 */
38 explicit UniformDistribution(const uint32_t probsCount) :
40 deleteDistrib(true),
41 probCount(probsCount),
42 probPerBin(1)
43 {
44
45 if ( probCount > 0 ) {
46 probPerBin = 1.0 / static_cast<double>(probCount);
47 }
48
49 baseDistrib = new MersenneRNG();
50 }
51
52 /**
53 Creates a Uniform distribution with a specific number of bins and user supplied
54 random number generator
55 \param probsCount Number of probability bins in the distribution
56 \param baseDist The base random number generator to take the distribution from.
57 */
58 UniformDistribution(const uint32_t probsCount, Random* baseDist) :
60 deleteDistrib(false),
61 probCount(probsCount),
62 probPerBin(1)
63 {
64
65 if ( probCount > 0 ) {
66 probPerBin = 1.0 / static_cast<double>(probCount);
67 }
68
69 baseDistrib = baseDist;
70 }
71
72 /**
73 Destroys the distribution and will delete locally allocated RNGs
74 */
76 {
77 if ( deleteDistrib ) {
78 delete baseDistrib;
79 }
80 }
81
82 /**
83 Gets the next (random) double value in the distribution
84 \return The next random double from the distribution, this is the double converted of the index where the
85 probability is located
86 */
87 double getNextDouble() override
88 {
89 const double nextD = baseDistrib->nextUniform();
90 uint32_t current_bin = 1;
91
92 while ( nextD > (static_cast<double>(current_bin) * probPerBin) ) {
93 current_bin++;
94 }
95
96 return static_cast<double>(current_bin - 1);
97 }
98
99 /**
100 Default constructor. FOR SERIALIZATION ONLY.
101 */
107
108 /**
109 Serialization function for checkpoint
110 */
112 {
113 SST_SER(baseDistrib);
114 SST_SER(const_cast<bool&>(deleteDistrib));
115 SST_SER(const_cast<uint32_t&>(probCount));
116 SST_SER(probPerBin);
117 }
118
119 /**
120 Serialization macro
121 */
122 ImplementSerializable(SST::RNG::UniformDistribution)
123
124protected:
125 /**
126 Sets the base random number generator for the distribution.
127 */
128 Random* baseDistrib;
129
130 /**
131 Controls whether the base distribution should be deleted when this class is destructed.
132 */
133 const bool deleteDistrib;
134
135 /**
136 Count of discrete probabilities
137 */
138 const uint32_t probCount;
139
140 /**
141 Range 0..1 split into discrete bins
142 */
144};
145
146} // namespace SST::RNG
147
148using SSTUniformDistribution = SST::RNG::UniformDistribution;
149
150#endif // SST_CORE_RNG_UNIFORM_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Implements a Mersenne-based RNG for use in the SST core or components.
Definition mersenne.h:34
RandomDistribution()
Creates the base (abstract) class of a distribution.
Definition distrib.h:41
Implements the base class for random number generators for the SST core.
Definition rng.h:29
Creates a Uniform distribution for use within SST.
Definition uniform.h:31
UniformDistribution(const uint32_t probsCount, Random *baseDist)
Creates a Uniform distribution with a specific number of bins and user supplied random number generat...
Definition uniform.h:58
double probPerBin
Range 0..1 split into discrete bins.
Definition uniform.h:143
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition uniform.h:111
~UniformDistribution()
Destroys the distribution and will delete locally allocated RNGs.
Definition uniform.h:75
ImplementSerializable(SST::RNG::UniformDistribution) protected const bool deleteDistrib
Serialization macro.
Definition uniform.h:122
UniformDistribution(const uint32_t probsCount)
Creates an uniform distribution with a specific number of bins.
Definition uniform.h:38
UniformDistribution()
Default constructor.
Definition uniform.h:102
double getNextDouble() override
Gets the next (random) double value in the distribution.
Definition uniform.h:87
const uint32_t probCount
Count of discrete probabilities.
Definition uniform.h:138