Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Typedefs | Functions | Variables
inexor::util::random Namespace Reference

Classes

class  auto_seeded
 Wrapper around any RNG Generator that seeds it automatically, by calling boost::uuids::detail::seed. More...
 
class  uniform_open_range_int_distribution
 It is like the boost uniform distribution, but while that uses an closed distribution [min; max], this class uses a half opened one [min; max). More...
 

Typedefs

typedef boost::random::mt19937 rng_engine
 The default random engine for inexor. More...
 
typedef rng_engine::result_type seed_t
 The type of the seed the deterministic_generator expects. More...
 
template<typename T >
using uniform_generic_open_range_distribution = typename std::conditional< std::is_floating_point< T >::value, boost::random::uniform_real_distribution< T >, uniform_open_range_int_distribution< T > >::type
 A generic uniform random number distribution that works on both floating point and integers. More...
 

Functions

template<typename T >
rnd (const T Rmin, const T Rmax)
 Generate a random number between Rmin and Rmax. More...
 
template<typename T >
rnd (const T Rmax)
 Generate a random number between 0 and Rmax. More...
 
template<typename T >
rnd_raw ()
 Generate any random number. More...
 
template<typename T >
deterministic_rnd (const seed_t seed, const T Rmin, const T Rmax)
 Generate a deterministic random number between Rmin and Rmax with a specific seed. More...
 
template<typename T >
deterministic_rnd (const seed_t seed, const T Rmax)
 Generate a deterministic random number between 0 and Rmax with a specific seed. More...
 

Variables

thread_local auto_seeded
< rng_engine
generator
 The default rng, seeded with a time stamp from the best clock availabl (std::chrono::high_resolution_clock) More...
 
thread_local rng_engine deterministic_generator
 The default rng for the deterministic_rng functions; this will be reseeded every time they're called. More...
 

Typedef Documentation

typedef boost::random::mt19937 inexor::util::random::rng_engine

The default random engine for inexor.

typedef rng_engine::result_type inexor::util::random::seed_t

The type of the seed the deterministic_generator expects.

template<typename T >
using inexor::util::random::uniform_generic_open_range_distribution = typedef typename std::conditional< std::is_floating_point<T>::value, boost::random::uniform_real_distribution<T>, uniform_open_range_int_distribution<T> >::type

A generic uniform random number distribution that works on both floating point and integers.

This works by checking whether T is a floating point type; if it is, this template resolves to std::uniform_real_distribution, otherwise std::uniform_int_distribution.

Function Documentation

template<typename T >
T inexor::util::random::deterministic_rnd ( const seed_t  seed,
const T  Rmin,
const T  Rmax 
)

Generate a deterministic random number between Rmin and Rmax with a specific seed.

Since the underlying generator is thread local, this function is thread safe.

For a given seed this will always return the same value.

References deterministic_generator.

Referenced by deterministic_rnd(), and TEST().

template<typename T >
T inexor::util::random::deterministic_rnd ( const seed_t  seed,
const T  Rmax 
)

Generate a deterministic random number between 0 and Rmax with a specific seed.

The same as deterministic_rnd(seed, Rmin, Rmax), but with Rmin=0

References deterministic_rnd().

template<typename T >
T inexor::util::random::rnd ( const T  Rmin,
const T  Rmax 
)

Generate a random number between Rmin and Rmax.

Internally this allocates a uniform_generic_distribution<T> on the fly and uses generator to actually retrieve the random number.

Since the default genera function is thread safe.

TODO: We could generate the randomness in an extra theread and pool it.

Template Parameters
TThe type of random number to generate
Returns
A number in the range [Rmin, Rmax[: Rmin <= rnd(Rmin, Rmax) < Rmax

References generator.

Referenced by rnd(), rnd_raw(), and fpsstate::spawnstate().

template<typename T >
T inexor::util::random::rnd ( const T  Rmax)

Generate a random number between 0 and Rmax.

The same as rnd(Rmin, Rmax), but with Rmin=0

References rnd().

template<typename T >
T inexor::util::random::rnd_raw ( )

Generate any random number.

This generates any possible random number of the given type.

Otherwise this is like rnd(Rmin, Rmax)

References inexor::rpc::max(), inexor::rpc::min(), and rnd().

Variable Documentation

thread_local rng_engine inexor::util::random::deterministic_generator

The default rng for the deterministic_rng functions; this will be reseeded every time they're called.

Referenced by deterministic_rnd().

thread_local auto_seeded< rng_engine > inexor::util::random::generator

The default rng, seeded with a time stamp from the best clock availabl (std::chrono::high_resolution_clock)

Referenced by rnd(), and TEST().