1 /* boost random/bernoulli_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: bernoulli_distribution.hpp,v 1.19 2004/07/27 03:43:32 dgregor Exp $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
24 // Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean)
25 template<class RealType = double>
26 class bernoulli_distribution
29 // In principle, this could work with both integer and floating-point
30 // types. Generating floating-point random numbers in the first
31 // place is probably more expensive, so use integer as input.
32 typedef int input_type;
33 typedef bool result_type;
35 explicit bernoulli_distribution(const RealType& p = RealType(0.5))
42 // compiler-generated copy ctor and assignment operator are fine
44 RealType p() const { return _p; }
47 template<class Engine>
48 result_type operator()(Engine& eng)
53 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
56 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
57 template<class CharT, class Traits>
58 friend std::basic_ostream<CharT,Traits>&
59 operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
65 template<class CharT, class Traits>
66 friend std::basic_istream<CharT,Traits>&
67 operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
69 is >> std::ws >> bd._p;
80 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP