Random.hpp

A versatile pseudo-random-number generator. Status: RELEASE.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2015-2025 Michigan State University MIT Software license; see doc/LICENSE.md

Defines

INCLUDE_EMP_MATH_RANDOM_HPP_GUARD

Typedefs

using RandomBest = Random_Base<Random_Xoshiro256pp>
using Random32 = Random_Base<Random_MSWS>
using RandomFast = Random_Base<Random_Xorshift>
using Random = RandomBest

Functions

template<typename ForwardIterator, typename OutputIterator, typename RNG>
void sample_with_replacement(ForwardIterator first, ForwardIterator last, OutputIterator ofirst, OutputIterator olast, RNG &rng) noexcept

Draw a sample (with replacement) from an input range, copying to the output range.

template<typename ENGINE_T>
class Random_Base
#include <Random.hpp>

Public Types

enum Prob

Enumeration for common probabilities. (not class, so can be referred to as e.g., Random::PROB_50)

Values:

enumerator PROB_0
enumerator PROB_12_5
enumerator PROB_25
enumerator PROB_37_5
enumerator PROB_50
enumerator PROB_62_5
enumerator PROB_75
enumerator PROB_87_5
enumerator PROB_100
using mem_ptr_t = Ptr<uint8_t>

Shortcut type for all functions that deal with chunks of memory.

Public Functions

inline Random_Base(uint64_t seed = 0) noexcept

Set up the random generator object with an optional seed value (0 = based on time and ptr).

inline void StepEngine() noexcept

Advance pseudorandom number generation engine one step.

inline uint64_t GetSeed() const noexcept
Returns:

The current seed used to initialize this pseudo-random sequence.

inline std::string GetEngineType()
Returns:

The type name of the engine.

inline void ResetSeed(uint64_t seed) noexcept

Starts a new sequence of pseudo random numbers. A negative seed means that the random number generator gets its seed from the current system time and the process memory.

inline double GetDouble() noexcept
Returns:

A pseudo-random double value between [0.0, 1.0)

inline double GetDouble(const double max) noexcept
Returns:

A pseudo-random double value between [0.0, max)

inline double GetDouble(const double min, const double max) noexcept
Returns:

A pseudo-random double value between [min, max)

inline double GetDouble(const Range<double> range) noexcept
Returns:

A pseudo-random double in the provided range.

inline double GetDoubleNonZero() noexcept
Returns:

A pseudo-random double value between (0.0, 1.0)

inline uint64_t GetUInt() noexcept
Returns:

A pseudo-random 64-bit (8 byte) unsigned int value.

template<typename T>
inline uint64_t GetUInt(const T max) noexcept
Returns:

A pseudo-random 64-bit unsigned int value between [0, max)

template<typename T1, typename T2>
inline uint64_t GetUInt(const T1 min, const T2 max) noexcept
Returns:

A pseudo-random 64-bit unsigned int value between [min, max)

template<typename T>
inline uint64_t GetUInt(const Range<T> range) noexcept
Returns:

A pseudo-random 64-bit unsigned int value in the provided range.

inline uint32_t GetUInt32() noexcept
Returns:

A pseudo-random 32-bit (4 byte) unsigned int value.

template<typename T>
inline uint32_t GetUInt32(const T max) noexcept
Returns:

A pseudo-random 32-bit unsigned int value between [0, max)

template<typename T1, typename T2>
inline uint32_t GetUInt32(const T1 min, const T2 max) noexcept
Returns:

A pseudo-random 32-bit unsigned int value between [min, max)

template<typename T>
inline uint32_t GetUInt32(const Range<T> range) noexcept
Returns:

A pseudo-random 32-bit unsigned int value in the provided range.

inline uint64_t GetUInt64() noexcept
Returns:

A pseudo-random 64-bit (8 byte) unsigned int value.

inline uint64_t GetUInt64(const uint64_t max) noexcept
Returns:

A pseudo-random 64-bit unsigned int value between [0, max) This is a high-precision accessor with perfectly even probabilities in the range.

template<typename T1, typename T2>
inline uint64_t GetUInt64(const T1 min, const T2 max) noexcept
Returns:

A pseudo-random 64-bit unsigned int value between [min, max)

template<typename T>
inline uint64_t GetUInt64(const Range<T> range) noexcept
Returns:

A pseudo-random 64-bit unsigned int value in the provided range.

inline size_t GetSizeT() noexcept
Returns:

A pseudo-random value that fits perfectly in size_t.

inline size_t GetSizeT(const size_t max) noexcept
Returns:

A pseudo-random size_t value between [0, max) This is a high-precision accessor with perfectly even probabilities in the range.

template<typename T1, typename T2>
inline size_t GetSizeT(const T1 min, const T2 max) noexcept
Returns:

