ModErn Text Analysis
META Enumerates Textual Applications
Classes | Functions
meta::random Namespace Reference

A collection of utility classes/functions for randomness. More...

Classes

class  any_rng
 A class to type-erase any unsigned random number generator in a way that makes STL algorithms happy. More...
 

Functions

template<class RandomEngine >
RandomEngine::result_type bounded_rand (RandomEngine &rng, typename RandomEngine::result_type upper_bound)
 Generate a random number between 0 and an (exclusive) upper bound. More...
 
template<class RandomAccessIterator , class RandomEngine >
void shuffle (RandomAccessIterator first, RandomAccessIterator last, RandomEngine &&rng)
 Shuffles the given range using the provided rng. More...
 

Detailed Description

A collection of utility classes/functions for randomness.

(e.g. random number generation, shuffling, etc.).

Function Documentation

§ bounded_rand()

template<class RandomEngine >
RandomEngine::result_type meta::random::bounded_rand ( RandomEngine &  rng,
typename RandomEngine::result_type  upper_bound 
)

Generate a random number between 0 and an (exclusive) upper bound.

This uses the rejection sampling technique, and it assumes that the RandomEngine has a strictly larger range than the desired one.

Parameters
rngThe rng to generate numbers from
upper_boundThe exclusive upper bound for the number
Returns
a random number in the range [0, upper_bound)

§ shuffle()

template<class RandomAccessIterator , class RandomEngine >
void meta::random::shuffle ( RandomAccessIterator  first,
RandomAccessIterator  last,
RandomEngine &&  rng 
)

Shuffles the given range using the provided rng.

THERE IS A REASON we don't use std::shuffle here: we want reproducibility between compilers, who don't seem to agree on the number of times to call rng_ in the shuffle process.

Furthermore, it seems that we can't rely on a canonical number of rng_ calls in std::uniform_int_distribution, either, so that's out too.

We instead use random::bounded_rand(), since we know that the range of the RNG is definitely going to be larger than the upper bounds we request here.

Parameters
firstThe iterator to the beginning of the range to be shuffled
lastThe iterator to the end of the range to be shuffled
rngThe random number generator to use