Vita
Namespaces | Typedefs | Enumerations | Functions
random.h File Reference

Go to the source code of this file.

Namespaces

namespace  vita
 The main namespace for the project.
 

Typedefs

using vita::random::engine_t = vigna::xoshiro256ss
 xoshiro256** (XOR/shift/rotate) is an all-purpose, rock-solid generator (not a cryptographically secure generator). More...
 

Enumerations

enum class  distribution { uniform , normal }
 

Functions

template<class T >
std::enable_if_t< std::is_floating_point_v< T >, T > vita::random::between (T min, T sup)
 A specialization for floating point values of the random::between(T, T) template function. More...
 
template<class T >
std::enable_if_t< std::is_integral_v< T >, T > vita::random::between (T min, T sup)
 Picks up a random integer value uniformly distributed in the set of integers {min, min+1, ..., sup-1}. More...
 
template<class T >
std::enable_if_t< std::is_enum_v< T >, T > vita::random::between (T min, T sup)
 
bool vita::random::boolean (double=0.5)
 
template<class C >
C::value_type & vita::random::element (C &c)
 
template<class C >
const C::value_type & vita::random::element (const C &c)
 
template<class T >
vita::random::ephemeral (distribution d, T p1, T p2)
 Used for ephemeral random constant generation. More...
 
template<class T >
vita::random::in (range_t< T > r)
 Uniformly extracts a random value in a range. More...
 
void vita::random::randomize ()
 Sets the shared engine to an unpredictable state. More...
 
unsigned vita::random::ring (unsigned base, unsigned width, unsigned n)
 Returns a random number in a modular arithmetic system. More...
 
void vita::random::seed (unsigned s)
 Initalizes the random number generator. More...
 
template<class T >
vita::random::sup (T)
 

Detailed Description

Remarks
This file is part of VITA.
License\n
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

Definition in file random.h.

Typedef Documentation

◆ engine_t

using vita::random::engine_t = typedef vigna::xoshiro256ss

xoshiro256** (XOR/shift/rotate) is an all-purpose, rock-solid generator (not a cryptographically secure generator).

It has excellent (sub-ns) speed, a state space (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of.

Definition at line 34 of file random.h.

Enumeration Type Documentation

◆ distribution

enum class vita::random::distribution
strong

Definition at line 26 of file random.h.

Function Documentation

◆ between() [1/3]

template<class T >
std::enable_if_t< std::is_floating_point_v< T >, T > vita::random::between ( min,
sup 
)

A specialization for floating point values of the random::between(T, T) template function.

Parameters
[in]minminimum random number
[in]supupper bound
Returns
a random double in the [min;sup[ range
See also
For further details:

Definition at line 85 of file random.h.

◆ between() [2/3]

template<class T >
std::enable_if_t< std::is_integral_v< T >, T > vita::random::between ( min,
sup 
)

Picks up a random integer value uniformly distributed in the set of integers {min, min+1, ..., sup-1}.

Parameters
[in]minminimum random number
[in]supupper bound
Returns
a random number in the [min;sup[ range
Note
Contrary to boost usage this function does not take a closed range. Instead it takes a half-open range (C++ usage and same behaviour of the real number distribution).

Definition at line 108 of file random.h.

◆ between() [3/3]

template<class T >
std::enable_if_t< std::is_enum_v< T >, T > vita::random::between ( min,
sup 
)

Definition at line 118 of file random.h.

◆ boolean()

bool vita::random::boolean ( double  p)
inline
Parameters
[in]pa probability ([0;1] range)
Returns
true p% times
Note
bool values are produced according to the Bernoulli distribution.

Definition at line 183 of file random.h.

◆ element() [1/2]

template<class C >
C::value_type & vita::random::element ( C &  c)
Parameters
[in]ca STL container
Returns
a random element of container c

Definition at line 168 of file random.h.

◆ element() [2/2]

template<class C >
const C::value_type & vita::random::element ( const C &  c)
Parameters
[in]ca STL container
Returns
a random element of container c
Examples
example4.cc, and titanic04.cc.

Definition at line 154 of file random.h.

◆ ephemeral()

template<class T >
T vita::random::ephemeral ( distribution  d,
p1,
p2 
)

Used for ephemeral random constant generation.

Parameters
[in]dtype of distribution
[in]p1minimum for uniform distribution; mean for normal distribution
[in]p2maximum for uniform distribution, standard deviation for normal distribution
Returns
a random number distributed according to distribution d

Definition at line 58 of file random.h.

◆ in()

template<class T >
T vita::random::in ( range_t< T >  r)

Uniformly extracts a random value in a range.

Parameters
[in]ra half-open range
Returns
a random value in range r

Definition at line 132 of file random.h.

◆ randomize()

void vita::random::randomize ( )

Sets the shared engine to an unpredictable state.

Definition at line 50 of file random.cc.

◆ ring()

unsigned vita::random::ring ( unsigned  base,
unsigned  width,
unsigned  n 
)

Returns a random number in a modular arithmetic system.

Parameters
[in]basea base number
[in]widthmaximum distance from the base number
[in]nmodulus
Returns
a random number in the [base - width/2, base + width/2] mod n interval

Definition at line 65 of file random.cc.

◆ seed()

void vita::random::seed ( unsigned  s)

Initalizes the random number generator.

Parameters
[in]sthe seed for the random number generator

The seed is used to initalize the random number generator. With the same seed the numbers produced will be the same every time the program is run.

Note
A common method to seed a PRNG is using the current time (std::time(0)). It works... but the preferred way in Vita is the randomize method (which is based on std::random_device).

Definition at line 42 of file random.cc.

◆ sup()

template<class T >
T vita::random::sup ( sup)
Parameters
[in]supupper bound
Returns
a random number in the [0;sup[ range
Note
This is a shortcut for: between<T>(0, sup)

Definition at line 144 of file random.h.