34#define SAVE_ONLY_COUNT 1000000
60 const uint64_t t = this->
state[1] << 17;
88 static inline uint64_t
rotl64(
const uint64_t x,
const int k) {
89 return (x << k) | (x >> (64 - k));
93 uint64_t z = (
seed + 0x9e3779b97f4a7c15);
94 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
95 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
125 static void initRand(
SumoRNG* which =
nullptr,
const bool random =
false,
const int seed = 23423);
135 return maxV *
rand(rng);
139 static inline double rand(
double minV,
double maxV,
SumoRNG* rng =
nullptr) {
140 return minV + (maxV - minV) *
rand(rng);
145 if (rng ==
nullptr) {
148 unsigned int usedBits = maxV - 1;
149 usedBits |= usedBits >> 1;
150 usedBits |= usedBits >> 2;
151 usedBits |= usedBits >> 4;
152 usedBits |= usedBits >> 8;
153 usedBits |= usedBits >> 16;
158 result = (*rng)() & usedBits;
160 }
while (result >= maxV);
165 static inline int rand(
int minV,
int maxV,
SumoRNG* rng =
nullptr) {
166 return minV +
rand(maxV - minV, rng);
170 static inline long long int rand(
long long int maxV,
SumoRNG* rng =
nullptr) {
171 if (maxV <= std::numeric_limits<int>::max()) {
172 return rand((
int)maxV, rng);
174 if (rng ==
nullptr) {
177 unsigned long long int usedBits = maxV - 1;
178 usedBits |= usedBits >> 1;
179 usedBits |= usedBits >> 2;
180 usedBits |= usedBits >> 4;
181 usedBits |= usedBits >> 8;
182 usedBits |= usedBits >> 16;
183 usedBits |= usedBits >> 32;
186 long long int result;
188 result = (((
unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits;
190 }
while (result >= maxV);
195 static inline long long int rand(
long long int minV,
long long int maxV,
SumoRNG* rng =
nullptr) {
196 return minV +
rand(maxV - minV, rng);
200 static double randNorm(
double mean,
double variance,
SumoRNG* rng =
nullptr);
207 static inline const T&
209 assert(v.size() > 0);
210 return v[
rand((
int)v.size(), rng)];
215 if (rng ==
nullptr) {
218 std::ostringstream oss;
221 oss <<
" " << (*rng);
228 if (rng ==
nullptr) {
231 std::istringstream iss(state);
234 rng->discard(rng->count);
242 for (
int i = (
int)(v.size() - 1); i > 0; --i) {
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 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 int rand(int minV, int maxV, SumoRNG *rng=nullptr)
Returns a random integer in [minV, maxV-1].
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 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)
static uint64_t splitmix64(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