random_utils.hpp

Helper functions for Random for common random tasks. Status: RELEASE.

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

Defines

INCLUDE_EMP_MATH_RANDOM_UTILS_HPP_GUARD

Functions

template<typename T, typename RAND_T>
inline auto SelectRandom(RAND_T &random, const T &container)

Choose a random element from an indexable container.

template<typename T, typename RAND_T>
inline void Shuffle(RAND_T &random, vector<T> &v, size_t max_count)

Randomly reorder all of the elements in a vector. If max_count is provided, just make sure that the first max_count entries are randomly drawn from entire vector.

template<typename T, typename RAND_T>
inline void Shuffle(RAND_T &random, vector<T> &v)
template<typename T, typename RAND_T>
inline void ShuffleRange(RAND_T &random, vector<T> &v, size_t first, size_t last)
inline vector<size_t> GetPermutation(auto &random, size_t size)

Return an vector<int> numbered 0 through size-1 in a random order.

inline void Choose(auto &random, size_t N, size_t K, std::vector<size_t> &choices)

Choose K positions from N possibilities.

inline std::vector<size_t> Choose(auto &random, size_t N, size_t K)
inline BitVector RandomBitVector(auto &random, size_t size, double p = 0.5)

Generate a random BitVector of the specified size.

inline vector<double> RandomDoubleVector(auto &random, size_t size, double min, double max)

Generate a random double vector in the specified range.

inline vector<size_t> RandomUIntVector(auto &random, size_t size, size_t min, size_t max)

Generate a random size_t vector in the specified range.

template<typename T>
inline vector<T> RandomVector(auto &random, size_t size, T min, T max)

Generate a random vector in the specified type and range.

inline void RandomizeBitVector(BitVector &bits, auto &random, double p = 0.5)

Generate a random BitVector of the specified size.

template<typename T, typename RAND_T>
inline void RandomizeVector(vector<T> &vals, RAND_T &random, T min, T max)

Generate a random vector in the specified type and range.

template<typename RAND_T>
inline size_t CountRngTouches(std::function<void(RAND_T&)> routine)
inline int UnbiasedDivide(int dividend, int divisor, Random &r)

Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.

inline size_t UnbiasedDivide(size_t dividend, size_t divisor, Random &r)

Regular integer division is truncated, not rounded. Round the division result instead of truncating it. Rounding ties (i.e., result % divisor == 0.5) are broken by coin toss.

class RandProbability
#include <random_utils.hpp>

Public Functions

inline RandProbability(double p)
template<typename RAND_T>
inline bool Test(RAND_T &random)

Private Members

const double p
class LowProbability
#include <random_utils.hpp>

Public Functions

inline LowProbability(double p)
template<typename RAND_T>
inline bool Test(RAND_T &random)

Private Functions

inline double CalcScale(double p) const

Private Members

const double scale
uint32_t next = 0
class HighProbability
#include <random_utils.hpp>

Public Functions

inline HighProbability(double p)
template<typename RAND_T>
inline bool Test(RAND_T &random)

Private Functions

inline double CalcScale(double p) const

Private Members

const double scale
size_t next = 0
class BufferedProbability
#include <random_utils.hpp>

Public Functions

inline BufferedProbability(double p)
template<typename RAND_T>
inline bool Test(RAND_T &random)

Private Functions

template<typename RAND_T>
inline void ResetBuffer(RAND_T &random)

Private Members

const double p
array<uint8_t, BUFFER_SIZE> buffer
uint32_t next = BUFFER_SIZE

Private Static Attributes

static constexpr uint32_t BUFFER_SIZE = 256