[857] | 1 | /* boost random/geometric_distribution.hpp header file
|
---|
| 2 | *
|
---|
| 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)
|
---|
| 7 | *
|
---|
| 8 | * See http://www.boost.org for most recent version including documentation.
|
---|
| 9 | *
|
---|
| 10 | * $Id: geometric_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
|
---|
| 11 | *
|
---|
| 12 | * Revision history
|
---|
| 13 | * 2001-02-18 moved to individual header files
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
---|
| 17 | #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
---|
| 18 |
|
---|
| 19 | #include <cmath> // std::log
|
---|
| 20 | #include <cassert>
|
---|
| 21 | #include <iostream>
|
---|
| 22 | #include <boost/random/uniform_01.hpp>
|
---|
| 23 |
|
---|
| 24 | namespace boost {
|
---|
| 25 |
|
---|
| 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)
|
---|
| 29 | using std::log;
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | // geometric distribution: p(i) = (1-p) * pow(p, i-1) (integer)
|
---|
| 33 | template<class IntType = int, class RealType = double>
|
---|
| 34 | class geometric_distribution
|
---|
| 35 | {
|
---|
| 36 | public:
|
---|
| 37 | typedef RealType input_type;
|
---|
| 38 | typedef IntType result_type;
|
---|
| 39 |
|
---|
| 40 | explicit geometric_distribution(const RealType& p = RealType(0.5))
|
---|
| 41 | : _p(p)
|
---|
| 42 | {
|
---|
| 43 | assert(RealType(0) < p && p < RealType(1));
|
---|
| 44 | init();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | // compiler-generated copy ctor and assignment operator are fine
|
---|
| 48 |
|
---|
| 49 | RealType p() const { return _p; }
|
---|
| 50 | void reset() { }
|
---|
| 51 |
|
---|
| 52 | template<class Engine>
|
---|
| 53 | result_type operator()(Engine& eng)
|
---|
| 54 | {
|
---|
| 55 | #ifndef BOOST_NO_STDC_NAMESPACE
|
---|
| 56 | using std::log;
|
---|
| 57 | using std::floor;
|
---|
| 58 | #endif
|
---|
| 59 | return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 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)
|
---|
| 66 | {
|
---|
| 67 | os << gd._p;
|
---|
| 68 | return os;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | template<class CharT, class Traits>
|
---|
| 72 | friend std::basic_istream<CharT,Traits>&
|
---|
| 73 | operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
|
---|
| 74 | {
|
---|
| 75 | is >> std::ws >> gd._p;
|
---|
| 76 | gd.init();
|
---|
| 77 | return is;
|
---|
| 78 | }
|
---|
| 79 | #endif
|
---|
| 80 |
|
---|
| 81 | private:
|
---|
| 82 | void init()
|
---|
| 83 | {
|
---|
| 84 | #ifndef BOOST_NO_STDC_NAMESPACE
|
---|
| 85 | using std::log;
|
---|
| 86 | #endif
|
---|
| 87 | _log_p = log(_p);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | RealType _p;
|
---|
| 91 | RealType _log_p;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | } // namespace boost
|
---|
| 95 |
|
---|
| 96 | #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
---|
| 97 |
|
---|