SST 15.0
Structural Simulation Toolkit
xorshift.h
1// Copyright 2009-2025 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-2025, 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#define XORSHIFT_UINT32_MAX 4294967295U
21#define XORSHIFT_UINT64_MAX 18446744073709551615ULL
22#define XORSHIFT_INT32_MAX 2147483647L
23#define XORSHIFT_INT64_MAX 9223372036854775807LL
24
25namespace SST::RNG {
26/**
27 \class XORShiftRNG xorshift.h "sst/core/rng/xorshift.h"
28
29 Implements a lightweight RNG based on XOR-shift operations. We utilize the
30 XORSHIFT algorithm from: http://en.wikipedia.org/wiki/Xorshift. This is a very
31 lightweight and inexpensive RNG.
32
33*/
35{
36
37public:
38 /**
39 Create a new Xorshift RNG with a specified seed
40 @param[in] seed The seed for this RNG
41 */
42 explicit XORShiftRNG(unsigned int seed);
43
44 /**
45 Creates a new Xorshift using a random seed which is obtained from the system
46 clock. Note this will give different results on different platforms and between
47 runs.
48 */
50
51 /**
52 Generates the next random number as a double value between 0 and 1.
53 */
54 double nextUniform() override;
55
56 /**
57 Generates the next random number as an unsigned 32-bit integer
58 */
59 uint32_t generateNextUInt32() override;
60
61 /**
62 Generates the next random number as an unsigned 64-bit integer
63 */
64 uint64_t generateNextUInt64() override;
65
66 /**
67 Generates the next random number as a signed 64-bit integer
68 */
69 int64_t generateNextInt64() override;
70
71 /**
72 Generates the next random number as a signed 32-bit integer
73 */
74 int32_t generateNextInt32() override;
75
76 /**
77 Seed the XOR RNG
78 */
79 void seed(uint64_t newSeed);
80
81 /**
82 Destructor for Xorshift
83 */
85
86 /**
87 * Serialization function for checkpoint
88 */
90
91 /**
92 Serialization macro
93 */
94 ImplementSerializable(SST::RNG::XORShiftRNG)
95
96protected:
97 uint32_t x;
98 uint32_t y;
99 uint32_t z;
100 uint32_t w;
101};
102
103} // namespace SST::RNG
104
105#endif // SST_CORE_RNG_XORSHIFT_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Implements the base class for random number generators for the SST core.
Definition rng.h:29
Implements a lightweight RNG based on XOR-shift operations.
Definition xorshift.h:35
XORShiftRNG()
Creates a new Xorshift using a random seed which is obtained from the system clock.
Definition xorshift.cc:29
uint32_t generateNextUInt32() override
Generates the next random number as an unsigned 32-bit integer.
Definition xorshift.cc:70
double nextUniform() override
Generates the next random number as a double value between 0 and 1.
Definition xorshift.cc:58
uint64_t generateNextUInt64() override
Generates the next random number as an unsigned 64-bit integer.
Definition xorshift.cc:80
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition xorshift.cc:145
int32_t generateNextInt32() override
Generates the next random number as a signed 32-bit integer.
Definition xorshift.cc:118
~XORShiftRNG()
Destructor for Xorshift.
Definition xorshift.cc:142
XORShiftRNG(unsigned int seed)
Create a new Xorshift RNG with a specified seed.
Definition xorshift.cc:44
ImplementSerializable(SST::RNG::XORShiftRNG) protected uint32_t y
Serialization macro.
Definition xorshift.h:94
void seed(uint64_t newSeed)
Seed the XOR RNG.
Definition xorshift.cc:134
int64_t generateNextInt64() override
Generates the next random number as a signed 64-bit integer.
Definition xorshift.cc:96