Random selection from a discrete set. More...
#include <RandomLib/RandomSelect.hpp>
Public Member Functions | |
RandomSelect () throw (std::bad_alloc) | |
template<typename WeightType > | |
RandomSelect (const std::vector< WeightType > &w) throw (std::out_of_range, std::bad_alloc) | |
template<typename InputIterator > | |
RandomSelect (InputIterator a, InputIterator b) throw (std::out_of_range, std::bad_alloc) | |
void | Init () throw () |
template<typename WeightType > | |
void | Init (const std::vector< WeightType > &w) throw (std::out_of_range, std::bad_alloc) |
template<typename InputIterator > | |
void | Init (InputIterator a, InputIterator b) throw (std::out_of_range, std::bad_alloc) |
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 () |
Private Attributes | |
unsigned | _k |
std::vector< NumericType > | _Q |
std::vector< unsigned > | _Y |
NumericType | _wsum |
NumericType | _wmax |
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:
#include "RandomLib/RandomSelect.hpp" // 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::RandomSelect<unsigned> sel(w, w + 13); RandomLib::Random r; // Initialize random numbers std::cout << "Seed set to " << r.SeedString() << std::endl; std::cout << "Throw a pair of dice 100 times:"; for (unsigned i = 0; i < 100; ++i) std::cout << " " << sel(r); std::cout << std::endl;
Definition at line 68 of file RandomSelect.hpp.
RandomLib::RandomSelect< NumericType >::RandomSelect | ( | ) | throw (std::bad_alloc) [inline] |
Initialize in a cleared state (equivalent to having a single choice).
Definition at line 74 of file RandomSelect.hpp.
RandomLib::RandomSelect< NumericType >::RandomSelect | ( | const std::vector< WeightType > & | w | ) | throw (std::out_of_range, std::bad_alloc) [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:
Definition at line 88 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::Init().
RandomLib::RandomSelect< NumericType >::RandomSelect | ( | InputIterator | a, | |
InputIterator | b | |||
) | throw (std::out_of_range, std::bad_alloc) [inline] |
Initialize with a weight given by a pair of iterators [a, b).
Definition at line 205 of file RandomSelect.hpp.
References STATIC_ASSERT.
void RandomLib::RandomSelect< NumericType >::Init | ( | ) | throw () [inline] |
Clear the state (equivalent to having a single choice).
Definition at line 102 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_k, RandomLib::RandomSelect< NumericType >::_Q, RandomLib::RandomSelect< NumericType >::_wmax, RandomLib::RandomSelect< NumericType >::_wsum, and RandomLib::RandomSelect< NumericType >::_Y.
Referenced by RandomLib::RandomSelect< NumericType >::RandomSelect().
void RandomLib::RandomSelect< NumericType >::Init | ( | const std::vector< WeightType > & | w | ) | throw (std::out_of_range, std::bad_alloc) [inline] |
Re-initialize with a weight vector w. Leave state unaltered in the case of an error.
Definition at line 110 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::Init().
Referenced by RandomLib::RandomSelect< NumericType >::Init().
void RandomLib::RandomSelect< NumericType >::Init | ( | InputIterator | a, | |
InputIterator | b | |||
) | throw (std::out_of_range, std::bad_alloc) [inline] |
Re-initialize with a weight given as a pair of iterators [a, b). Leave state unaltered in the case of an error.
Definition at line 118 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_k, RandomLib::RandomSelect< NumericType >::_Q, and RandomLib::RandomSelect< NumericType >::_Y.
unsigned RandomLib::RandomSelect< NumericType >::operator() | ( | Random & | r | ) | const throw () [inline] |
Return an index into the weight vector with probability proportional to the weight.
Definition at line 131 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_k, RandomLib::RandomSelect< NumericType >::_Q, RandomLib::RandomSelect< NumericType >::_wsum, and RandomLib::RandomSelect< NumericType >::_Y.
NumericType RandomLib::RandomSelect< NumericType >::TotalWeight | ( | ) | const throw () [inline] |
Return the sum of the weights.
Definition at line 147 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_wsum.
NumericType RandomLib::RandomSelect< NumericType >::MaxWeight | ( | ) | const throw () [inline] |
Return the maximum weight.
Definition at line 152 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_wmax.
NumericType RandomLib::RandomSelect< NumericType >::Weight | ( | unsigned | i | ) | const throw () [inline] |
Return the weight for sample i. Weight(i) / TotalWeight() gives the probability of sample i.
Definition at line 158 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_k, RandomLib::RandomSelect< NumericType >::_Q, RandomLib::RandomSelect< NumericType >::_wsum, and RandomLib::RandomSelect< NumericType >::_Y.
unsigned RandomLib::RandomSelect< NumericType >::Choices | ( | ) | const throw () [inline] |
Return the number of choices, i.e., the length of the weight vector.
Definition at line 177 of file RandomSelect.hpp.
References RandomLib::RandomSelect< NumericType >::_k.
unsigned RandomLib::RandomSelect< NumericType >::_k [private] |
Size of weight vector
Definition at line 184 of file RandomSelect.hpp.
Referenced by RandomLib::RandomSelect< NumericType >::Choices(), RandomLib::RandomSelect< NumericType >::Init(), RandomLib::RandomSelect< NumericType >::operator()(), and RandomLib::RandomSelect< NumericType >::Weight().
std::vector<NumericType> RandomLib::RandomSelect< NumericType >::_Q [private] |
Vector of cutoffs
Definition at line 188 of file RandomSelect.hpp.
Referenced by RandomLib::RandomSelect< NumericType >::Init(), RandomLib::RandomSelect< NumericType >::operator()(), and RandomLib::RandomSelect< NumericType >::Weight().
std::vector<unsigned> RandomLib::RandomSelect< NumericType >::_Y [private] |
Vector of aliases
Definition at line 192 of file RandomSelect.hpp.
Referenced by RandomLib::RandomSelect< NumericType >::Init(), RandomLib::RandomSelect< NumericType >::operator()(), and RandomLib::RandomSelect< NumericType >::Weight().
NumericType RandomLib::RandomSelect< NumericType >::_wsum [private] |
The sum of the weights
Definition at line 196 of file RandomSelect.hpp.
Referenced by RandomLib::RandomSelect< NumericType >::Init(), RandomLib::RandomSelect< NumericType >::operator()(), RandomLib::RandomSelect< NumericType >::TotalWeight(), and RandomLib::RandomSelect< NumericType >::Weight().
NumericType RandomLib::RandomSelect< NumericType >::_wmax [private] |
The maximum weight
Definition at line 200 of file RandomSelect.hpp.
Referenced by RandomLib::RandomSelect< NumericType >::Init(), and RandomLib::RandomSelect< NumericType >::MaxWeight().