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