RandomLib::RandomSelect< NumericType > Class Template Reference

Random selection from a discrete set. More...

#include <RandomLib/RandomSelect.hpp>

List of all members.

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

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:

   #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.


Constructor & Destructor Documentation

template<typename NumericType = double>
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.

template<typename NumericType = double>
template<typename WeightType >
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:

  • WeightType integer, NumericType integer with digits(NumericType) >= digits(WeightType)
  • WeightType integer, NumericType real
  • WeightType real, NumericType real with digits(NumericType) >= digits(WeightType)

Definition at line 88 of file RandomSelect.hpp.

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

template<typename NumericType >
template<typename InputIterator >
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.


Member Function Documentation

template<typename NumericType = double>
void RandomLib::RandomSelect< NumericType >::Init (  )  throw () [inline]
template<typename NumericType = double>
template<typename WeightType >
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().

template<typename NumericType = double>
template<typename InputIterator >
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.

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.

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.

template<typename NumericType = double>
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.

template<typename NumericType = double>
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.

template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::Weight ( unsigned  i  )  const throw () [inline]
template<typename NumericType = double>
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.


Member Data Documentation

template<typename NumericType = double>
unsigned RandomLib::RandomSelect< NumericType >::_k [private]
template<typename NumericType = double>
std::vector<NumericType> RandomLib::RandomSelect< NumericType >::_Q [private]
template<typename NumericType = double>
std::vector<unsigned> RandomLib::RandomSelect< NumericType >::_Y [private]
template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::_wsum [private]
template<typename NumericType = double>
NumericType RandomLib::RandomSelect< NumericType >::_wmax [private]

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

Generated on Thu Dec 31 15:24:30 2009 by  doxygen 1.6.1