RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
ExponentialDistribution.hpp
Go to the documentation of this file.
1 /**
2  * \file ExponentialDistribution.hpp
3  * \brief Header for ExponentialDistribution
4  *
5  * Sample from an exponential distribution.
6  *
7  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
8  * under the MIT/X11 License. For more information, see
9  * http://randomlib.sourceforge.net/
10  **********************************************************************/
11 
12 #if !defined(RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP)
13 #define RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP 1
14 
15 #include <cmath>
16 
17 namespace RandomLib {
18  /**
19  * \brief The exponential distribution.
20  *
21  * Sample from the distribution exp(&minus;<i>x</i>/&mu;) for \e x &ge; 0.
22  * This uses the logarithm method, see Knuth, TAOCP, Vol 2, Sec 3.4.1.D.
23  * Example \code
24  #include <RandomLib/ExponentialDistribution.hpp>
25 
26  RandomLib::Random r;
27  std::cout << "Seed set to " << r.SeedString() << "\n";
28  RandomLib::ExponentialDistribution<double> expdist;
29  std::cout << "Select from exponential distribution:";
30  for (size_t i = 0; i < 10; ++i)
31  std::cout << " " << expdist(r);
32  std::cout << "\n";
33  \endcode
34  *
35  * @tparam RealType the real type of the results (default double).
36  **********************************************************************/
37  template<typename RealType = double> class ExponentialDistribution {
38  public:
39  /**
40  * The type returned by ExponentialDistribution::operator()(Random&)
41  **********************************************************************/
42  typedef RealType result_type;
43  /**
44  * Return a sample of type RealType from the exponential distribution and
45  * mean &mu;. This uses Random::FloatU() which avoids taking log(0) and
46  * allows rare large values to be returned. If &mu; = 1, minimum returned
47  * value = 0 with prob 1/2<sup><i>p</i></sup>; maximum returned value =
48  * log(2)(\e p + \e e) with prob 1/2<sup><i>p</i> + <i>e</i></sup>. Here
49  * \e p is the precision of real type RealType and \e e is the exponent
50  * range.
51  *
52  * @tparam Random the type of RandomCanonical generator.
53  * @param[in,out] r the RandomCanonical generator.
54  * @param[in] mu the mean value of the exponential distribution (default 1).
55  * @return the random sample.
56  **********************************************************************/
57  template<class Random>
58  RealType operator()(Random& r, RealType mu = RealType(1)) const throw();
59  };
60 
61  template<typename RealType> template<class Random> inline RealType
62  ExponentialDistribution<RealType>::operator()(Random& r, RealType mu) const
63  throw() {
64  return -mu * std::log(r.template FloatU<RealType>());
65  }
66 
67 } // namespace RandomLib
68 
69 #endif // RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP
RealType operator()(Random &r, RealType mu=RealType(1)) const
Generate random integers, reals, and booleans.