58 const uint64_t t = this->
state[1] << 17;
86 static inline uint64_t
rotl64(
const uint64_t x,
const int k) {
87 return (x << k) | (x >> (64 - k));
91 uint64_t z = (
seed + 0x9e3779b97f4a7c15);
92 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
93 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
123 static void initRand(
SumoRNG* which =
nullptr,
const bool random =
false,
const int seed = 23423);
133 return maxV *
rand(rng);
137 static inline double rand(
double minV,
double maxV,
SumoRNG* rng =
nullptr) {
138 return minV + (maxV - minV) *
rand(rng);
143 if (rng ==
nullptr) {
146 unsigned int usedBits = maxV - 1;
147 usedBits |= usedBits >> 1;
148 usedBits |= usedBits >> 2;
149 usedBits |= usedBits >> 4;
150 usedBits |= usedBits >> 8;
151 usedBits |= usedBits >> 16;
156 result = (*rng)() & usedBits;
158 }
while (result >= maxV);
163 static inline int rand(
int minV,
int maxV,
SumoRNG* rng =
nullptr) {
164 return minV +
rand(maxV - minV, rng);
168 static inline long long int rand(
long long int maxV,
SumoRNG* rng =
nullptr) {
169 if (maxV <= std::numeric_limits<int>::max()) {
170 return rand((
int)maxV, rng);
172 if (rng ==
nullptr) {
175 unsigned long long int usedBits = maxV - 1;
176 usedBits |= usedBits >> 1;
177 usedBits |= usedBits >> 2;
178 usedBits |= usedBits >> 4;
179 usedBits |= usedBits >> 8;
180 usedBits |= usedBits >> 16;
181 usedBits |= usedBits >> 32;
184 long long int result;
186 result = (((
unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits;
188 }
while (result >= maxV);
193 static inline long long int rand(
long long int minV,
long long int maxV,
SumoRNG* rng =
nullptr) {
194 return minV +
rand(maxV - minV, rng);
198 static double randNorm(
double mean,
double variance,
SumoRNG* rng =
nullptr);
205 static inline const T&
207 assert(v.size() > 0);
208 return v[
rand((
int)v.size(), rng)];
213 if (rng ==
nullptr) {
216 std::ostringstream oss;
217 if (rng->count < 1000000) {
227 if (rng ==
nullptr) {
230 std::istringstream iss(state);
231 if (state.size() < 10) {
233 rng->discard(rng->count);
241 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 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