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... | |
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.
|
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.
|
static |
Return a random number generated from a normal distribution.
The normal (or Gaussian) distribution will use the given mean and standard deviation.
| mean | The mean of the distribution. |
| stdev | The standard deviation of the distribution. |
|
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.
|
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.
|
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.
1.8.6