Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
RandomLib::ExponentialProb Class Reference

The exponential probability. More...

#include <RandomLib-2010-01/ExponentialProb.hpp>

List of all members.

Public Member Functions

 ExponentialProb ()
template<typename RealType , class Random >
bool operator() (Random &r, RealType p) const throw (std::bad_alloc)

Private Types

typedef unsigned word

Private Member Functions

template<typename RealType , class Random >
bool ExpFraction (Random &r, RealType p) const throw (std::bad_alloc)

Private Attributes

std::vector< word_v

Static Private Attributes

static const unsigned alloc_incr = 16

Detailed Description

The exponential probability.

Return true with probability exp(-p). Basic method taken from:
J. von Neumann,
Various Techniques used in Connection with Random Digits,
J. Res. Nat. Bur. Stand., Appl. Math. Ser. 12, 36-38 (1951),
reprinted in Collected Works, Vol. 5, 768-770 (Pergammon, 1963).
See also the references given for the ExactExponential class.

Here the method is extended to be exact by generating sufficient bits in the random numbers in the algorithm to allow the unambiguous comparisons to be made.

Here's one way of sampling from a normal distribution with zero mean and unit variance in the interval [-1,1] with reasonable accuracy:

 #include "RandomLib/Random.hpp"
 #include "RandomLib/ExponentialProb.hpp"

 double Normal(RandomLib::Random& r) {
   double x;
   RandomLib::ExponentialProb e;
   do
      x = r.FloatW();
   while ( !e(r, - 0.5 * x * x) );
   return x;
 }

Member Typedef Documentation

typedef unsigned RandomLib::ExponentialProb::word [private]

Definition at line 49 of file ExponentialProb.hpp.


Constructor & Destructor Documentation

RandomLib::ExponentialProb::ExponentialProb ( ) [inline]

Definition at line 52 of file ExponentialProb.hpp.


Member Function Documentation

template<typename RealType , class Random >
bool RandomLib::ExponentialProb::operator() ( Random r,
RealType  p 
) const throw (std::bad_alloc) [inline]

Return true with probability exp(-p). Returns false if p <= 0. For in p (0,1], it requires about exp(p) random deviates. For p large, it requires about exp(1)/(1 - exp(-1)) random deviates.

Definition at line 78 of file ExponentialProb.hpp.

template<typename RealType , class Random >
bool RandomLib::ExponentialProb::ExpFraction ( Random r,
RealType  p 
) const throw (std::bad_alloc) [inline, private]

Return true with probability exp(-p) for p in [0,1].

Definition at line 89 of file ExponentialProb.hpp.


Member Data Documentation

std::vector<word> RandomLib::ExponentialProb::_v [mutable, private]

Holds as much of intermediate uniform deviates as needed.

Definition at line 70 of file ExponentialProb.hpp.

const unsigned RandomLib::ExponentialProb::alloc_incr = 16 [static, private]

Increment on size of _v.

Definition at line 74 of file ExponentialProb.hpp.


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