sl@0: /* boost random/bernoulli_distribution.hpp header file sl@0: * sl@0: * Copyright Jens Maurer 2000-2001 sl@0: * Distributed under the Boost Software License, Version 1.0. (See sl@0: * accompanying file LICENSE_1_0.txt or copy at sl@0: * http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: * See http://www.boost.org for most recent version including documentation. sl@0: * sl@0: * $Id: bernoulli_distribution.hpp,v 1.19 2004/07/27 03:43:32 dgregor Exp $ sl@0: * sl@0: * Revision history sl@0: * 2001-02-18 moved to individual header files sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP sl@0: #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP sl@0: sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: // Bernoulli distribution: p(true) = p, p(false) = 1-p (boolean) sl@0: template sl@0: class bernoulli_distribution sl@0: { sl@0: public: sl@0: // In principle, this could work with both integer and floating-point sl@0: // types. Generating floating-point random numbers in the first sl@0: // place is probably more expensive, so use integer as input. sl@0: typedef int input_type; sl@0: typedef bool result_type; sl@0: sl@0: explicit bernoulli_distribution(const RealType& p = RealType(0.5)) sl@0: : _p(p) sl@0: { sl@0: assert(p >= 0); sl@0: assert(p <= 1); sl@0: } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: sl@0: RealType p() const { return _p; } sl@0: void reset() { } sl@0: sl@0: template sl@0: result_type operator()(Engine& eng) sl@0: { sl@0: if(_p == RealType(0)) sl@0: return false; sl@0: else sl@0: return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)()); sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, const bernoulli_distribution& bd) sl@0: { sl@0: os << bd._p; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, bernoulli_distribution& bd) sl@0: { sl@0: is >> std::ws >> bd._p; sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: private: sl@0: RealType _p; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP