35#define SAVE_ONLY_COUNT 1000000
42__attribute__((no_sanitize(
"unsigned-integer-overflow")))
45 uint64_t z = (seed + 0x9e3779b97f4a7c15);
46 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
47 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
76 const uint64_t t = this->
state[1] << 17;
104 static inline uint64_t
rotl64(
const uint64_t x,
const int k) {
105 return (x << k) | (x >> (64 - k));
140 static void initRand(
SumoRNG* which =
nullptr,
const bool random =
false,
const int seed = 23423);
152 return maxV *
rand(rng);
156 static inline double rand(
double minV,
double maxV,
SumoRNG* rng =
nullptr) {
157 return minV + (maxV - minV) *
rand(rng);
162 if (rng ==
nullptr) {
165 unsigned int usedBits = maxV - 1;
166 usedBits |= usedBits >> 1;
167 usedBits |= usedBits >> 2;
168 usedBits |= usedBits >> 4;
169 usedBits |= usedBits >> 8;
170 usedBits |= usedBits >> 16;
175 result = (*rng)() & usedBits;
177 }
while (result >= maxV);
182 static inline int rand(
int minV,
int maxV,
SumoRNG* rng =
nullptr) {
183 return minV +
rand(maxV - minV, rng);
187 static inline long long int rand(
long long int maxV,
SumoRNG* rng =
nullptr) {
188 if (maxV <= std::numeric_limits<int>::max()) {
189 return rand((
int)maxV, rng);
191 if (rng ==
nullptr) {
194 unsigned long long int usedBits = maxV - 1;
195 usedBits |= usedBits >> 1;
196 usedBits |= usedBits >> 2;
197 usedBits |= usedBits >> 4;
198 usedBits |= usedBits >> 8;
199 usedBits |= usedBits >> 16;
200 usedBits |= usedBits >> 32;
203 long long int result;
205 result = (((
unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits;
207 }
while (result >= maxV);
212 static inline long long int rand(
long long int minV,
long long int maxV,
SumoRNG* rng =
nullptr) {
213 return minV +
rand(maxV - minV, rng);
217 static double randNorm(
double mean,
double variance,
SumoRNG* rng =
nullptr);
224 static inline const T&
226 assert(v.size() > 0);
227 return v[
rand((
int)v.size(), rng)];
232 if (rng ==
nullptr) {
235 std::ostringstream oss;
238 oss <<
" " << (*rng);
245 if (rng ==
nullptr) {
248 std::istringstream iss(state);
251 rng->discard(rng->count);
259 for (
int i = (
int)(v.size() - 1); i > 0; --i) {
271 return (
double)h / (double(1ULL << 53));
276 __attribute__((no_sanitize(
"integer")))
278 static uint32_t
murmur3_32(
const std::string& key2,
int seed) {
279 const uint8_t* key =
reinterpret_cast<const uint8_t*
>(key2.data());
280 const size_t len = key2.size();
284 for (
size_t i = len >> 2; i; i--) {
285 memcpy(&k, key,
sizeof(uint32_t));
286 key +=
sizeof(uint32_t);
288 h = (h << 13) | (h >> 19);
289 h = h * 5 + 0xe6546b64;
293 for (
size_t i = len & 3; i; i--) {
315 __attribute__((no_sanitize(
"integer")))
319 k = (k << 15) | (k >> 17);
uint64_t splitmix64(const uint64_t seed)
A storage for options typed value containers)
Utility functions for using a global, resetable random number generator.
static SumoRNG myRandomNumberGenerator
the default random number generator to use
static void loadState(const std::string &state, SumoRNG *rng=nullptr)
load rng state from string
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].
static double randHash(long long int x)
return a value scrambled value from [0, 1]
static double rand(double minV, double maxV, SumoRNG *rng=nullptr)
Returns a random real number in [minV, maxV)
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
static int rand(int minV, int maxV, SumoRNG *rng=nullptr)
Returns a random integer in [minV, maxV-1].
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].
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
static long long int count()
static std::string saveState(SumoRNG *rng=nullptr)
save rng state to string
static uint32_t murmur_32_scramble(uint32_t k)
helper function for murmur_32_scramble from https://en.wikipedia.org/wiki/MurmurHash
static double rand(double maxV, SumoRNG *rng=nullptr)
Returns a random real number in [0, maxV)
static void insertRandOptions(OptionsCont &oc)
Initialises the given options container with random number options.
static const T & getRandomFrom(const std::vector< T > &v, SumoRNG *rng=nullptr)
Returns a random element from the given vector.
static long long int rand(long long int maxV, SumoRNG *rng=nullptr)
Returns a random 64 bit integer in [0, maxV-1].
static void shuffle(std::vector< T > &v, SumoRNG *rng=nullptr)
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
SumoRNG(const std::string &_id)
unsigned long long int count
A random number generator as proposed here https://prng.di.unimi.it/xoshiro256plusplus....
friend std::ostream & operator<<(std::ostream &os, const XoShiRo256PlusPlus &r)
friend std::istream & operator>>(std::istream &is, XoShiRo256PlusPlus &r)
void discard(unsigned long long skip)
static uint64_t rotl64(const uint64_t x, const int k)
void seed(const uint64_t seed)
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) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects