Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
RandHelper.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2005-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20//
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <cassert>
26#include <cstring>
27#include <vector>
28#include <map>
29#include <random>
30#include <sstream>
31#include <iostream>
32#include <algorithm>
33
34// TODO make this configurable
35#define SAVE_ONLY_COUNT 1000000
36
37// ===========================================================================
38// helper function
39// ===========================================================================
40
41#ifdef __clang__
42__attribute__((no_sanitize("unsigned-integer-overflow")))
43#endif
44inline uint64_t splitmix64(const uint64_t seed) {
45 uint64_t z = (seed + 0x9e3779b97f4a7c15);
46 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
47 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
48 return z ^ (z >> 31);
49}
50
51
52// ===========================================================================
53// class definitions
54// ===========================================================================
60public:
61 inline void seed(const uint64_t seed) {
62 this->state[0] = splitmix64(splitmix64(seed));
63 this->state[1] = splitmix64(this->state[0]);
64 this->state[2] = splitmix64(this->state[1]);
65 this->state[3] = splitmix64(this->state[2]);
66 }
67
68 inline uint64_t operator()() {
69 const uint64_t result = rotl64(this->state[0] + this->state[3], 23) + this->state[0];
70 const uint64_t t = this->state[1] << 17;
71 this->state[2] ^= this->state[0];
72 this->state[3] ^= this->state[1];
73 this->state[1] ^= this->state[2];
74 this->state[0] ^= this->state[3];
75 this->state[2] ^= t;
76 this->state[3] = rotl64(this->state[3], 45);
77 return result;
78 }
79
80 void discard(unsigned long long skip) {
81 while (skip-- > 0) {
82 (*this)();
83 }
84 }
85
86 friend std::ostream& operator<<(std::ostream& os, const XoShiRo256PlusPlus& r) {
87 os << r.state[0] << r.state[1] << r.state[2] << r.state[3];
88 return os;
89 }
90
91 friend std::istream& operator>>(std::istream& is, XoShiRo256PlusPlus& r) {
92 is >> r.state[0] >> r.state[1] >> r.state[2] >> r.state[3];
93 return is;
94 }
95
96
97private:
98 static inline uint64_t rotl64(const uint64_t x, const int k) {
99 return (x << k) | (x >> (64 - k));
100 }
101
102 uint64_t state[4];
103
104};
105
106
107//class SumoRNG : public XoShiRo256PlusPlus {
108class SumoRNG : public std::mt19937 {
109public:
110 SumoRNG(const std::string& _id) : id(_id) {}
111
112 void setSeed(int _seed) {
113 origSeed = _seed;
114 seed(_seed);
115 }
116
117 unsigned long long int count = 0;
118 int origSeed = default_seed;
119 std::string id;
120};
121
122
128
129public:
131 static void initRand(SumoRNG* which = nullptr, const bool random = false, const int seed = 23423);
132
133 static int getSeed(SumoRNG* which = nullptr);
134
136 static double rand(SumoRNG* rng = nullptr);
137
139 static inline double rand(double maxV, SumoRNG* rng = nullptr) {
140 return maxV * rand(rng);
141 }
142
144 static inline double rand(double minV, double maxV, SumoRNG* rng = nullptr) {
145 return minV + (maxV - minV) * rand(rng);
146 }
147
149 static inline int rand(int maxV, SumoRNG* rng = nullptr) {
150 if (rng == nullptr) {
152 }
153 unsigned int usedBits = maxV - 1;
154 usedBits |= usedBits >> 1;
155 usedBits |= usedBits >> 2;
156 usedBits |= usedBits >> 4;
157 usedBits |= usedBits >> 8;
158 usedBits |= usedBits >> 16;
159
160 // Draw numbers until one is found in [0, maxV-1]
161 int result;
162 do {
163 result = (*rng)() & usedBits;
164 rng->count++;
165 } while (result >= maxV);
166 return result;
167 }
168
170 static inline int rand(int minV, int maxV, SumoRNG* rng = nullptr) {
171 return minV + rand(maxV - minV, rng);
172 }
173
175 static inline long long int rand(long long int maxV, SumoRNG* rng = nullptr) {
176 if (maxV <= std::numeric_limits<int>::max()) {
177 return rand((int)maxV, rng);
178 }
179 if (rng == nullptr) {
181 }
182 unsigned long long int usedBits = maxV - 1;
183 usedBits |= usedBits >> 1;
184 usedBits |= usedBits >> 2;
185 usedBits |= usedBits >> 4;
186 usedBits |= usedBits >> 8;
187 usedBits |= usedBits >> 16;
188 usedBits |= usedBits >> 32;
189
190 // Draw numbers until one is found in [0, maxV-1]
191 long long int result;
192 do {
193 result = (((unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits; // toss unused bits to shorten search
194 rng->count += 2;
195 } while (result >= maxV);
196 return result;
197 }
198
200 static inline long long int rand(long long int minV, long long int maxV, SumoRNG* rng = nullptr) {
201 return minV + rand(maxV - minV, rng);
202 }
203
205 static double randNorm(double mean, double variance, SumoRNG* rng = nullptr);
206
208 static double randExp(double rate, SumoRNG* rng = nullptr);
209
211 template<class T>
212 static inline const T&
213 getRandomFrom(const std::vector<T>& v, SumoRNG* rng = nullptr) {
214 assert(v.size() > 0);
215 return v[rand((int)v.size(), rng)];
216 }
217
219 static std::string saveState(SumoRNG* rng = nullptr) {
220 if (rng == nullptr) {
222 }
223 std::ostringstream oss;
224 oss << rng->count;
225 if (rng->count >= SAVE_ONLY_COUNT) {
226 oss << " " << (*rng);
227 }
228 return oss.str();
229 }
230
232 static void loadState(const std::string& state, SumoRNG* rng = nullptr) {
233 if (rng == nullptr) {
235 }
236 std::istringstream iss(state);
237 iss >> rng->count;
238 if (rng->count < SAVE_ONLY_COUNT) {
239 rng->discard(rng->count);
240 } else {
241 iss >> (*rng);
242 }
243 }
244
245 template<class T>
246 static void shuffle(std::vector<T>& v, SumoRNG* rng = nullptr) {
247 for (int i = (int)(v.size() - 1); i > 0; --i) {
248 std::swap(*(v.begin() + i), *(v.begin() + rand(i, rng)));
249 }
250 }
251
252 static long long int count() {
254 }
255
257 static double randHash(long long int x) {
258 const uint64_t h = splitmix64(x) >> 11;
259 return (double)h / (double(1ULL << 53));
260 }
261
263#ifdef __clang__
264 __attribute__((no_sanitize("integer"))) // left-shift and unsigned-integer-overflow
265#endif
266 static uint32_t murmur3_32(const std::string& key2, int seed) {
267 const uint8_t* key = reinterpret_cast<const uint8_t*>(key2.data());
268 const size_t len = key2.size();
269 uint32_t h = seed;
270 uint32_t k;
271 /* Read in groups of 4. */
272 for (size_t i = len >> 2; i; i--) {
273 memcpy(&k, key, sizeof(uint32_t));
274 key += sizeof(uint32_t);
275 h ^= murmur_32_scramble(k);
276 h = (h << 13) | (h >> 19);
277 h = h * 5 + 0xe6546b64;
278 }
279 /* Read the rest. */
280 k = 0;
281 for (size_t i = len & 3; i; i--) {
282 k <<= 8;
283 k |= key[i - 1];
284 }
285 h ^= murmur_32_scramble(k);
286 /* Finalize. */
287 h ^= len;
288 h ^= h >> 16;
289 h *= 0x85ebca6b;
290 h ^= h >> 13;
291 h *= 0xc2b2ae35;
292 h ^= h >> 16;
293 return h;
294 }
295
296
297protected:
300
302#ifdef __clang__
303 __attribute__((no_sanitize("integer"))) // left-shift and unsigned-integer-overflow
304#endif
305 static inline uint32_t murmur_32_scramble(uint32_t k) {
306 k *= 0xcc9e2d51;
307 k = (k << 15) | (k >> 17);
308 k *= 0x1b873593;
309 return k;
310 }
311
312};
uint64_t splitmix64(const uint64_t seed)
Definition RandHelper.h:44
#define SAVE_ONLY_COUNT
Definition RandHelper.h:35
Utility functions for using a global, resetable random number generator.
Definition RandHelper.h:127
static SumoRNG myRandomNumberGenerator
the default random number generator to use
Definition RandHelper.h:299
static void loadState(const std::string &state, SumoRNG *rng=nullptr)
load rng state from string
Definition RandHelper.h:232
static double randExp(double rate, SumoRNG *rng=nullptr)
Access to a random number from an exponential distribution.
static long long int rand(long long int minV, long long int maxV, SumoRNG *rng=nullptr)
Returns a random 64 bit integer in [minV, maxV-1].
Definition RandHelper.h:200
static double randHash(long long int x)
return a value scrambled value from [0, 1]
Definition RandHelper.h:257
static double rand(double minV, double maxV, SumoRNG *rng=nullptr)
Returns a random real number in [minV, maxV)
Definition RandHelper.h:144
static void initRand(SumoRNG *which=nullptr, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
static uint32_t murmur3_32(const std::string &key2, int seed)
string hashing adapted from https://en.wikipedia.org/wiki/MurmurHash
Definition RandHelper.h:266
static int rand(int minV, int maxV, SumoRNG *rng=nullptr)
Returns a random integer in [minV, maxV-1].
Definition RandHelper.h:170
static int getSeed(SumoRNG *which=nullptr)
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
static int rand(int maxV, SumoRNG *rng=nullptr)
Returns a random integer in [0, maxV-1].
Definition RandHelper.h:149
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
static long long int count()
Definition RandHelper.h:252
static std::string saveState(SumoRNG *rng=nullptr)
save rng state to string
Definition RandHelper.h:219
static uint32_t murmur_32_scramble(uint32_t k)
helper function for murmur_32_scramble from https://en.wikipedia.org/wiki/MurmurHash
Definition RandHelper.h:305
static double rand(double maxV, SumoRNG *rng=nullptr)
Returns a random real number in [0, maxV)
Definition RandHelper.h:139
static const T & getRandomFrom(const std::vector< T > &v, SumoRNG *rng=nullptr)
Returns a random element from the given vector.
Definition RandHelper.h:213
static long long int rand(long long int maxV, SumoRNG *rng=nullptr)
Returns a random 64 bit integer in [0, maxV-1].
Definition RandHelper.h:175
static void shuffle(std::vector< T > &v, SumoRNG *rng=nullptr)
Definition RandHelper.h:246
std::string id
Definition RandHelper.h:119
int origSeed
Definition RandHelper.h:118
SumoRNG(const std::string &_id)
Definition RandHelper.h:110
unsigned long long int count
Definition RandHelper.h:117
void setSeed(int _seed)
Definition RandHelper.h:112
A random number generator as proposed here https://prng.di.unimi.it/xoshiro256plusplus....
Definition RandHelper.h:59
uint64_t operator()()
Definition RandHelper.h:68
uint64_t state[4]
Definition RandHelper.h:102
friend std::ostream & operator<<(std::ostream &os, const XoShiRo256PlusPlus &r)
Definition RandHelper.h:86
friend std::istream & operator>>(std::istream &is, XoShiRo256PlusPlus &r)
Definition RandHelper.h:91
void discard(unsigned long long skip)
Definition RandHelper.h:80
static uint64_t rotl64(const uint64_t x, const int k)
Definition RandHelper.h:98
void seed(const uint64_t seed)
Definition RandHelper.h:61
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:25297