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