/** * \file ExponentialDistribution.hpp * \brief Header for ExponentialDistribution * * Sample from an exponential distribution. * * Copyright (c) Charles Karney (2006-2011) and licensed * under the MIT/X11 License. For more information, see * http://randomlib.sourceforge.net/ **********************************************************************/ #if !defined(RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP) #define RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP 1 #include namespace RandomLib { /** * \brief The exponential distribution. * * Sample from the distribution exp(−x/μ) for \e x ≥ 0. * This uses the logarithm method, see Knuth, TAOCP, Vol 2, Sec 3.4.1.D. * Example \code #include RandomLib::Random r; std::cout << "Seed set to " << r.SeedString() << "\n"; RandomLib::ExponentialDistribution expdist; std::cout << "Select from exponential distribution:"; for (size_t i = 0; i < 10; ++i) std::cout << " " << expdist(r); std::cout << "\n"; \endcode * * @tparam RealType the real type of the results (default double). **********************************************************************/ template class ExponentialDistribution { public: /** * The type returned by ExponentialDistribution::operator()(Random&) **********************************************************************/ typedef RealType result_type; /** * Return a sample of type RealType from the exponential distribution and * mean μ. This uses Random::FloatU() which avoids taking log(0) and * allows rare large values to be returned. If μ = 1, minimum returned * value = 0 with prob 1/2p; maximum returned value = * log(2)(\e p + \e e) with prob 1/2p + e. Here * \e p is the precision of real type RealType and \e e is the exponent * range. * * @tparam Random the type of RandomCanonical generator. * @param[in,out] r the RandomCanonical generator. * @param[in] mu the mean value of the exponential distribution (default 1). * @return the random sample. **********************************************************************/ template RealType operator()(Random& r, RealType mu = RealType(1)) const throw(); }; template template inline RealType ExponentialDistribution::operator()(Random& r, RealType mu) const throw() { return -mu * std::log(r.template FloatU()); } } // namespace RandomLib #endif // RANDOMLIB_EXPONENTIALDISTRIBUTION_HPP