SST 15.0
Structural Simulation Toolkit
marsaglia.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_MARSAGLIA_H
13#define SST_CORE_RNG_MARSAGLIA_H
14
15#include "sst/core/sst_types.h"
16
17#include "rng.h"
18
19#include <stdint.h>
20#include <sys/time.h>
21
22#define MARSAGLIA_UINT32_MAX 4294967295U
23#define MARSAGLIA_UINT64_MAX 18446744073709551615ULL
24#define MARSAGLIA_INT32_MAX 2147483647L
25#define MARSAGLIA_INT64_MAX 9223372036854775807LL
26
27namespace SST::RNG {
28/**
29 \class MarsagliaRNG marsaglia.h "sst/core/rng/marsaglia.h"
30
31 Implements a random number generator using the Marsaglia method. This method is
32 computationally cheap and provides a reasonable distribution of random numbers. If
33 you need additional strength in the random numbers you may want to consider the
34 Mersenne RNG.
35
36 For more information see the Multiply-with-carry Random Number Generator article
37 at Wikipedia (http://en.wikipedia.org/wiki/Multiply-with-carry).
38*/
39class MarsagliaRNG : public Random
40{
41
42public:
43 /**
44 Creates a new Marsaglia RNG using the initial seeds.
45 @param[in] initial_z One of the random seed pairs
46 @param[in] initial_w One of the random seed pairs.
47 */
48 MarsagliaRNG(unsigned int initial_z, unsigned int initial_w);
49
50 /**
51 Creates a new Marsaglia RNG using random initial seeds (which are
52 read from the system clock). Note that these will create variation
53 between runs and between platforms.
54 */
56
57 /**
58 Restart the random number generator with new seeds
59 @param[in] new_z A new Z-seed
60 @param[in] new_w A new W-seed
61 */
62 void restart(unsigned int new_z, unsigned int new_w);
63
64 /**
65 Generates the next random number as a double in the range 0 to 1.
66 */
67 double nextUniform() override;
68
69 /**
70 Generates the next random number as an unsigned 32-bit integer.
71 */
72 uint32_t generateNextUInt32() override;
73
74 /**
75 Generates the next random number as an unsigned 64-bit integer.
76 */
77 uint64_t generateNextUInt64() override;
78
79 /**
80 Generates the next number as a signed 64-bit integer.
81 */
82 int64_t generateNextInt64() override;
83
84 /**
85 Generates the next number as a signed 32-bit integer.
86 */
87 int32_t generateNextInt32() override;
88
89 /**
90 Seed the XOR RNG
91 */
92 void seed(uint64_t newSeed);
93
94 /**
95 * Serialization function for checkpoint
96 */
98
99 /**
100 Serialization macro
101 */
102 ImplementSerializable(SST::RNG::MarsagliaRNG)
103
104private:
105 /**
106 Generates the next random number
107 */
108 unsigned int generateNext();
109
110 /**
111 The Z seed of the Marsaglia generator
112 */
113 unsigned int m_z;
114
115 /**
116 The W seed of the Marsaglia generator
117 */
118 unsigned int m_w;
119};
120
121} // namespace SST::RNG
122
123#endif // SST_CORE_RNG_MARSAGLIA_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 random number generator using the Marsaglia method.
Definition marsaglia.h:40
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition marsaglia.cc:144
unsigned int m_w
The W seed of the Marsaglia generator.
Definition marsaglia.h:118
int32_t generateNextInt32() override
Generates the next number as a signed 32-bit integer.
Definition marsaglia.cc:128
void restart(unsigned int new_z, unsigned int new_w)
Restart the random number generator with new seeds.
Definition marsaglia.cc:54
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition marsaglia.cc:167
ImplementSerializable(SST::RNG::MarsagliaRNG) private unsigned int m_z
Serialization macro.
Definition marsaglia.h:102
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition marsaglia.cc:151
MarsagliaRNG(unsigned int initial_z, unsigned int initial_w)
Creates a new Marsaglia RNG using the initial seeds.
Definition marsaglia.cc:29
MarsagliaRNG()
Creates a new Marsaglia RNG using random initial seeds (which are read from the system clock).
Definition marsaglia.cc:40
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition marsaglia.cc:90
int64_t generateNextInt64() override
Generates the next number as a signed 64-bit integer.
Definition marsaglia.cc:106
double nextUniform() override
Generates the next random number as a double in the range 0 to 1.
Definition marsaglia.cc:78
Implements the base class for random number generators for the SST core.
Definition rng.h:29