SST 12.1.0
Structural Simulation Toolkit
marsaglia.h
1// Copyright 2009-2022 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-2022, 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 {
28namespace RNG {
29/**
30 \class MarsagliaRNG marsaglia.h "sst/core/rng/marsaglia.h"
31
32 Implements a random number generator using the Marsaglia method. This method is
33 computationally cheap and provides a reasonable distribution of random numbers. If
34 you need additional strength in the random numbers you may want to consider the
35 Mersenne RNG.
36
37 For more information see the Multiply-with-carry Random Number Generator article
38 at Wikipedia (http://en.wikipedia.org/wiki/Multiply-with-carry).
39*/
41{
42
43public:
44 /**
45 Creates a new Marsaglia RNG using the initial seeds.
46 @param[in] initial_z One of the random seed pairs
47 @param[in] initial_w One of the random seed pairs.
48 */
49 MarsagliaRNG(unsigned int initial_z, unsigned int initial_w);
50
51 /**
52 Creates a new Marsaglia RNG using random initial seeds (which are
53 read from the system clock). Note that these will create variation
54 between runs and between platforms.
55 */
57
58 /**
59 Restart the random number generator with new seeds
60 @param[in] new_z A new Z-seed
61 @param[in] new_w A new W-seed
62 */
63 void restart(unsigned int new_z, unsigned int new_w);
64
65 /**
66 Generates the next random number as a double in the range 0 to 1.
67 */
68 double nextUniform() override;
69
70 /**
71 Generates the next random number as an unsigned 32-bit integer.
72 */
73 uint32_t generateNextUInt32() override;
74
75 /**
76 Generates the next random number as an unsigned 64-bit integer.
77 */
78 uint64_t generateNextUInt64() override;
79
80 /**
81 Generates the next number as a signed 64-bit integer.
82 */
83 int64_t generateNextInt64() override;
84
85 /**
86 Generates the next number as a signed 32-bit integer.
87 */
88 int32_t generateNextInt32() override;
89
90 /**
91 Seed the XOR RNG
92 */
93 void seed(uint64_t newSeed);
94
95private:
96 /**
97 Generates the next random number
98 */
99 unsigned int generateNext();
100
101 /**
102 The Z seed of the Marsaglia generator
103 */
104 unsigned int m_z;
105
106 /**
107 The W seed of the Marsaglia generator
108 */
109 unsigned int m_w;
110};
111
112} // namespace RNG
113} // namespace SST
114
115#endif // SST_CORE_RNG_MARSAGLIA_H
Implements a random number generator using the Marsaglia method.
Definition: marsaglia.h:41
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition: marsaglia.cc:142
int32_t generateNextInt32() override
Generates the next number as a signed 32-bit integer.
Definition: marsaglia.cc:126
void restart(unsigned int new_z, unsigned int new_w)
Restart the random number generator with new seeds.
Definition: marsaglia.cc:52
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition: marsaglia.cc:149
MarsagliaRNG()
Creates a new Marsaglia RNG using random initial seeds (which are read from the system clock).
Definition: marsaglia.cc:39
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition: marsaglia.cc:88
int64_t generateNextInt64() override
Generates the next number as a signed 64-bit integer.
Definition: marsaglia.cc:104
double nextUniform() override
Generates the next random number as a double in the range 0 to 1.
Definition: marsaglia.cc:76
Implements the base class for random number generators for the SST core.
Definition: rng.h:28