Random selection from a discrete set. More...
#include <RandomLib-2010-01/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;
RandomLib::RandomSelect::RandomSelect | ( | ) | throw (std::bad_alloc) [inline] |
Initialize in a cleared state (equivalent to having a single choice).
Definition at line 73 of file RandomSelect.hpp.
RandomLib::RandomSelect::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 87 of file RandomSelect.hpp.
References Init().
RandomLib::RandomSelect::RandomSelect | ( | InputIterator | a, |
InputIterator | b | ||
) | throw (std::out_of_range, std::bad_alloc) |
Initialize with a weight given by a pair of iterators [a, b).
Definition at line 204 of file RandomSelect.hpp.
References STATIC_ASSERT.
void RandomLib::RandomSelect::Init | ( | ) | throw () [inline] |
Clear the state (equivalent to having a single choice).
Definition at line 101 of file RandomSelect.hpp.
References _k, _wsum, _wmax, _Q, and _Y.
Referenced by RandomSelect().
void RandomLib::RandomSelect::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 109 of file RandomSelect.hpp.
References Init().
Referenced by Init().
void RandomLib::RandomSelect::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 117 of file RandomSelect.hpp.
unsigned RandomLib::RandomSelect::operator() | ( | Random & | r | ) | const throw () [inline] |
NumericType RandomLib::RandomSelect::TotalWeight | ( | ) | const throw () [inline] |
NumericType RandomLib::RandomSelect::MaxWeight | ( | ) | const throw () [inline] |
NumericType RandomLib::RandomSelect::Weight | ( | unsigned | i | ) | const throw () [inline] |
Return the weight for sample i. Weight(i) / TotalWeight() gives the probability of sample i.
Definition at line 157 of file RandomSelect.hpp.
unsigned RandomLib::RandomSelect::Choices | ( | ) | const throw () [inline] |
Return the number of choices, i.e., the length of the weight vector.
Definition at line 176 of file RandomSelect.hpp.
References _k.
unsigned RandomLib::RandomSelect::_k [private] |
Size of weight vector
Definition at line 183 of file RandomSelect.hpp.
Referenced by Init(), operator()(), Weight(), and Choices().
std::vector<NumericType> RandomLib::RandomSelect::_Q [private] |
Vector of cutoffs
Definition at line 187 of file RandomSelect.hpp.
Referenced by Init(), operator()(), and Weight().
std::vector<unsigned> RandomLib::RandomSelect::_Y [private] |
Vector of aliases
Definition at line 191 of file RandomSelect.hpp.
Referenced by Init(), operator()(), and Weight().
NumericType RandomLib::RandomSelect::_wsum [private] |
The sum of the weights
Definition at line 195 of file RandomSelect.hpp.
Referenced by Init(), operator()(), TotalWeight(), and Weight().
NumericType RandomLib::RandomSelect::_wmax [private] |
The maximum weight
Definition at line 199 of file RandomSelect.hpp.
Referenced by Init(), and MaxWeight().