...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::random::uniform_smallint
// In header: <boost/random/uniform_smallint.hpp> template<typename IntType = int> class uniform_smallint { public: // types typedef IntType input_type; typedef IntType result_type; // member classes/structs/unions class param_type { public: // types typedef uniform_smallint distribution_type; // construct/copy/destruct param_type(IntType = 0, IntType = 9); // public member functions IntType a() const; IntType b() const; // friend functions template<typename CharT, typename Traits> std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &, const param_type &); template<typename CharT, typename Traits> std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &, const param_type &); bool operator==(const param_type &, const param_type &); bool operator!=(const param_type &, const param_type &); }; // construct/copy/destruct explicit uniform_smallint(IntType = 0, IntType = 9); explicit uniform_smallint(const param_type &); // public member functions result_type a() const; result_type b() const; result_type min() const; result_type max() const; param_type param() const; void param(const param_type &); void reset(); template<typename Engine> result_type operator()(Engine &) const; template<typename Engine> result_type operator()(Engine &, const param_type &) const; // friend functions template<typename CharT, typename Traits> std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &, const uniform_smallint &); template<typename CharT, typename Traits> std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &, const uniform_smallint &); bool operator==(const uniform_smallint &, const uniform_smallint &); bool operator!=(const uniform_smallint &, const uniform_smallint &); };
The distribution function uniform_smallint models a random distribution . On each invocation, it returns a random integer value uniformly distributed in the set of integer numbers {min, min+1, min+2, ..., max}. It assumes that the desired range (max-min+1) is small compared to the range of the underlying source of random numbers and thus makes no attempt to limit quantization errors.
Let the desired range of integer numbers, and let
be the range of the underlying source of random numbers. Then, for the uniform distribution, the theoretical probability for any number i in the range
will be
. Likewise, assume a uniform distribution on
for the underlying source of random numbers, i.e.
. Let
denote the random distribution generated by
uniform_smallint
. Then the sum over all i in of
shall not exceed
.
The template parameter IntType shall denote an integer-like value type.
![]() |
Note |
---|---|
The property above is the square sum of the relative differences in probabilities between the desired uniform distribution |
Note: The upper bound for is
. Regarding the upper bound for the square sum of the relative quantization error of
, it seems wise to either choose
so that
or ensure that
is divisible by
.
uniform_smallint
public
construct/copy/destructexplicit uniform_smallint(IntType min = 0, IntType max = 9);
Constructs a
. uniform_smallint
min
and max
are the lower and upper bounds of the output range, respectively.
explicit uniform_smallint(const param_type & param);
Constructs a
from its parameters. uniform_smallint
uniform_smallint
public member functionsresult_type a() const;
Returns the minimum value of the distribution.
result_type b() const;
Returns the maximum value of the distribution.
result_type min() const;
Returns the minimum value of the distribution.
result_type max() const;
Returns the maximum value of the distribution.
param_type param() const;
Returns the parameters of the distribution.
void param(const param_type & param);
Sets the parameters of the distribution.
void reset();
Effects: Subsequent uses of the distribution do not depend on values produced by any engine prior to invoking reset.
template<typename Engine> result_type operator()(Engine & eng) const;
Returns a value uniformly distributed in the range [min(), max()].
template<typename Engine> result_type operator()(Engine & eng, const param_type & param) const;
Returns a value uniformly distributed in the range [param.a(), param.b()].
uniform_smallint
friend functionstemplate<typename CharT, typename Traits> std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > & os, const uniform_smallint & ud);
Writes the distribution to a std::ostream
.
template<typename CharT, typename Traits> std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > & is, const uniform_smallint & ud);
Reads the distribution from a std::istream
.
bool operator==(const uniform_smallint & lhs, const uniform_smallint & rhs);
Returns true if the two distributions will produce identical sequences of values given equal generators.
bool operator!=(const uniform_smallint & lhs, const uniform_smallint & rhs);
Returns true if the two distributions may produce different sequences of values given equal generators.