os/ossrv/ossrv_pub/boost_apis/boost/random/bernoulli_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/bernoulli_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
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: bernoulli_distribution.hpp,v 1.19 2004/07/27 03:43:32 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
sl@0
    17
#define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
sl@0
    18
sl@0
    19
#include <cassert>
sl@0
    20
#include <iostream>
sl@0
    21
sl@0
    22
namespace boost {
sl@0
    23
sl@0
    24
// Bernoulli distribution: p(true) = p, p(false) = 1-p   (boolean)
sl@0
    25
template<class RealType = double>
sl@0
    26
class bernoulli_distribution
sl@0
    27
{
sl@0
    28
public:
sl@0
    29
  // In principle, this could work with both integer and floating-point
sl@0
    30
  // types.  Generating floating-point random numbers in the first
sl@0
    31
  // place is probably more expensive, so use integer as input.
sl@0
    32
  typedef int input_type;
sl@0
    33
  typedef bool result_type;
sl@0
    34
sl@0
    35
  explicit bernoulli_distribution(const RealType& p = RealType(0.5)) 
sl@0
    36
    : _p(p)
sl@0
    37
  {
sl@0
    38
    assert(p >= 0);
sl@0
    39
    assert(p <= 1);
sl@0
    40
  }
sl@0
    41
sl@0
    42
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    43
sl@0
    44
  RealType p() const { return _p; }
sl@0
    45
  void reset() { }
sl@0
    46
sl@0
    47
  template<class Engine>
sl@0
    48
  result_type operator()(Engine& eng)
sl@0
    49
  {
sl@0
    50
    if(_p == RealType(0))
sl@0
    51
      return false;
sl@0
    52
    else
sl@0
    53
      return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
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 bernoulli_distribution& bd)
sl@0
    60
  {
sl@0
    61
    os << bd._p;
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, bernoulli_distribution& bd)
sl@0
    68
  {
sl@0
    69
    is >> std::ws >> bd._p;
sl@0
    70
    return is;
sl@0
    71
  }
sl@0
    72
#endif
sl@0
    73
sl@0
    74
private:
sl@0
    75
  RealType _p;
sl@0
    76
};
sl@0
    77
sl@0
    78
} // namespace boost
sl@0
    79
sl@0
    80
#endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP