Random.hpp

A versatile and non-patterned pseudo-random-number generator. Status: RELEASE.

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.

class Random
#include <Random.hpp>

Middle Square Weyl Sequence: A versatile and non-patterned pseudo-random-number generator. Based on: https://en.wikipedia.org/wiki/Middle-square_method

Public Types

enum Prob

Enumeration for common probabilities. (not class, so can be referred to elsewhere 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<unsigned char>

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

Public Functions

inline Random(const int seed = -1) noexcept

Set up the random generator object with an optional seed value.

inline ~Random()
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 void ResetSeed(const int64_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 and 1.0

inline double GetDouble(const double max) noexcept
Returns:

A pseudo-random double value between 0.0 and max

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

A pseudo-random double value between min and 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 uint32_t GetUInt() noexcept
Returns:

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

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

A pseudo-random 32-bit unsigned int value between 0 and max

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

A pseudo-random 32-bit unsigned int value between min and max

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

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

inline uint32_t GetBits12_5() noexcept
Returns:

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

inline uint32_t GetBits25() noexcept
Returns:

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

inline uint32_t GetBits37_5() noexcept
Returns:

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

inline uint32_t GetBits50() noexcept
Returns:

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

inline uint32_t GetBits62_5() noexcept
Returns:

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

inline uint32_t GetBits75() noexcept
Returns:

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

inline uint32_t GetBits87_5() noexcept
Returns:

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

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 and max

inline int32_t GetInt(const int32_t max) noexcept
Returns:

A pseudo-random 32-bit (4 byte) int value between 0 and max

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

A pseudo-random 32-bit (4 byte) int value between min and max

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

A pseudo-random 32-bit (4 byte) int value in range

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 each bit being on.

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 each bit being on.

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 unsigned char 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 uint32_t GetPoisson(const double n, const double p)

Generate a random variable drawn from a Poisson distribution.

inline uint32_t GetPoisson(const double mean)

Generate a random variable drawn from a Poisson distribution.

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

Generate a random variable drawn from a Binomial distribution.

This function is exact, but slow.

See also

Random::GetApproxRandBinomial

See also

Binomial in source/tools/Distribution.h

inline double GetExponential(double p)

Generate a random variable drawn from an exponential distribution.

inline uint32_t GetGeometric(double p)

Generate a random variable drawn from a geometric distribution.

Protected Functions

inline uint32_t Get() noexcept

Basic Random number Returns a random number [0, RAND_CAP)

Protected Attributes

uint64_t value = 0

Current squaring value.

uint64_t weyl_state = 0

Weyl sequence state.

uint64_t original_seed = 0

Seed to start sequence; initialized weyl_state.

double expRV = 0.0

Exponential Random Variable for the randNormal function.

Protected Static Attributes

static constexpr const uint64_t RAND_CAP = 4294967296
static constexpr const uint64_t STEP_SIZE = 0xb5ad4eceda1ce2a9

Weyl sequence step size.

static constexpr const unsigned char BYTE1 = (unsigned char)1
struct RandomStdAdaptor
#include <Random.hpp>

This is an adaptor to make Random behave like a proper STL random number generator.

Public Types

typedef int argument_type
typedef int result_type

Public Functions

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

Public Members

Random &_rng