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,v 1.16 2004/07/27 03:43:32 dgregor Exp $
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 <cmath> // std::log
22 #include <boost/random/uniform_01.hpp>
26 #if defined(__GNUC__) && (__GNUC__ < 3)
27 // Special gcc workaround: gcc 2.95.x ignores using-declarations
28 // in template classes (confirmed by gcc author Martin v. Loewis)
32 // geometric distribution: p(i) = (1-p) * pow(p, i-1) (integer)
33 template<class IntType = int, class RealType = double>
34 class geometric_distribution
37 typedef RealType input_type;
38 typedef IntType result_type;
40 explicit geometric_distribution(const RealType& p = RealType(0.5))
43 assert(RealType(0) < p && p < RealType(1));
47 // compiler-generated copy ctor and assignment operator are fine
49 RealType p() const { return _p; }
52 template<class Engine>
53 result_type operator()(Engine& eng)
55 #ifndef BOOST_NO_STDC_NAMESPACE
59 return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
62 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
63 template<class CharT, class Traits>
64 friend std::basic_ostream<CharT,Traits>&
65 operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
71 template<class CharT, class Traits>
72 friend std::basic_istream<CharT,Traits>&
73 operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
75 is >> std::ws >> gd._p;
84 #ifndef BOOST_NO_STDC_NAMESPACE
96 #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP