SST  11.0.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 #include "sst/core/sst_types.h"
15 
16 #include <stdint.h>
17 #include <sys/time.h>
18 
19 #include "sstrng.h"
20 
21 #define MARSAGLIA_UINT32_MAX 4294967295U
22 #define MARSAGLIA_UINT64_MAX 18446744073709551615ULL
23 #define MARSAGLIA_INT32_MAX 2147483647L
24 #define MARSAGLIA_INT64_MAX 9223372036854775807LL
25 
26 namespace SST {
27 namespace 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 */
39 class MarsagliaRNG : public SSTRandom {
40 
41  public:
42  /**
43  Creates a new Marsaglia RNG using the initial seeds.
44  @param[in] initial_z One of the random seed pairs
45  @param[in] initial_w One of the random seed pairs.
46  */
47  MarsagliaRNG(unsigned int initial_z,
48  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  */
55  MarsagliaRNG();
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  private:
95  /**
96  Generates the next random number
97  */
98  unsigned int generateNext();
99 
100  /**
101  The Z seed of the Marsaglia generator
102  */
103  unsigned int m_z;
104 
105  /**
106  The W seed of the Marsaglia generator
107  */
108  unsigned int m_w;
109 
110 };
111 
112 } //namespace RNG
113 } //namespace SST
114 
115 #endif //SST_CORE_RNG_MARSAGLIA_H
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition: marsaglia.cc:118
int64_t generateNextInt64() override
Generates the next number as a signed 64-bit integer.
Definition: marsaglia.cc:84
MarsagliaRNG()
Creates a new Marsaglia RNG using random initial seeds (which are read from the system clock)...
Definition: marsaglia.cc:34
void restart(unsigned int new_z, unsigned int new_w)
Restart the random number generator with new seeds.
Definition: marsaglia.cc:45
int32_t generateNextInt32() override
Generates the next number as a signed 32-bit integer.
Definition: marsaglia.cc:104
Implements a random number generator using the Marsaglia method.
Definition: marsaglia.h:39
Implements the base class for random number generators for the SST core.
Definition: sstrng.h:27
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition: marsaglia.cc:70
double nextUniform() override
Generates the next random number as a double in the range 0 to 1.
Definition: marsaglia.cc:64
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition: marsaglia.cc:123