sl@0: /* boost random/geometric_distribution.hpp header file sl@0: * sl@0: * Copyright Jens Maurer 2000-2001 sl@0: * Distributed under the Boost Software License, Version 1.0. (See sl@0: * accompanying file LICENSE_1_0.txt or copy at sl@0: * http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: * See http://www.boost.org for most recent version including documentation. sl@0: * sl@0: * $Id: geometric_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $ sl@0: * sl@0: * Revision history sl@0: * 2001-02-18 moved to individual header files sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP sl@0: #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP sl@0: sl@0: #include // std::log sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: #if defined(__GNUC__) && (__GNUC__ < 3) sl@0: // Special gcc workaround: gcc 2.95.x ignores using-declarations sl@0: // in template classes (confirmed by gcc author Martin v. Loewis) sl@0: using std::log; sl@0: #endif sl@0: sl@0: // geometric distribution: p(i) = (1-p) * pow(p, i-1) (integer) sl@0: template sl@0: class geometric_distribution sl@0: { sl@0: public: sl@0: typedef RealType input_type; sl@0: typedef IntType result_type; sl@0: sl@0: explicit geometric_distribution(const RealType& p = RealType(0.5)) sl@0: : _p(p) sl@0: { sl@0: assert(RealType(0) < p && p < RealType(1)); sl@0: init(); sl@0: } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: sl@0: RealType p() const { return _p; } sl@0: void reset() { } sl@0: sl@0: template sl@0: result_type operator()(Engine& eng) sl@0: { sl@0: #ifndef BOOST_NO_STDC_NAMESPACE sl@0: using std::log; sl@0: using std::floor; sl@0: #endif sl@0: return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1); sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, const geometric_distribution& gd) sl@0: { sl@0: os << gd._p; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, geometric_distribution& gd) sl@0: { sl@0: is >> std::ws >> gd._p; sl@0: gd.init(); sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: private: sl@0: void init() sl@0: { sl@0: #ifndef BOOST_NO_STDC_NAMESPACE sl@0: using std::log; sl@0: #endif sl@0: _log_p = log(_p); sl@0: } sl@0: sl@0: RealType _p; sl@0: RealType _log_p; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP sl@0: