45 static void initRand(std::mt19937* which =
nullptr,
const bool random =
false,
const int seed = 23423);
51 static inline double rand(std::mt19937* rng =
nullptr) {
55 const double res = double((*rng)() / 4294967296.0);
56 #ifdef DEBUG_RANDCALLS
58 if (myCallCount[rng] == myDebugIndex) {
59 std::cout <<
"DEBUG\n";
61 std::stringstream stream;
62 stream <<
" rng" << myRngId.find(rng)->second <<
" rand call=" << myCallCount[rng] <<
" val=" << res <<
"\n";
63 std::cout << stream.str();
69 static inline double rand(
double maxV, std::mt19937* rng = 0) {
70 return maxV *
rand(rng);
74 static inline double rand(
double minV,
double maxV, std::mt19937* rng = 0) {
75 return minV + (maxV - minV) *
rand(rng);
79 static inline int rand(
int maxV, std::mt19937* rng = 0) {
83 unsigned int usedBits = maxV - 1;
84 usedBits |= usedBits >> 1;
85 usedBits |= usedBits >> 2;
86 usedBits |= usedBits >> 4;
87 usedBits |= usedBits >> 8;
88 usedBits |= usedBits >> 16;
93 result = (*rng)() & usedBits;
94 }
while (result >= maxV);
99 static inline int rand(
int minV,
int maxV, std::mt19937* rng = 0) {
100 return minV +
rand(maxV - minV, rng);
104 static inline long long int rand(
long long int maxV, std::mt19937* rng = 0) {
105 if (maxV <= std::numeric_limits<int>::max()) {
106 return rand((
int)maxV, rng);
111 unsigned long long int usedBits = maxV - 1;
112 usedBits |= usedBits >> 1;
113 usedBits |= usedBits >> 2;
114 usedBits |= usedBits >> 4;
115 usedBits |= usedBits >> 8;
116 usedBits |= usedBits >> 16;
117 usedBits |= usedBits >> 32;
120 long long int result;
122 result = (((
unsigned long long int)(*rng)() << 32) | (*rng)()) & usedBits;
123 }
while (result >= maxV);
128 static inline long long int rand(
long long int minV,
long long int maxV, std::mt19937* rng = 0) {
129 return minV +
rand(maxV - minV, rng);
133 static inline double randNorm(
double mean,
double variance, std::mt19937* rng = 0) {
137 u =
rand(2.0, rng) - 1;
138 const double v =
rand(2.0, rng) - 1;
140 }
while (q == 0.0 || q >= 1.0);
141 return (
double)(mean + variance * u * sqrt(-2 * log(q) / q));
146 static inline const T&
148 assert(v.size() > 0);
149 return v[
rand((
int)v.size(), rng)];
157 std::ostringstream oss;
163 static void loadState(
const std::string& state, std::mt19937* rng = 0) {
167 std::istringstream iss(state);
176 #ifdef DEBUG_RANDCALLS
177 static std::map<std::mt19937*, int> myCallCount;
178 static std::map<std::mt19937*, int> myRngId;
179 static int myDebugIndex;
Utility functions for using a global, resetable random number generator.
static double rand(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
static double rand(double minV, double maxV, std::mt19937 *rng=0)
Returns a random real number in [minV, maxV)
static std::string saveState(std::mt19937 *rng=0)
save rng state to string
static void loadState(const std::string &state, std::mt19937 *rng=0)
load rng state from string
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 void initRand(std::mt19937 *which=nullptr, 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.
static void initRandGlobal(std::mt19937 *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
static void insertRandOptions()
Initialises the given options container with random number options.
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 maxV, std::mt19937 *rng=0)
Returns a random 64 bit integer in [0, maxV-1].
static double rand(double maxV, std::mt19937 *rng=0)
Returns a random real number in [0, maxV)
static int rand(int minV, int maxV, std::mt19937 *rng=0)
Returns a random integer in [minV, maxV-1].
static std::mt19937 myRandomNumberGenerator
the random number generator to use
static int rand(int maxV, std::mt19937 *rng=0)
Returns a random integer in [0, maxV-1].