1 /* boost random/geometric_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: geometric_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
19 #include <boost/config/no_tr1/cmath.hpp> // std::log
22 #include <boost/random/detail/config.hpp>
23 #include <boost/random/uniform_01.hpp>
27 #if defined(__GNUC__) && (__GNUC__ < 3)
28 // Special gcc workaround: gcc 2.95.x ignores using-declarations
29 // in template classes (confirmed by gcc author Martin v. Loewis)
34 * An instantiation of the class template @c geometric_distribution models
35 * a \random_distribution. The distribution produces positive
36 * integers which are the number of bernoulli trials
37 * with probability @c p required to get one that fails.
39 * For the geometric distribution, \f$p(i) = (1-p) p^{i-1}\f$.
41 template<class IntType = int, class RealType = double>
42 class geometric_distribution
45 typedef RealType input_type;
46 typedef IntType result_type;
49 * Contructs a new geometric_distribution with the paramter @c p.
53 explicit geometric_distribution(const RealType& p = RealType(0.5))
56 assert(RealType(0) < _p && _p < RealType(1));
60 // compiler-generated copy ctor and assignment operator are fine
63 * Returns: the distribution parameter @c p
65 RealType p() const { return _p; }
68 template<class Engine>
69 result_type operator()(Engine& eng)
71 #ifndef BOOST_NO_STDC_NAMESPACE
75 return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
78 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
79 template<class CharT, class Traits>
80 friend std::basic_ostream<CharT,Traits>&
81 operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
87 template<class CharT, class Traits>
88 friend std::basic_istream<CharT,Traits>&
89 operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
91 is >> std::ws >> gd._p;
99 /// \cond hide_private_functions
103 #ifndef BOOST_NO_STDC_NAMESPACE
117 #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP