1 /* boost random/geometric_distribution.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: geometric_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
\r
17 #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
\r
19 #include <boost/config/no_tr1/cmath.hpp> // std::log
\r
22 #include <boost/random/detail/config.hpp>
\r
23 #include <boost/random/uniform_01.hpp>
\r
27 #if defined(__GNUC__) && (__GNUC__ < 3)
\r
28 // Special gcc workaround: gcc 2.95.x ignores using-declarations
\r
29 // in template classes (confirmed by gcc author Martin v. Loewis)
\r
33 // geometric distribution: p(i) = (1-p) * pow(p, i-1) (integer)
\r
34 template<class IntType = int, class RealType = double>
\r
35 class geometric_distribution
\r
38 typedef RealType input_type;
\r
39 typedef IntType result_type;
\r
41 explicit geometric_distribution(const RealType& p_arg = RealType(0.5))
\r
44 assert(RealType(0) < _p && _p < RealType(1));
\r
48 // compiler-generated copy ctor and assignment operator are fine
\r
50 RealType p() const { return _p; }
\r
53 template<class Engine>
\r
54 result_type operator()(Engine& eng)
\r
56 #ifndef BOOST_NO_STDC_NAMESPACE
\r
60 return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
\r
63 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
64 template<class CharT, class Traits>
\r
65 friend std::basic_ostream<CharT,Traits>&
\r
66 operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
\r
72 template<class CharT, class Traits>
\r
73 friend std::basic_istream<CharT,Traits>&
\r
74 operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
\r
76 is >> std::ws >> gd._p;
\r
85 #ifndef BOOST_NO_STDC_NAMESPACE
\r
95 } // namespace boost
\r
97 #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
\r