ShlubluLib
v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
|
Typedefs | |
using | Engine = std::mt19937_64 |
Engine that is used by all Random functions. | |
Functions | |
template<typename T > | |
T | random (T min, T max) |
Returns a number from a given range. More... | |
template<typename T > | |
T | random (T min, T max, T step) |
Returns a number from a given range and rounded following a given step. More... | |
template<typename T > | |
T | randomUnit () |
Returns a number from a unitary range. More... | |
template<typename T > | |
T | randomRelativeUnit () |
Returns a number from a relative unitary range. More... | |
template<typename T > | |
bool | probability (T p) |
Returns a boolean depending on a probability. More... | |
template<typename T > | |
bool | likelihood (T chance, T total) |
Returns a boolean depending on the likelihood of an event. More... | |
bool | tossACoin () |
Returns a boolean depending on a probability equal to 1/2. More... | |
Set of helper functions intended to make usual operations easier. These functions are based on the standard random header and use a std::mt19937_64
engine. Distribution is std::uniform_real_distribution for real types and std::uniform_int_distribution for integral types.
The engine is initialized automatically. There is no need (and no way) to seed it with a timer or anything.
T shlublu::Random::random | ( | T | min, |
T | max | ||
) |
Returns a number from a given range.
The returned number belongs to the following range:
min
, max
) for real numbers min
, max
] for integers T | the type of min , max and the returned value. This type should be arithmetic. |
min | the minimal value of the range |
max | the maximal value of the range, included for integers and excluded for real numbers. It should be strictly greater than min . |
min
and max
std::invalid_argument | if min > max |
Example
T shlublu::Random::random | ( | T | min, |
T | max, | ||
T | step | ||
) |
Returns a number from a given range and rounded following a given step.
The returned number belongs to the following range:
min
, max
) for real numbers min
, max
] for integers It is rounded to the nearest step.
T | the type of min , max , step and the returned value. This type should be arithmetic. |
min | the minimal value of the range |
max | the maximal value of the range, included for integers and excluded for real numbers. It should be strictly greater than min . |
step | the rounding step. It must be lower than the amplitude of the range. |
min
and max
and rounded to the nearest step
. std::invalid_argument | if step is negative or null, if step is greater than the amplitude of the range, or if min >= max |
Example
T shlublu::Random::randomUnit | ( | ) |
Returns a number from a unitary range.
Depending on T
, the returned value:
T | the type of the returned value. This type should be arithmetic. |
0
and 1
Example
T shlublu::Random::randomRelativeUnit | ( | ) |
Returns a number from a relative unitary range.
Depending on T
, the returned value:
T | the type of the returned value. This type should be arithmetic and signed. |
-1
and 1
Example
bool shlublu::Random::probability | ( | T | p | ) |
Returns a boolean depending on a probability.
This function has a probability of:
p
to return true
1 - p
ti return false
T | the type of the returned value. This type should be real. |
p | the probability of returning true . It should be in the range [0, 1] |
true
or false
depending on a pseudo-random number std::invalid_argument | if p is out of the range [0, 1] |
Example
bool shlublu::Random::likelihood | ( | T | chance, |
T | total | ||
) |
Returns a boolean depending on the likelihood of an event.
This function has a chance
out of total
chance to return true. It returns false
otherwise.
T | the type of the returned value. This type should be integral. |
chance | the numberator of the chance |
total | the denominator of the chance |
true
or false
depending on a pseudo-random number std::invalid_argument | if T is signed and chance is negative or if chance > total |
Example
bool shlublu::Random::tossACoin | ( | ) |
Returns a boolean depending on a probability equal to 1/2.
true
or false
depending on a pseudo-random numberExample