ExRandom
3.0
|
A class to sample integers [0,m). More...
#include <exrandom/i_rand.hpp>
Public Member Functions | |
i_rand (digit_gen &D) | |
template<typename Generator > | |
i_rand & | init (Generator &g, int m) |
template<typename Generator > | |
int | operator() (Generator &g) |
int | min () const |
int | max () const |
int | entropy () const |
void | negate () |
void | add (int c) |
template<typename Generator > | |
bool | less_than (Generator &g, long long m, long long n=1) |
template<typename Generator > | |
bool | less_than_equal (Generator &g, long long m, long long n=1) |
template<typename Generator > | |
bool | greater_than (Generator &g, long long m, long long n=1) |
template<typename Generator > | |
bool | greater_than_equal (Generator &g, long long m, long long n=1) |
std::string | print () const |
template<typename Generator > | |
void | refine (Generator &g) |
Friends | |
std::ostream & | operator<< (std::ostream &os, const i_rand &h) |
A class to sample integers [0,m).
The basic method for sampling is taken from J. Lumbroso (2013), arxiv:1304.1916 generalized to accept random digits in an arbitrary base, b. However, an important additional feature is that only sufficient random digits are drawn to narrow the allowed range to a power of b. Thus, for b = 2, after initializing with m = 9 the i_rand represents
range prob [0,8) 32/63 [0,2) 2/21 [2,6) 4/21 [6,8) 2/21 [8,9) 1/9
min() and max() give the current extent of the closed range. The number of additional random digits needed to fix the value is given by entropy(). The comparison operations may require additional digits to be drawn and so the range might be narrowed down, e.g., to [4,8). If you need a definite value then use operator()().
digit_gen | the type of digit generator. |
Definition at line 44 of file i_rand.hpp.
|
inlineexplicit |
The constructor.
D | A reference to the digit generator to be used. |
Initializes to a random integer in [0,1), i.e., to 0.
Definition at line 54 of file i_rand.hpp.
|
inline |
Initialize.
Generator | the type of g. |
g | the random generator engine. |
m | this must be greater than 0. |
If m is less than 1, it is treated as 1.
Definition at line 67 of file i_rand.hpp.
|
inline |
Sample enough digits to narrow the range to an integer.
Generator | the type of g. |
g | the random generator engine. |
Definition at line 97 of file i_rand.hpp.
|
inline |
Definition at line 102 of file i_rand.hpp.
|
inline |
Definition at line 106 of file i_rand.hpp.
|
inline |
Definition at line 110 of file i_rand.hpp.
|
inline |
Negate the range.
Definition at line 114 of file i_rand.hpp.
|
inline |
Add a constant to the range.
c | the constant to be added. |
Definition at line 120 of file i_rand.hpp.
|
inline |
Test *this < m/n.
Generator | the type of g. |
g | the random generator engine. |
m | the numerator of the fraction. |
n | the denominate of the fraction (default value 1). |
Requires that n > 0.
Definition at line 133 of file i_rand.hpp.
|
inline |
Test *this ≤ m/n.
Generator | the type of g. |
g | the random generator engine. |
m | the numerator of the fraction. |
n | the denominator of the fraction (default value 1). |
Requires that n > 0.
Definition at line 152 of file i_rand.hpp.
|
inline |
Test *this > m/n.
Generator | the type of g. |
g | the random generator engine. |
m | the numerator of the fraction. |
n | the denominator of the fraction (default value 1). |
Requires that n > 0.
Definition at line 166 of file i_rand.hpp.
|
inline |
Test *this ≥ m/n.
Generator | the type of g. |
g | the random generator engine. |
m | the numerator of the fraction. |
n | the denominator of the fraction (default value 1). |
Requires that n > 0.
Definition at line 180 of file i_rand.hpp.
|
inline |
|
inline |
Refine the range by sampling an extra digit.
Generator | the type of g. |
g | the random generator engine. |
Definition at line 204 of file i_rand.hpp.
|
friend |
Print an i_rand. Format is min+[0,max-min+1) unless the entropy is zero, in which case it's val.
os | an output stream. |
h | an i_rand. |
Definition at line 216 of file i_rand.hpp.