SST  11.1.0
StructuralSimulationToolkit
xorshift.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_XORSHIFT_H
13 #define SST_CORE_RNG_XORSHIFT_H
14 
15 #include "rng.h"
16 
17 #include <stdint.h>
18 #include <sys/time.h>
19 
20 #include "sstrng.h"
21 
22 #define XORSHIFT_UINT32_MAX 4294967295U
23 #define XORSHIFT_UINT64_MAX 18446744073709551615ULL
24 #define XORSHIFT_INT32_MAX 2147483647L
25 #define XORSHIFT_INT64_MAX 9223372036854775807LL
26 
27 namespace SST {
28 namespace RNG {
29 /**
30  \class XORShiftRNG xorshift.h "sst/core/rng/xorshift.h"
31 
32  Implements a lightweight RNG based on XOR-shift operations. We utilize the
33  XORSHIFT algorithm from: http://en.wikipedia.org/wiki/Xorshift. This is a very
34  lightweight and inexpensive RNG.
35 
36 */
38 {
39 
40 public:
41  /**
42  Create a new Mersenne RNG with a specified seed
43  @param[in] seed The seed for this RNG
44  */
45  XORShiftRNG(unsigned int seed);
46 
47  /**
48  Creates a new Mersenne using a random seed which is obtained from the system
49  clock. Note this will give different results on different platforms and between
50  runs.
51  */
52  XORShiftRNG();
53 
54  /**
55  Generates the next random number as a double value between 0 and 1.
56  */
57  double nextUniform() override;
58 
59  /**
60  Generates the next random number as an unsigned 32-bit integer
61  */
62  uint32_t generateNextUInt32() override;
63 
64  /**
65  Generates the next random number as an unsigned 64-bit integer
66  */
67  uint64_t generateNextUInt64() override;
68 
69  /**
70  Generates the next random number as a signed 64-bit integer
71  */
72  int64_t generateNextInt64() override;
73 
74  /**
75  Generates the next random number as a signed 32-bit integer
76  */
77  int32_t generateNextInt32() override;
78 
79  /**
80  Seed the XOR RNG
81  */
82  void seed(uint64_t newSeed);
83 
84  /**
85  Destructor for Mersenne
86  */
87  ~XORShiftRNG();
88 
89 protected:
90  uint32_t x;
91  uint32_t y;
92  uint32_t z;
93  uint32_t w;
94 };
95 
96 } // namespace RNG
97 } // namespace SST
98 
99 #endif // SST_CORE_RNG_XORSHIFT_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:27
int32_t generateNextInt32() override
Generates the next random number as a signed 32-bit integer.
Definition: xorshift.cc:116
XORShiftRNG()
Creates a new Mersenne using a random seed which is obtained from the system clock.
Definition: xorshift.cc:29
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition: xorshift.cc:132
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition: xorshift.cc:78
~XORShiftRNG()
Destructor for Mersenne.
Definition: xorshift.cc:140
Implements a lightweight RNG based on XOR-shift operations.
Definition: xorshift.h:37
int64_t generateNextInt64() override
Generates the next random number as a signed 64-bit integer.
Definition: xorshift.cc:94
double nextUniform() override
Generates the next random number as a double value between 0 and 1.
Definition: xorshift.cc:56
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition: xorshift.cc:68