00001 /** 00002 * \file ExponentialDistribution.hpp 00003 * \brief Header for ExponentialDistribution 00004 * 00005 * Sample from an exponential distribution. 00006 * 00007 * Written by <a href="http://charles.karney.info/">Charles Karney</a> 00008 * <charles@karney.com> and licensed under the LGPL. For more 00009 * information, see http://charles.karney.info/random/ 00010 **********************************************************************/ 00011 00012 #if !defined(EXPONENTIALDISTRIBUTION_HPP) 00013 #define EXPONENTIALDISTRIBUTION_HPP "$Id: ExponentialDistribution.hpp 6415 2008-01-12 19:12:01Z ckarney $" 00014 00015 #include <cmath> 00016 00017 namespace RandomLib { 00018 /** 00019 * \brief The exponential distribution. 00020 * 00021 * Sample from the distribution exp(-\e x) for \e x >= 0. This uses the 00022 * logarithm method, see Knuth, TAOCP, Vol 2, Sec 3.4.1.D. Example 00023 * \code 00024 * #include "RandomLib/ExponentialDistribution.hpp" 00025 * 00026 * RandomLib::Random r; 00027 * std::cout << "Seed set to " << r.SeedString() << std::endl; 00028 * RandomLib::ExponentialDistribution<double> expdist; 00029 * std::cout << "Select from exponential distribution:"; 00030 * for (size_t i = 0; i < 10; ++i) 00031 * std::cout << " " << expdist(r); 00032 * std::cout << std::endl; 00033 * \endcode 00034 **********************************************************************/ 00035 template<typename RealType = double> class ExponentialDistribution { 00036 public: 00037 /** 00038 * The type returned by ExponentialDistribution::operator()(Random&) 00039 **********************************************************************/ 00040 typedef RealType result_type; 00041 /** 00042 * Return a sample of type RealType from the exponential distribution and 00043 * mean \a mu. This uses Random::FloatU() which avoids taking log(0) and 00044 * allows rare large values to be returned. If \a mu = 1, minimum returned 00045 * value = 0 with prob 1/2<sup><i>p</i></sup>; maximum returned value = 00046 * log(2)(\e p + \e e) with prob 1/2<sup><i>p</i> + <i>e</i></sup>. Here 00047 * \e p is the precision of real type RealType and \e e is the exponent 00048 * range. 00049 **********************************************************************/ 00050 template<class Random> 00051 RealType operator()(Random& r, RealType mu = RealType(1)) const throw(); 00052 }; 00053 00054 template<typename RealType> template<class Random> inline RealType 00055 ExponentialDistribution<RealType>::operator()(Random& r, RealType mu) const 00056 throw() { 00057 return -mu * std::log(r.template FloatU<RealType>()); 00058 } 00059 } // namespace RandomLib 00060 #endif // EXPONENTIALDISTRIBUTION_HPP