Lazarus
Engine for creating roguelikes in C++
 All Classes Namespaces Functions
Static Public Member Functions | List of all members
lz::Random Class Reference

The Random class provides a simple interface for commonly used RNG functionality. More...

#include <Random.h>

Static Public Member Functions

static void seed ()
 Sets a new seed for the random generator using a hardware random device if available, or a seed using the current time.
 
static void seed (unsigned seed)
 Sets the random generator to use the given seed. More...
 
template<typename T , typename U , typename std::enable_if_t< (std::is_integral< T >::value||std::is_unsigned< T >::value)&&(std::is_integral< U >::value||std::is_unsigned< U >::value) > * = nullptr>
static std::common_type< T, U >
::type 
range (T a, U b)
 Return a random integral between the two given numbers with equal probability. More...
 
template<typename T , typename U , typename std::enable_if_t< std::is_floating_point< T >::value||std::is_floating_point< U >::value > * = nullptr>
static std::common_type< T, U >
::type 
range (T a, U b)
 Return a random floating point number between the two given numbers with equal probability. More...
 
static ulong roll (unsigned sides=6, unsigned times=1)
 Rolls a dice with the specified number of sides a certain number of times and returns the total result.
 
static bool oneIn (unsigned n)
 Return true with a 1 in n probability.
 
static double normal (double mean, double stdev)
 Return a random number generated from a normal distribution. More...
 
template<typename C , typename T = typename C::value_type>
static T & choice (C &container)
 Return a reference to a random item from a container. More...
 

Detailed Description

The Random class provides a simple interface for commonly used RNG functionality.

It is meant to be available from everywhere by providing static methods, but encapsulating the generation logic inside of it.

It is important that the user calls lz::Random::seed() at the beginning of their program to correctly seed the generator used with a pseudo-random number. After that, the user can call the methods from anywhere in their program, without having to ever instantiate a Random object, by just calling the methods within the namespace provided.

For example, lz::Random::range(1, 5) will produce a random integer between 1 and 5.

Member Function Documentation

template<typename C , typename T >
T & lz::Random::choice ( C &  container)
static

Return a reference to a random item from a container.

The container must support the size() method, and the operator [] to get an element from the container. If the container is empty, an exception will be thrown.

double Random::normal ( double  mean,
double  stdev 
)
static

Return a random number generated from a normal distribution.

The normal (or Gaussian) distribution will use the given mean and standard deviation.

Parameters
meanThe mean of the distribution.
stdevThe standard deviation of the distribution.
template<typename T , typename U , typename std::enable_if_t< (std::is_integral< T >::value||std::is_unsigned< T >::value)&&(std::is_integral< U >::value||std::is_unsigned< U >::value) > * = nullptr>
static std::common_type<T, U>::type lz::Random::range ( a,
b 
)
inlinestatic

Return a random integral between the two given numbers with equal probability.

If a < b, then it will return an integral in [a, b]. Otherwise, it will be in [b, a].

Type is automatically detected, and the return type will be the most appropriate type depending on the argument types. For example, range(int, unsigned) will return an unsigned, and range(short, long) will return a long.

template<typename T , typename U , typename std::enable_if_t< std::is_floating_point< T >::value||std::is_floating_point< U >::value > * = nullptr>
static std::common_type<T, U>::type lz::Random::range ( a,
b 
)
inlinestatic

Return a random floating point number between the two given numbers with equal probability.

If a < b, then it will return an integral in [a, b). Otherwise, it will be in [b, a).

Type is automatically detected, and the return type will be the most appropriate type depending on the argument types. For example, range(float, double) will return a double, and range(int, float) will return a float.

void Random::seed ( unsigned  seed)
static

Sets the random generator to use the given seed.

Note that using the same seed will produce the same sequences of random numbers in different executions. This can be useful, for example, for testing.


The documentation for this class was generated from the following files: