58 static void initRand(std::mt19937* which = 0,
const bool random =
false,
const int seed = 23423);
64 static inline double rand(std::mt19937* rng = 0) {
68 return double((*rng)() / 4294967296.0);
72 static inline double rand(
double maxV, std::mt19937* rng = 0) {
73 return maxV *
rand(rng);
77 static inline double rand(
double minV,
double maxV, std::mt19937* rng = 0) {
78 return minV + (maxV - minV) *
rand(rng);
82 static inline int rand(
int maxV, std::mt19937* rng = 0) {
86 unsigned int usedBits = maxV - 1;
87 usedBits |= usedBits >> 1;
88 usedBits |= usedBits >> 2;
89 usedBits |= usedBits >> 4;
90 usedBits |= usedBits >> 8;
91 usedBits |= usedBits >> 16;
96 result = (*rng)() & usedBits;
97 }
while (result >= maxV);
102 static inline int rand(
int minV,
int maxV, std::mt19937* rng = 0) {
103 return minV +
rand(maxV - minV, rng);
107 static inline long long int rand(
long long int maxV, std::mt19937* rng = 0) {
108 if (maxV <= std::numeric_limits<int>::max()) {
109 return rand((
int)maxV, rng);
114 unsigned long long int usedBits = maxV - 1;
115 usedBits |= usedBits >> 1;
116 usedBits |= usedBits >> 2;
117 usedBits |= usedBits >> 4;
118 usedBits |= usedBits >> 8;
119 usedBits |= usedBits >> 16;
120 usedBits |= usedBits >> 32;
123 long long int result;
125 result = (((
unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits;
126 }
while (result >= maxV);
131 static inline long long int rand(
long long int minV,
long long int maxV, std::mt19937* rng = 0) {
132 return minV +
rand(maxV - minV, rng);
136 static inline double randNorm(
double mean,
double variance, std::mt19937* rng = 0) {
140 u =
rand(2.0, rng) - 1;
141 const double v =
rand(2.0, rng) - 1;
143 }
while (q == 0.0 || q >= 1.0);
144 return (
double)(mean + variance * u * sqrt(-2 * log(q) / q));
149 static inline const T&
151 assert(v.size() > 0);
152 return v[
rand((
int)v.size(), rng)];
static double rand(double minV, double maxV, std::mt19937 *rng=0)
Returns a random real number in [minV, maxV)
static void insertRandOptions()
Initialises the given options container with random number options.
static double rand(double maxV, std::mt19937 *rng=0)
Returns a random real number in [0, maxV)
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
static int rand(int minV, int maxV, std::mt19937 *rng=0)
Returns a random integer in [minV, maxV-1].
static long long int rand(long long int maxV, std::mt19937 *rng=0)
Returns a random 64 bit integer in [0, maxV-1].
Utility functions for using a global, resetable random number generator.
static int rand(int maxV, std::mt19937 *rng=0)
Returns a random integer in [0, maxV-1].
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
static long long int rand(long long int minV, long long int maxV, std::mt19937 *rng=0)
Returns a random 64 bit integer in [minV, maxV-1].
static std::mt19937 myRandomNumberGenerator
the random number generator to use
static void initRand(std::mt19937 *which=0, const bool random=false, const int seed=23423)
Initialises the random number generator with hardware randomness or seed.
static const T & getRandomFrom(const std::vector< T > &v, std::mt19937 *rng=0)
Returns a random element from the given vector.
A storage for options typed value containers)
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...