ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
ppforest2::stats::Uniform Class Reference

Discrete uniform random integer generator over [min, max]. More...

#include <Uniform.hpp>

Public Member Functions

 Uniform (int min, int max)
 Construct a uniform integer generator over [min, max].
 
std::vector< int > distinct (int count, RNG &rng)
 Sample without replacement from [min, max].
 
std::vector< int > operator() (int count, RNG &rng) const
 Generate multiple uniform random integers (with replacement).
 
int operator() (RNG &rng) const
 Generate a single uniform random integer in [min, max].
 

Detailed Description

Discrete uniform random integer generator over [min, max].

All random integers are produced via Lemire's nearly-divisionless method, which replaces the classical modulo reduction with a multiply-and-shift, falling back to rejection sampling only when needed to eliminate bias. This is both faster and provably unbiased.

The distinct() method builds on this by performing a Fisher-Yates (Knuth) shuffle to sample without replacement — the foundation of bootstrap sampling and random variable selection in forests.

Note
This class is used instead of std::uniform_int_distribution to guarantee identical output across compilers and platforms.
See also
https://arxiv.org/abs/1805.10941

Constructor & Destructor Documentation

◆ Uniform()

ppforest2::stats::Uniform::Uniform ( int min,
int max )

Construct a uniform integer generator over [min, max].

Parameters
minInclusive lower bound (must be ≥ 0).
maxInclusive upper bound (must be ≥ min).

Member Function Documentation

◆ distinct()

std::vector< int > ppforest2::stats::Uniform::distinct ( int count,
RNG & rng )

Sample without replacement from [min, max].

Implements the Fisher-Yates (Knuth) shuffle: fills a vector with [min, max], shuffles it using gen_lemire() for each swap, and returns the first count elements. The result is a uniformly distributed subset of size count with no repeats.

This is the method used by vars::Uniform and bootstrap sampling, ensuring reproducible variable selection across platforms.

Parameters
countNumber of distinct integers to draw (≤ range size).
rngRandom number generator (pcg32).
Returns
Vector of count distinct integers from [min, max].

◆ operator()() [1/2]

std::vector< int > ppforest2::stats::Uniform::operator() ( int count,
RNG & rng ) const

Generate multiple uniform random integers (with replacement).

Parameters
countNumber of samples to generate.
rngRandom number generator (pcg32).
Returns
Vector of count i.i.d. integers from [min, max].

◆ operator()() [2/2]

int ppforest2::stats::Uniform::operator() ( RNG & rng) const

Generate a single uniform random integer in [min, max].

Parameters
rngRandom number generator (pcg32).
Returns
Uniformly distributed integer in [min, max].

The documentation for this class was generated from the following file: