Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::random::uniform_smallint< IntType > Class Template Reference

The distribution function uniform_smallint models a . More...

#include <uniform_smallint.hpp>

Classes

class  param_type
 

Public Types

typedef IntType input_type
 
typedef IntType result_type
 

Public Member Functions

 uniform_smallint (IntType min_arg=0, IntType max_arg=9)
 Constructs a uniform_smallint. More...
 
 uniform_smallint (const param_type &parm)
 Constructs a uniform_smallint from its parameters. More...
 
result_type a () const
 Returns the minimum value of the distribution. More...
 
result_type b () const
 Returns the maximum value of the distribution. More...
 
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const
 Returns the minimum value of the distribution. More...
 
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const
 Returns the maximum value of the distribution. More...
 
param_type param () const
 Returns the parameters of the distribution. More...
 
void param (const param_type &parm)
 Sets the parameters of the distribution. More...
 
void reset ()
 Effects: Subsequent uses of the distribution do not depend on values produced by any engine prior to invoking reset. More...
 
template<class Engine >
result_type operator() (Engine &eng) const
 Returns a value uniformly distributed in the range [min(), max()]. More...
 
template<class Engine >
result_type operator() (Engine &eng, const param_type &parm) const
 Returns a value uniformly distributed in the range [param.a(), param.b()]. More...
 
 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR (os, uniform_smallint, ud)
 Writes the distribution to a std::ostream. More...
 
 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR (is, uniform_smallint, ud)
 Reads the distribution from a std::istream. More...
 
 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR (uniform_smallint, lhs, rhs)
 Returns true if the two distributions will produce identical sequences of values given equal generators. More...
 

Detailed Description

template<class IntType = int>
class boost::random::uniform_smallint< IntType >

The distribution function uniform_smallint models a .

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 $r_{\mathtt{out}} = (\mbox{max}-\mbox{min}+1)$ the desired range of integer numbers, and let $r_{\mathtt{base}}$ 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 $r_{\mathtt{out}}$ will be $\displaystyle p_{\mathtt{out}}(i) = \frac{1}{r_{\mathtt{out}}}$. Likewise, assume a uniform distribution on $r_{\mathtt{base}}$ for the underlying source of random numbers, i.e. $\displaystyle p_{\mathtt{base}}(i) = \frac{1}{r_{\mathtt{base}}}$. Let $p_{\mathtt{out\_s}}(i)$ denote the random distribution generated by uniform_smallint. Then the sum over all i in $r_{\mathtt{out}}$ of $\displaystyle \left(\frac{p_{\mathtt{out\_s}}(i)}{p_{\mathtt{out}}(i)} - 1\right)^2$ shall not exceed $\displaystyle \frac{r_{\mathtt{out}}}{r_{\mathtt{base}}^2} (r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}) (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})$.

The template parameter IntType shall denote an integer-like value type.

The property above is the square sum of the relative differences in probabilities between the desired uniform distribution $p_{\mathtt{out}}(i)$ and the generated distribution $p_{\mathtt{out\_s}}(i)$. The property can be fulfilled with the calculation $(\mbox{base\_rng} \mbox{ mod } r_{\mathtt{out}})$, as follows: Let $r = r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}$. The base distribution on $r_{\mathtt{base}}$ is folded onto the range $r_{\mathtt{out}}$. The numbers i < r have assigned $\displaystyle \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor+1$ numbers of the base distribution, the rest has only $\displaystyle \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor$. Therefore, $\displaystyle p_{\mathtt{out\_s}}(i) = \left(\left\lfloor\frac{r_{\mathtt{base}}} {r_{\mathtt{out}}}\right\rfloor+1\right) / r_{\mathtt{base}}$ for i < r and $\displaystyle p_{\mathtt{out\_s}}(i) = \left\lfloor\frac{r_{\mathtt{base}}} {r_{\mathtt{out}}}\right\rfloor/r_{\mathtt{base}}$ otherwise. Substituting this in the above sum formula leads to the desired result.

Note: The upper bound for $(r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}) (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})$ is $\displaystyle \frac{r_{\mathtt{out}}^2}{4}$. Regarding the upper bound for the square sum of the relative quantization error of $\displaystyle \frac{r_\mathtt{out}^3}{4r_{\mathtt{base}}^2}$, it seems wise to either choose $r_{\mathtt{base}}$ so that $r_{\mathtt{base}} > 10r_{\mathtt{out}}^2$ or ensure that $r_{\mathtt{base}}$ is divisible by $r_{\mathtt{out}}$.

Member Typedef Documentation

template<class IntType = int>
typedef IntType boost::random::uniform_smallint< IntType >::input_type
template<class IntType = int>
typedef IntType boost::random::uniform_smallint< IntType >::result_type

Constructor & Destructor Documentation

template<class IntType = int>
boost::random::uniform_smallint< IntType >::uniform_smallint ( IntType  min_arg = 0,
IntType  max_arg = 9 
)
inlineexplicit

Constructs a uniform_smallint.

min and max are the lower and upper bounds of the output range, respectively.

Referenced by boost::random::uniform_smallint< IntType >::operator()().

template<class IntType = int>
boost::random::uniform_smallint< IntType >::uniform_smallint ( const param_type parm)
inlineexplicit

Constructs a uniform_smallint from its parameters.

Member Function Documentation

template<class IntType = int>
result_type boost::random::uniform_smallint< IntType >::a ( ) const
inline

Returns the minimum value of the distribution.

template<class IntType = int>
result_type boost::random::uniform_smallint< IntType >::b ( ) const
inline

Returns the maximum value of the distribution.

template<class IntType = int>
result_type min boost::random::uniform_smallint< IntType >::BOOST_PREVENT_MACRO_SUBSTITUTION ( ) const
inline

Returns the minimum value of the distribution.

template<class IntType = int>
result_type max boost::random::uniform_smallint< IntType >::BOOST_PREVENT_MACRO_SUBSTITUTION ( ) const
inline

Returns the maximum value of the distribution.

template<class IntType = int>
boost::random::uniform_smallint< IntType >::BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR ( uniform_smallint< IntType >  ,
lhs  ,
rhs   
)
inline

Returns true if the two distributions will produce identical sequences of values given equal generators.

template<class IntType = int>
boost::random::uniform_smallint< IntType >::BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR ( is  ,
uniform_smallint< IntType >  ,
ud   
)
inline

Reads the distribution from a std::istream.

template<class IntType = int>
boost::random::uniform_smallint< IntType >::BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR ( os  ,
uniform_smallint< IntType >  ,
ud   
)
inline

Writes the distribution to a std::ostream.

template<class IntType = int>
template<class Engine >
result_type boost::random::uniform_smallint< IntType >::operator() ( Engine &  eng) const
inline

Returns a value uniformly distributed in the range [min(), max()].

References boost::range::generate().

template<class IntType = int>
template<class Engine >
result_type boost::random::uniform_smallint< IntType >::operator() ( Engine &  eng,
const param_type parm 
) const
inline

Returns a value uniformly distributed in the range [param.a(), param.b()].

References boost::random::uniform_smallint< IntType >::uniform_smallint().

template<class IntType = int>
param_type boost::random::uniform_smallint< IntType >::param ( ) const
inline

Returns the parameters of the distribution.

References boost::random::uniform_smallint< IntType >::param_type::param_type().

template<class IntType = int>
void boost::random::uniform_smallint< IntType >::param ( const param_type parm)
inline
template<class IntType = int>
void boost::random::uniform_smallint< IntType >::reset ( void  )
inline

Effects: Subsequent uses of the distribution do not depend on values produced by any engine prior to invoking reset.


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