RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Public Member Functions | List of all members
RandomLib::RandomSelect< NumericType > Class Template Reference

Random selection from a discrete set. More...

#include <RandomLib/RandomSelect.hpp>

Public Member Functions

 RandomSelect ()
 
template<typename WeightType >
 RandomSelect (const std::vector< WeightType > &w)
 
template<typename InputIterator >
 RandomSelect (InputIterator a, InputIterator b)
 
void Init () throw ()
 
template<typename WeightType >
void Init (const std::vector< WeightType > &w)
 
template<typename InputIterator >
void Init (InputIterator a, InputIterator b)
 
template<class Random >
unsigned operator() (Random &r) const throw ()
 
NumericType TotalWeight () const throw ()
 
NumericType MaxWeight () const throw ()
 
NumericType Weight (unsigned i) const throw ()
 
unsigned Choices () const throw ()
 

Detailed Description

template<typename NumericType = double>
class RandomLib::RandomSelect< NumericType >

Random selection from a discrete set.

An implementation of Walker algorithm for selecting from a finite set (following Knuth, TAOCP, Vol 2, Sec 3.4.1.A). This provides a rapid way of selecting one of several choices depending on a discrete set weights. Original citation is
A. J. Walker,
An Efficient Method for Generating Discrete Random Variables and General Distributions,
ACM TOMS 3, 253–256 (1977).

There are two changes here in the setup algorithm as given by Knuth:

Example:

// Weights for throwing a pair of dice
unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
// Initialize selection
RandomLib::Random r; // Initialize random numbers
std::cout << "Seed set to " << r.SeedString() << "\n";
std::cout << "Throw a pair of dice 100 times:";
for (unsigned i = 0; i < 100; ++i)
std::cout << " " << sel(r);
std::cout << "\n";
Template Parameters
NumericTypethe numeric type to use (default double).

Definition at line 69 of file RandomSelect.hpp.

Constructor & Destructor Documentation

template<typename NumericType = double>
RandomLib::RandomSelect< NumericType >::RandomSelect ( )
inline

Initialize in a cleared state (equivalent to having a single choice).

Definition at line 75 of file RandomSelect.hpp.

template<typename NumericType = double>
template<typename WeightType >
RandomLib::RandomSelect< NumericType >::RandomSelect ( const std::vector< WeightType > &  w)
inline

Initialize with a weight vector w of elements of type WeightType. Internal calculations are carried out with type NumericType. NumericType needs to allow Choices() * MaxWeight() to be represented. Sensible combinations are:

  • WeightType integer, NumericType integer with digits(NumericType) ≥ digits(WeightType)
  • WeightType integer, NumericType real
  • WeightType real, NumericType real with digits(NumericType) ≥ digits(WeightType)
Template Parameters
WeightTypethe type of the weights.
Parameters
[in]wthe vector of weights.
Exceptions
RandomErrif any of the weights are negative or if the total weight is not positive.

Definition at line 94 of file RandomSelect.hpp.

References RandomLib::RandomSelect< NumericType >::Init().

template<typename NumericType >
template<typename InputIterator >
RandomLib::RandomSelect< NumericType >::RandomSelect ( InputIterator  a,
InputIterator  b 
)

Initialize with a weight given by a pair of iterators [a, b).

Template Parameters
InputIteratorthe type of the iterator.
Parameters
[in]athe beginning iterator.
[in]bthe ending iterator.
Exceptions
RandomErrif any of the weights are negative or if the total weight is not positive.

Definition at line 223 of file RandomSelect.hpp.

References STATIC_ASSERT.

Member Function Documentation

template<typename NumericType = double>
void RandomLib::RandomSelect< NumericType >::Init ( )
throw (
)
inline

Clear the state (equivalent to having a single choice).

Definition at line 111 of file RandomSelect.hpp.

Referenced by coverage32(), coverage64(), and RandomLib::RandomSelect< NumericType >::RandomSelect().

template<typename NumericType = double>
template<typename WeightType >
void RandomLib::RandomSelect< NumericType >::Init ( const std::vector< WeightType > &  w)
inline

Re-initialize with a weight vector w. Leave state unaltered in the case of an error.

Template Parameters
WeightTypethe type of the weights.
Parameters
[in]wthe vector of weights.

Definition at line 122 of file RandomSelect.hpp.

References RandomLib::RandomSelect< NumericType >::Init().

Referenced by RandomLib::RandomSelect< NumericType >::Init().

template<typename NumericType = double>
template<typename InputIterator >
void RandomLib::RandomSelect< NumericType >::Init ( InputIterator  a,
InputIterator  b 
)
inline

Re-initialize with a weight given as a pair of iterators [a, b). Leave state unaltered in the case of an error.

Template Parameters
InputIteratorthe type of the iterator.
Parameters
[in]athe beginning iterator.
[in]bthe ending iterator.

Definition at line 133 of file RandomSelect.hpp.

template<typename NumericType = double>
template<class Random >
unsigned RandomLib::RandomSelect< NumericType >::operator() ( Random r) const
throw (
)
inline

Return an index into the weight vector with probability proportional to the weight.

Template Parameters
Randomthe type of RandomCanonical generator.
Parameters
[in,out]rthe RandomCanonical generator.
Returns
the random index into the weight vector.

Definition at line 149 of file RandomSelect.hpp.

template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::TotalWeight ( ) const
throw (
)
inline
Returns
the sum of the weights.

Definition at line 164 of file RandomSelect.hpp.

template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::MaxWeight ( ) const
throw (
)
inline
Returns
the maximum weight.

Definition at line 169 of file RandomSelect.hpp.

template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::Weight ( unsigned  i) const
throw (
)
inline
Parameters
[in]ithe index in to the weight vector.
Returns
the weight for sample i. Weight(i) / TotalWeight() gives the probability of sample i.

Definition at line 176 of file RandomSelect.hpp.

template<typename NumericType = double>
unsigned RandomLib::RandomSelect< NumericType >::Choices ( ) const
throw (
)
inline
Returns
the number of choices, i.e., the length of the weight vector.

Definition at line 195 of file RandomSelect.hpp.


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