ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Typedefs | Functions
shlublu::Random Namespace Reference

Typedefs

using Engine = std::mt19937_64
 Engine that is used by all Random functions.
 

Functions

template<typename T >
random (T min, T max)
 Returns a number from a given range. More...
 
template<typename 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 >
randomUnit ()
 Returns a number from a unitary range. More...
 
template<typename 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...
 

Detailed Description

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.

Function Documentation

◆ random() [1/2]

template<typename T >
T shlublu::Random::random ( min,
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
Template Parameters
Tthe type of min, max and the returned value. This type should be arithmetic.
Parameters
minthe minimal value of the range
maxthe maximal value of the range, included for integers and excluded for real numbers. It should be strictly greater than min.
Returns
a pseudo-random number in the range defined by min and max
Exceptions
std::invalid_argumentif min > max

Example

std::cout << Random::random(-5.0, 5.0) << std::endl; // For example: 2.58067

◆ random() [2/2]

template<typename T >
T shlublu::Random::random ( min,
max,
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.

Template Parameters
Tthe type of min, max, step and the returned value. This type should be arithmetic.
Parameters
minthe minimal value of the range
maxthe maximal value of the range, included for integers and excluded for real numbers. It should be strictly greater than min.
stepthe rounding step. It must be lower than the amplitude of the range.
Returns
a pseudo-random number in the range defined by min and max and rounded to the nearest step.
Exceptions
std::invalid_argumentif step is negative or null, if step is greater than the amplitude of the range, or if min >= max

Example

std::cout << Random::random(-5.0, 5.0, 0.25) << std::endl; // For example: -3.75

◆ randomUnit()

template<typename T >
T shlublu::Random::randomUnit ( )

Returns a number from a unitary range.

Depending on T, the returned value:

  • is an element of [0, 1) for real numbers
  • is either 0 or 1 for integers
Template Parameters
Tthe type of the returned value. This type should be arithmetic.
Returns
a pseudo-random number in the range defined by 0 and 1

Example

std::cout << Random::randomUnit<double>() << std::endl; // For example: 0.579902

◆ randomRelativeUnit()

template<typename T >
T shlublu::Random::randomRelativeUnit ( )

Returns a number from a relative unitary range.

Depending on T, the returned value:

  • is an element of [-1, 1) for real numbers
  • is either -1, 0 or 1 for integers
Template Parameters
Tthe type of the returned value. This type should be arithmetic and signed.
Returns
a pseudo-random number in the range defined by -1 and 1

Example

std::cout << Random::randomRelativeUnit<double>() << std::endl; // For example: -0.926407

◆ probability()

template<typename T >
bool shlublu::Random::probability ( p)

Returns a boolean depending on a probability.

This function has a probability of:

  • p to return true
  • 1 - p ti return false
Template Parameters
Tthe type of the returned value. This type should be real.
Parameters
pthe probability of returning true. It should be in the range [0, 1]
Returns
true or false depending on a pseudo-random number
Exceptions
std::invalid_argumentif p is out of the range [0, 1]

Example

std::cout << (Random::probability(0.6) ? "yes" : "no") << std::endl; // For example: "yes"

◆ likelihood()

template<typename T >
bool shlublu::Random::likelihood ( chance,
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.

Template Parameters
Tthe type of the returned value. This type should be integral.
Parameters
chancethe numberator of the chance
totalthe denominator of the chance
Returns
true or false depending on a pseudo-random number
Exceptions
std::invalid_argumentif T is signed and chance is negative or if chance > total

Example

std::cout << (Random::likelihood(3, 11) ? "yes" : "no") << std::endl; // For example: "no"

◆ tossACoin()

bool shlublu::Random::tossACoin ( )

Returns a boolean depending on a probability equal to 1/2.

Returns
true or false depending on a pseudo-random number

Example

std::cout << (Random::tossACoin() ? "heads" : "tails") << std::endl; // For example: "heads"
shlublu::Random::likelihood
bool likelihood(T chance, T total)
Returns a boolean depending on the likelihood of an event.
Definition: Random.h:226
shlublu::Random::tossACoin
bool tossACoin()
Returns a boolean depending on a probability equal to 1/2.
shlublu::Random::probability
bool probability(T p)
Returns a boolean depending on a probability.
Definition: Random.h:187
shlublu::Random::random
T random(T min, T max)
Returns a number from a given range.
Definition: Random.h:56