os/ossrv/ossrv_pub/boost_apis/boost/random/binomial_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/binomial_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2002
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: binomial_distribution.hpp,v 1.10 2005/06/24 22:13:43 jmaurer Exp $
sl@0
    11
 *
sl@0
    12
 */
sl@0
    13
sl@0
    14
#ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
sl@0
    15
#define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP
sl@0
    16
sl@0
    17
#include <cmath>
sl@0
    18
#include <cassert>
sl@0
    19
#include <boost/random/bernoulli_distribution.hpp>
sl@0
    20
sl@0
    21
namespace boost {
sl@0
    22
sl@0
    23
// Knuth
sl@0
    24
template<class IntType = int, class RealType = double>
sl@0
    25
class binomial_distribution
sl@0
    26
{
sl@0
    27
public:
sl@0
    28
  typedef typename bernoulli_distribution<RealType>::input_type input_type;
sl@0
    29
  typedef IntType result_type;
sl@0
    30
sl@0
    31
  explicit binomial_distribution(IntType t = 1,
sl@0
    32
                                 const RealType& p = RealType(0.5))
sl@0
    33
    : _bernoulli(p), _t(t)
sl@0
    34
  {
sl@0
    35
    assert(t >= 0);
sl@0
    36
    assert(RealType(0) <= 0 && p <= RealType(1));
sl@0
    37
  }
sl@0
    38
sl@0
    39
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    40
sl@0
    41
  IntType t() const { return _t; }
sl@0
    42
  RealType p() const { return _bernoulli.p(); }
sl@0
    43
  void reset() { }
sl@0
    44
sl@0
    45
  template<class Engine>
sl@0
    46
  result_type operator()(Engine& eng)
sl@0
    47
  {
sl@0
    48
    // TODO: This is O(_t), but it should be O(log(_t)) for large _t
sl@0
    49
    result_type n = 0;
sl@0
    50
    for(IntType i = 0; i < _t; ++i)
sl@0
    51
      if(_bernoulli(eng))
sl@0
    52
        ++n;
sl@0
    53
    return n;
sl@0
    54
  }
sl@0
    55
sl@0
    56
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    57
  template<class CharT, class Traits>
sl@0
    58
  friend std::basic_ostream<CharT,Traits>&
sl@0
    59
  operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd)
sl@0
    60
  {
sl@0
    61
    os << bd._bernoulli << " " << bd._t;
sl@0
    62
    return os;
sl@0
    63
  }
sl@0
    64
sl@0
    65
  template<class CharT, class Traits>
sl@0
    66
  friend std::basic_istream<CharT,Traits>&
sl@0
    67
  operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)
sl@0
    68
  {
sl@0
    69
    is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;
sl@0
    70
    return is;
sl@0
    71
  }
sl@0
    72
#endif
sl@0
    73
sl@0
    74
private:
sl@0
    75
  bernoulli_distribution<RealType> _bernoulli;
sl@0
    76
  IntType _t;
sl@0
    77
};
sl@0
    78
sl@0
    79
} // namespace boost
sl@0
    80
sl@0
    81
#endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP