fix doc example typo
[boost.git] / boost / random / bernoulli_distribution.hpp
blob5a417270a358de824bb7a9447b846bf3b290bcb0
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$
12 * Revision history
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
19 #include <cassert>
20 #include <iostream>
21 #include <boost/random/detail/config.hpp>
23 namespace boost {
25 // Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean)
26 template<class RealType = double>
27 class bernoulli_distribution
29 public:
30 // In principle, this could work with both integer and floating-point
31 // types. Generating floating-point random numbers in the first
32 // place is probably more expensive, so use integer as input.
33 typedef int input_type;
34 typedef bool result_type;
36 explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
37 : _p(p_arg)
39 assert(_p >= 0);
40 assert(_p <= 1);
43 // compiler-generated copy ctor and assignment operator are fine
45 RealType p() const { return _p; }
46 void reset() { }
48 template<class Engine>
49 result_type operator()(Engine& eng)
51 if(_p == RealType(0))
52 return false;
53 else
54 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
57 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
58 template<class CharT, class Traits>
59 friend std::basic_ostream<CharT,Traits>&
60 operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
62 os << bd._p;
63 return os;
66 template<class CharT, class Traits>
67 friend std::basic_istream<CharT,Traits>&
68 operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
70 is >> std::ws >> bd._p;
71 return is;
73 #endif
75 private:
76 RealType _p;
79 } // namespace boost
81 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP