SST 15.0
Structural Simulation Toolkit
mersenne.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_MERSENNE_H
13#define SST_CORE_RNG_MERSENNE_H
14
15#include "rng.h"
16
17#include <stdint.h>
18#include <sys/time.h>
19
20#define MERSENNE_UINT32_MAX 4294967295U
21#define MERSENNE_UINT64_MAX 18446744073709551615ULL
22#define MERSENNE_INT32_MAX 2147483647L
23#define MERSENNE_INT64_MAX 9223372036854775807LL
24
25namespace SST::RNG {
26/**
27 \class MersenneRNG mersenne.h "sst/core/rng/mersenne.h"
28
29 Implements a Mersenne-based RNG for use in the SST core or components. The Mersenne
30 RNG provides a better "randomness" to the distribution of outputs but is computationally
31 more expensive than the Marsaglia RNG.
32*/
33class MersenneRNG : public Random
34{
35
36public:
37 /**
38 Create a new Mersenne RNG with a specified seed
39 @param[in] seed The seed for this RNG
40 */
41 explicit MersenneRNG(unsigned int seed);
42
43 /**
44 Creates a new Mersenne using a random seed which is obtained from the system
45 clock. Note this will give different results on different platforms and between
46 runs.
47 */
49
50 /**
51 Generates the next random number as a double value between 0 and 1.
52 */
53 double nextUniform() override;
54
55 /**
56 Generates the next random number as an unsigned 32-bit integer
57 */
58 uint32_t generateNextUInt32() override;
59
60 /**
61 Generates the next random number as an unsigned 64-bit integer
62 */
63 uint64_t generateNextUInt64() override;
64
65 /**
66 Generates the next random number as a signed 64-bit integer
67 */
68 int64_t generateNextInt64() override;
69
70 /**
71 Generates the next random number as a signed 32-bit integer
72 */
73 int32_t generateNextInt32() override;
74
75 /**
76 Seed the XOR RNG
77 */
78 void seed(uint64_t newSeed);
79
80 /**
81 Destructor for Mersenne
82 */
84
85 /**
86 * Serialization function for checkpoint
87 */
89
90 /**
91 Serialization macro
92 */
93 ImplementSerializable(SST::RNG::MersenneRNG)
94
95private:
96 /**
97 Generates the next batch of random numbers
98 */
99 void generateNextBatch();
100
101 /**
102 Stores the next set of random numbers
103 */
104 uint32_t* numbers;
105
106 /**
107 Tells us what index of the random number list the next returnable number should come from
108 */
109 int index;
110};
111
112} // namespace SST::RNG
113
114#endif // SST_CORE_RNG_MERSENNE_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
double nextUniform() override
Generates the next random number as a double value between 0 and 1.
Definition mersenne.cc:76
MersenneRNG(unsigned int seed)
Create a new Mersenne RNG with a specified seed.
Definition mersenne.cc:49
ImplementSerializable(SST::RNG::MersenneRNG) private uint32_t * numbers
Serialization macro.
Definition mersenne.h:93
int32_t generateNextInt32() override
Generates the next random number as a signed 32-bit integer.
Definition mersenne.cc:138
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition mersenne.cc:167
MersenneRNG()
Creates a new Mersenne using a random seed which is obtained from the system clock.
Definition mersenne.cc:29
int index
Tells us what index of the random number list the next returnable number should come from.
Definition mersenne.h:109
int64_t generateNextInt64() override
Generates the next random number as a signed 64-bit integer.
Definition mersenne.cc:116
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition mersenne.cc:88
~MersenneRNG()
Destructor for Mersenne.
Definition mersenne.cc:161
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition mersenne.cc:105
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition mersenne.cc:149
Implements the base class for random number generators for the SST core.
Definition rng.h:29