A pseudo-random 64-bit unsigned int value between [min, max)

template<typename T>
inline size_t GetSizeT(const Range<T> range) noexcept
Returns:

A pseudo-random 64-bit unsigned int value in the provided range.

inline int GetInt(const int max) noexcept
Returns:

A pseudo-random int value in [0, max)

inline int GetInt(const int min, const int max) noexcept
Returns:

A pseudo-random int value in [min, max)

inline int GetInt(const Range<int> range) noexcept
Returns:

A pseudo-random int value in range

inline uint64_t GetBits12_5() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 12.5% chance of each bit being 1.

inline uint64_t GetBits25() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 25% chance of each bit being 1.

inline uint64_t GetBits37_5() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 37.5% chance of each bit being 1.

inline uint64_t GetBits50() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 50% chance of each bit being 1.

inline uint64_t GetBits62_5() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 62.5% chance of each bit being 1.

inline uint64_t GetBits75() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 75% chance of each bit being 1.

inline uint64_t GetBits87_5() noexcept
Returns:

A pseudo-random 64 bits (unsigned int) with a 87.5% chance of each bit being 1.

inline void RandFill(mem_ptr_t dest, const size_t num_bytes) noexcept

Randomize a contiguous segment of memory.

template<Prob PROB>
inline void RandFillP(mem_ptr_t dest, const size_t num_bytes) noexcept

Randomize a contiguous segment of memory.

template<Prob PROB>
inline void RandFillP(mem_ptr_t dest, [[maybe_unused]] const size_t num_bytes, size_t start_bit, size_t stop_bit) noexcept

Randomize a contiguous segment of memory between specified bit positions.

inline void RandFill0(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill12_5(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill25(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill37_5(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill50(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill62_5(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill75(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill87_5(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill100(mem_ptr_t dest, const size_t bytes) noexcept
inline void RandFill0(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill12_5(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill25(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill37_5(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill50(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill62_5(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill75(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill87_5(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill100(mem_ptr_t dest, const size_t bytes, size_t start_bit, size_t stop_bit) noexcept
inline void RandFill(mem_ptr_t dest, const size_t num_bytes, const double p) noexcept

Randomize a contiguous segment of memory with a given probability of ones.

inline void RandFill(mem_ptr_t dest, const size_t num_bytes, const double p, const size_t start_bit, const size_t stop_bit) noexcept

Randomize a contiguous segment of memory with a given probability of ones.

inline bool P(const double p) noexcept

Tests a random value [0,1) against a given probability p, and returns true of false.

Parameters:

p – The probability of the result being “true”.

inline uint8_t GetByte(const double p) noexcept

Full random byte with each bit being a one with a given probability.

inline double GetNormal() noexcept

Generate a random variable drawn from a unit normal distribution.

inline double GetNormal(const double mean, const double std)
Parameters:
  • mean – Center of distribution.

  • std – Standard deviation of distribution.

Returns:

A random variable drawn from a normal distribution.

inline uint64_t GetPoisson(const double n, const double p)

Generate a random variable drawn from a Poisson distribution.

inline uint64_t GetPoisson(const double mean)

Generate a random variable drawn from a Poisson distribution.

inline uint64_t GetBinomial(const uint64_t n, const double p)

Generate a random variable drawn from a Binomial distribution.

This function is exact, but slow.

See also

Random_Base::GetApproxRandBinomial

See also

Binomial in source/tools/Distribution.h

inline double GetExponential(double p)

Generate a random variable drawn from an exponential distribution.

inline uint64_t GetGeometric(double p)

Generate a random variable drawn from a geometric distribution.

Private Functions

inline uint32_t Get32() noexcept
inline uint64_t Get64() noexcept
inline auto Get() noexcept

Private Members

ENGINE_T engine
uint64_t original_seed = 0
double expRV = 0.0

Exponential Random Variable for the randNormal function.

Private Static Attributes

static constexpr bool NATIVE64 = std::same_as<decltype(engine.Get()), uint64_t>
static constexpr uint64_t VAL32_CAP = uint64_t{1} << 32
static constexpr double VAL32_CAP_D = static_cast<double>(VAL32_CAP)
static constexpr double VAL32_FRAC = 1.0 / VAL32_CAP_D
static constexpr uint64_t VAL53_CAP = uint64_t{1} << 53
static constexpr double VAL53_CAP_D = static_cast<double>(VAL53_CAP)
static constexpr double VAL53_FRAC = 1.0 / VAL53_CAP_D
struct RandomStdAdaptor
#include <Random.hpp>

This is an adaptor to make Random behave like a Standard Library random number generator.

Public Types

using argument_type = int
using result_type = int

Public Functions

inline RandomStdAdaptor(Random &rng)
inline int operator()(int n)

Public Members

Random &_rng