Chris@16: /* boost random/uniform_real_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: T generate_uniform_real( Chris@16: Engine& eng, T min_value, T max_value, Chris@16: boost::mpl::false_ /** is_integral */) Chris@16: { Chris@16: for(;;) { Chris@16: typedef T result_type; Chris@16: result_type numerator = static_cast(eng() - (eng.min)()); Chris@16: result_type divisor = static_cast((eng.max)() - (eng.min)()); Chris@16: BOOST_ASSERT(divisor > 0); Chris@16: BOOST_ASSERT(numerator >= 0 && numerator <= divisor); Chris@16: T result = numerator / divisor * (max_value - min_value) + min_value; Chris@16: if(result < max_value) return result; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: T generate_uniform_real( Chris@16: Engine& eng, T min_value, T max_value, Chris@16: boost::mpl::true_ /** is_integral */) Chris@16: { Chris@16: for(;;) { Chris@16: typedef T result_type; Chris@16: typedef typename Engine::result_type base_result; Chris@16: result_type numerator = static_cast(subtract()(eng(), (eng.min)())); Chris@16: result_type divisor = static_cast(subtract()((eng.max)(), (eng.min)())) + 1; Chris@16: BOOST_ASSERT(divisor > 0); Chris@16: BOOST_ASSERT(numerator >= 0 && numerator <= divisor); Chris@16: T result = numerator / divisor * (max_value - min_value) + min_value; Chris@16: if(result < max_value) return result; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline T generate_uniform_real(Engine& eng, T min_value, T max_value) Chris@16: { Chris@101: if(max_value / 2 - min_value / 2 > (std::numeric_limits::max)() / 2) Chris@101: return 2 * generate_uniform_real(eng, min_value / 2, max_value / 2); Chris@16: typedef typename Engine::result_type base_result; Chris@16: return generate_uniform_real(eng, min_value, max_value, Chris@16: boost::is_integral()); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: /** Chris@16: * The class template uniform_real_distribution models a \random_distribution. Chris@16: * On each invocation, it returns a random floating-point value uniformly Chris@16: * distributed in the range [min..max). Chris@16: */ Chris@16: template Chris@16: class uniform_real_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef RealType result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef uniform_real_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs the parameters of a uniform_real_distribution. Chris@16: * Chris@16: * Requires min <= max Chris@16: */ Chris@16: explicit param_type(RealType min_arg = RealType(0.0), Chris@16: RealType max_arg = RealType(1.0)) Chris@16: : _min(min_arg), _max(max_arg) Chris@16: { Chris@101: BOOST_ASSERT(_min < _max); Chris@16: } Chris@16: Chris@16: /** Returns the minimum value of the distribution. */ Chris@16: RealType a() const { return _min; } Chris@16: /** Returns the maximum value of the distribution. */ Chris@16: RealType b() const { return _max; } Chris@16: Chris@16: /** Writes the parameters to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._min << " " << parm._max; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: RealType min_in, max_in; Chris@16: if(is >> min_in >> std::ws >> max_in) { Chris@16: if(min_in <= max_in) { Chris@16: parm._min = min_in; Chris@16: parm._max = max_in; Chris@16: } else { Chris@16: is.setstate(std::ios_base::failbit); Chris@16: } Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._min == rhs._min && lhs._max == rhs._max; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: Chris@16: RealType _min; Chris@16: RealType _max; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a uniform_real_distribution. @c min and @c max are Chris@16: * the parameters of the distribution. Chris@16: * Chris@16: * Requires: min <= max Chris@16: */ Chris@16: explicit uniform_real_distribution( Chris@16: RealType min_arg = RealType(0.0), Chris@16: RealType max_arg = RealType(1.0)) Chris@16: : _min(min_arg), _max(max_arg) Chris@16: { Chris@101: BOOST_ASSERT(min_arg < max_arg); Chris@16: } Chris@16: /** Constructs a uniform_real_distribution from its parameters. */ Chris@16: explicit uniform_real_distribution(const param_type& parm) Chris@16: : _min(parm.a()), _max(parm.b()) {} Chris@16: Chris@16: /** Returns the minimum value of the distribution */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; } Chris@16: /** Returns the maximum value of the distribution */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; } Chris@16: Chris@16: /** Returns the minimum value of the distribution */ Chris@16: RealType a() const { return _min; } Chris@16: /** Returns the maximum value of the distribution */ Chris@16: RealType b() const { return _max; } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_min, _max); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _min = parm.a(); Chris@16: _max = parm.b(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Returns a value uniformly distributed in the range [min, max). */ Chris@16: template Chris@16: result_type operator()(Engine& eng) const Chris@16: { return detail::generate_uniform_real(eng, _min, _max); } Chris@16: Chris@16: /** Chris@16: * Returns a value uniformly distributed in the range Chris@16: * [param.a(), param.b()). Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng, const param_type& parm) const Chris@16: { return detail::generate_uniform_real(eng, parm.a(), parm.b()); } Chris@16: Chris@16: /** Writes the distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud) Chris@16: { Chris@16: os << ud.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: ud.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions will produce identical sequences Chris@16: * of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs) Chris@16: { return lhs._min == rhs._min && lhs._max == rhs._max; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions may produce different sequences Chris@16: * of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution) Chris@16: Chris@16: private: Chris@16: RealType _min; Chris@16: RealType _max; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_UNIFORM_INT_HPP