From 411eeb9c26f1520458254be276b10fe8ccd615af Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Sat, 5 Feb 2011 05:54:11 +0000 Subject: [PATCH] Update uniform_int and uniform_real. [SVN r68647] --- include/boost/random/uniform_int.hpp | 297 +++----------------- include/boost/random/uniform_real.hpp | 106 +++---- test/Jamfile.v2 | 4 + test/test_old_uniform_int.cpp | 26 ++ test/test_old_uniform_int_distribution.cpp | 38 +++ test/test_old_uniform_real.cpp | 26 ++ test/test_old_uniform_real_distribution.cpp | 38 +++ 7 files changed, 214 insertions(+), 321 deletions(-) create mode 100644 test/test_old_uniform_int.cpp create mode 100644 test/test_old_uniform_int_distribution.cpp create mode 100644 test/test_old_uniform_real.cpp create mode 100644 test/test_old_uniform_real_distribution.cpp diff --git a/include/boost/random/uniform_int.hpp b/include/boost/random/uniform_int.hpp index 2eff53b..e258835 100644 --- a/include/boost/random/uniform_int.hpp +++ b/include/boost/random/uniform_int.hpp @@ -18,14 +18,7 @@ #define BOOST_RANDOM_UNIFORM_INT_HPP #include -#include -#include -#include -#include -#include -#include -#include -#include +#include namespace boost { @@ -35,264 +28,58 @@ namespace boost { * distributed in the set of integer numbers {min, min+1, min+2, ..., max}. * * The template parameter IntType shall denote an integer-like value type. + * + * This class is deprecated. Please use @c uniform_int_distribution in + * new code. */ template -class uniform_int +class uniform_int : public random::uniform_int_distribution { + typedef random::uniform_int_distribution base_type; public: - typedef IntType input_type; - typedef IntType result_type; - /// \cond hide_private_members - typedef typename make_unsigned::type range_type; - /// \endcond + class param_type : public base_type::param_type + { + public: + typedef uniform_int distribution_type; + /** + * Constructs the parameters of a uniform_int distribution. + * + * Requires: min <= max + */ + explicit param_type(IntType min_arg = 0, IntType max_arg = 9) + : base_type::param_type(min_arg, max_arg) + {} + }; - /** - * Constructs a uniform_int object. @c min and @c max are - * the parameters of the distribution. - * - * Requires: min <= max - */ - explicit uniform_int(IntType min_arg = 0, IntType max_arg = 9) - : _min(min_arg), _max(max_arg) - { -#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS - // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope - BOOST_STATIC_ASSERT(std::numeric_limits::is_integer); -#endif - assert(min_arg <= max_arg); - init(); - } + /** + * Constructs a uniform_int object. @c min and @c max are + * the parameters of the distribution. + * + * Requires: min <= max + */ + explicit uniform_int(IntType min_arg = 0, IntType max_arg = 9) + : base_type(min_arg, max_arg) + {} - /** - * Returns: The "min" parameter of the distribution - */ - result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; } - /** - * Returns: The "max" parameter of the distribution - */ - result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; } - void reset() { } - - // can't have member function templates out-of-line due to MSVC bugs - template - result_type operator()(Engine& eng) - { - return generate(eng, _min, _max, _range); - } + /** Constructs a uniform_int distribution from its parameters. */ + explicit uniform_int(const param_type& parm) + : base_type(parm) + {} - template - result_type operator()(Engine& eng, result_type n) - { - assert(n > 0); + /** Returns the parameters of the distribution */ + param_type param() const { return param_type(this->a(), this->b()); } + /** Sets the parameters of the distribution. */ + void param(const param_type& parm) { this->base_type::param(parm); } - if (n == 1) - { - return 0; - } + using base_type::operator(); - return generate(eng, 0, n - 1, n - 1); - } - -#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS - template - friend std::basic_ostream& - operator<<(std::basic_ostream& os, const uniform_int& ud) - { - os << ud._min << " " << ud._max; - return os; - } - - template - friend std::basic_istream& - operator>>(std::basic_istream& is, uniform_int& ud) - { - is >> std::ws >> ud._min >> std::ws >> ud._max; - ud.init(); - return is; - } -#endif - -private: - -#ifdef BOOST_MSVC -#pragma warning(push) -// disable division by zero warning, since we can't -// actually divide by zero. -#pragma warning(disable:4723) -#endif - - /// \cond hide_private_members - template - static result_type generate(Engine& eng, result_type min_value, result_type /*max_value*/, range_type range) - { - typedef typename Engine::result_type base_result; - // ranges are always unsigned - typedef typename make_unsigned::type base_unsigned; - const base_result bmin = (eng.min)(); - const base_unsigned brange = - random::detail::subtract()((eng.max)(), (eng.min)()); - - if(range == 0) { - return min_value; - } else if(brange == range) { - // this will probably never happen in real life - // basically nothing to do; just take care we don't overflow / underflow - base_unsigned v = random::detail::subtract()(eng(), bmin); - return random::detail::add()(v, min_value); - } else if(brange < range) { - // use rejection method to handle things like 0..3 --> 0..4 - for(;;) { - // concatenate several invocations of the base RNG - // take extra care to avoid overflows - - // limit == floor((range+1)/(brange+1)) - // Therefore limit*(brange+1) <= range+1 - range_type limit; - if(range == (std::numeric_limits::max)()) { - limit = range/(range_type(brange)+1); - if(range % (range_type(brange)+1) == range_type(brange)) - ++limit; - } else { - limit = (range+1)/(range_type(brange)+1); - } - - // We consider "result" as expressed to base (brange+1): - // For every power of (brange+1), we determine a random factor - range_type result = range_type(0); - range_type mult = range_type(1); - - // loop invariants: - // result < mult - // mult <= range - while(mult <= limit) { - // Postcondition: result <= range, thus no overflow - // - // limit*(brange+1)<=range+1 def. of limit (1) - // eng()-bmin<=brange eng() post. (2) - // and mult<=limit. loop condition (3) - // Therefore mult*(eng()-bmin+1)<=range+1 by (1),(2),(3) (4) - // Therefore mult*(eng()-bmin)+mult<=range+1 rearranging (4) (5) - // result(random::detail::subtract()(eng(), bmin) * mult); - - // equivalent to (mult * (brange+1)) == range+1, but avoids overflow. - if(mult * range_type(brange) == range - mult + 1) { - // The destination range is an integer power of - // the generator's range. - return(result); - } - - // Postcondition: mult <= range - // - // limit*(brange+1)<=range+1 def. of limit (1) - // mult<=limit loop condition (2) - // Therefore mult*(brange+1)<=range+1 by (1), (2) (3) - // mult*(brange+1)!=range+1 preceding if (4) - // Therefore mult*(brange+1) limit loop condition (1) - // Suppose range/mult >= brange+1 Assumption (2) - // range >= mult*(brange+1) by (2) (3) - // range+1 > mult*(brange+1) by (3) (4) - // range+1 > (limit+1)*(brange+1) by (1), (4) (5) - // (range+1)/(brange+1) > limit+1 by (5) (6) - // limit < floor((range+1)/(brange+1)) by (6) (7) - // limit==floor((range+1)/(brange+1)) def. of limit (8) - // not (2) reductio (9) - // - // loop postcondition: (range/mult)*mult+(mult-1) >= range - // - // (range/mult)*mult + range%mult == range identity (1) - // range%mult < mult def. of % (2) - // (range/mult)*mult+mult > range by (1), (2) (3) - // (range/mult)*mult+(mult-1) >= range by (3) (4) - // - // Note that the maximum value of result at this point is (mult-1), - // so after this final step, we generate numbers that can be - // at least as large as range. We have to really careful to avoid - // overflow in this final addition and in the rejection. Anything - // that overflows is larger than range and can thus be rejected. - - // range/mult < brange+1 -> no endless loop - range_type result_increment = uniform_int(0, range/mult)(eng); - if((std::numeric_limits::max)() / mult < result_increment) { - // The multiplcation would overflow. Reject immediately. - continue; - } - result_increment *= mult; - // unsigned integers are guaranteed to wrap on overflow. - result += result_increment; - if(result < result_increment) { - // The addition overflowed. Reject. - continue; - } - if(result > range) { - // Too big. Reject. - continue; - } - return random::detail::add()(result, min_value); - } - } else { // brange > range - base_unsigned bucket_size; - // it's safe to add 1 to range, as long as we cast it first, - // because we know that it is less than brange. However, - // we do need to be careful not to cause overflow by adding 1 - // to brange. - if(brange == (std::numeric_limits::max)()) { - bucket_size = brange / (static_cast(range)+1); - if(brange % (static_cast(range)+1) == static_cast(range)) { - ++bucket_size; - } - } else { - bucket_size = (brange+1) / (static_cast(range)+1); - } - for(;;) { - base_unsigned result = - random::detail::subtract()(eng(), bmin); - result /= bucket_size; - // result and range are non-negative, and result is possibly larger - // than range, so the cast is safe - if(result <= static_cast(range)) - return random::detail::add()(result, min_value); - } + template + IntType operator()(Engine& eng, IntType n) + { + assert(n > 0); + return (*this)(eng, param_type(0, n - 1)); } - } - -#ifdef BOOST_MSVC -#pragma warning(pop) -#endif - - void init() - { - _range = random::detail::subtract()(_max, _min); - } - - /// \endcond - - // The result_type may be signed or unsigned, but the _range is always - // unsigned. - result_type _min, _max; - range_type _range; }; } // namespace boost diff --git a/include/boost/random/uniform_real.hpp b/include/boost/random/uniform_real.hpp index 544ca40..762d0a5 100644 --- a/include/boost/random/uniform_real.hpp +++ b/include/boost/random/uniform_real.hpp @@ -18,89 +18,63 @@ #define BOOST_RANDOM_UNIFORM_REAL_HPP #include -#include #include #include -#include -#include +#include namespace boost { /** * The distribution function uniform_real models a random distribution. * On each invocation, it returns a random floating-point value uniformly - * distributed in the range [min..max). The value is computed using - * std::numeric_limits::digits random binary digits, i.e. - * the mantissa of the floating-point value is completely filled with - * random bits. + * distributed in the range [min..max). * - * Note: The current implementation is buggy, because it may not fill - * all of the mantissa with random bits. + * This class is deprecated. Please use @c uniform_real_distribution in + * new code. */ template -class uniform_real +class uniform_real : public random::uniform_real_distribution { + typedef random::uniform_real_distribution base_type; public: - typedef RealType input_type; - typedef RealType result_type; - /** - * Constructs a uniform_real object. @c min and @c max are the - * parameters of the distribution. - * - * Requires: min <= max - */ - explicit uniform_real(RealType min_arg = RealType(0.0), - RealType max_arg = RealType(1.0)) - : _min(min_arg), _max(max_arg) - { -#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS - BOOST_STATIC_ASSERT(!std::numeric_limits::is_integer); -#endif - assert(min_arg <= max_arg); - } + class param_type : public base_type::param_type + { + public: + typedef uniform_real distribution_type; + /** + * Constructs the parameters of a uniform_real distribution. + * + * Requires: min <= max + */ + explicit param_type(RealType min_arg = RealType(0.0), + RealType max_arg = RealType(1.0)) + : base_type::param_type(min_arg, max_arg) + {} + }; - // compiler-generated copy ctor and assignment operator are fine + /** + * Constructs a uniform_real object. @c min and @c max are the + * parameters of the distribution. + * + * Requires: min <= max + */ + explicit uniform_real(RealType min_arg = RealType(0.0), + RealType max_arg = RealType(1.0)) + : base_type(min_arg, max_arg) + { + assert(min_arg <= max_arg); + } - /** - * Returns: The "min" parameter of the distribution - */ - result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; } - /** - * Returns: The "max" parameter of the distribution - */ - result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; } - void reset() { } + /** Constructs a uniform_real distribution from its parameters. */ + explicit uniform_real(const param_type& parm) + : base_type(parm) + {} - template - result_type operator()(Engine& eng) { - result_type numerator = static_cast(eng() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION()); - result_type divisor = static_cast(eng.max BOOST_PREVENT_MACRO_SUBSTITUTION() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION()); - assert(divisor > 0); - assert(numerator >= 0 && numerator <= divisor); - return numerator / divisor * (_max - _min) + _min; - } - -#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS - template - friend std::basic_ostream& - operator<<(std::basic_ostream& os, const uniform_real& ud) - { - os << ud._min << " " << ud._max; - return os; - } - - template - friend std::basic_istream& - operator>>(std::basic_istream& is, uniform_real& ud) - { - is >> std::ws >> ud._min >> std::ws >> ud._max; - return is; - } -#endif - -private: - RealType _min, _max; + /** Returns the parameters of the distribution */ + param_type param() const { return param_type(this->a(), this->b()); } + /** Sets the parameters of the distribution. */ + void param(const param_type& parm) { this->base_type::param(parm); } }; } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index e71bd18..7f7dee0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -92,6 +92,10 @@ run test_uniform_real_distribution.cpp /boost//unit_test_framework ; run test_uniform_on_sphere_distribution.cpp /boost//unit_test_framework ; run test_uniform_smallint.cpp ; run test_uniform_smallint_distribution.cpp /boost//unit_test_framework ; +run test_old_uniform_real.cpp ; +run test_old_uniform_real_distribution.cpp /boost//unit_test_framework ; +run test_old_uniform_int.cpp ; +run test_old_uniform_int_distribution.cpp /boost//unit_test_framework ; # run nondet_random_speed.cpp ; # run random_device.cpp ; diff --git a/test/test_old_uniform_int.cpp b/test/test_old_uniform_int.cpp new file mode 100644 index 0000000..442f22f --- /dev/null +++ b/test/test_old_uniform_int.cpp @@ -0,0 +1,26 @@ +/* test_old_uniform_int.cpp + * + * Copyright Steven Watanabe 2011 + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * $Id$ + * + */ + +#include +#include + +#define BOOST_RANDOM_DISTRIBUTION boost::uniform_int<> +#define BOOST_RANDOM_DISTRIBUTION_NAME uniform_int +#define BOOST_MATH_DISTRIBUTION boost::math::uniform +#define BOOST_RANDOM_ARG1_TYPE int +#define BOOST_RANDOM_ARG1_NAME b +#define BOOST_RANDOM_ARG1_DEFAULT 1000 +#define BOOST_RANDOM_ARG1_DISTRIBUTION(n) boost::uniform_int<>(0, n) +#define BOOST_RANDOM_DISTRIBUTION_INIT (0, b) +#define BOOST_MATH_DISTRIBUTION_INIT (0, b+1) +#define BOOST_RANDOM_DISTRIBUTION_MAX b + +#include "test_real_distribution.ipp" diff --git a/test/test_old_uniform_int_distribution.cpp b/test/test_old_uniform_int_distribution.cpp new file mode 100644 index 0000000..b05c0c6 --- /dev/null +++ b/test/test_old_uniform_int_distribution.cpp @@ -0,0 +1,38 @@ +/* test_old_uniform_int_distribution.cpp + * + * Copyright Steven Watanabe 2011 + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * $Id$ + * + */ + +#include +#include + +#define BOOST_RANDOM_DISTRIBUTION boost::uniform_int<> +#define BOOST_RANDOM_ARG1 a +#define BOOST_RANDOM_ARG2 b +#define BOOST_RANDOM_ARG1_DEFAULT 0 +#define BOOST_RANDOM_ARG2_DEFAULT 9 +#define BOOST_RANDOM_ARG1_VALUE 5 +#define BOOST_RANDOM_ARG2_VALUE 250 + +#define BOOST_RANDOM_DIST0_MIN 0 +#define BOOST_RANDOM_DIST0_MAX 9 +#define BOOST_RANDOM_DIST1_MIN 5 +#define BOOST_RANDOM_DIST1_MAX 9 +#define BOOST_RANDOM_DIST2_MIN 5 +#define BOOST_RANDOM_DIST2_MAX 250 + +#define BOOST_RANDOM_TEST1_PARAMS (0, 9) +#define BOOST_RANDOM_TEST1_MIN 0 +#define BOOST_RANDOM_TEST1_MAX 9 + +#define BOOST_RANDOM_TEST2_PARAMS (10, 19) +#define BOOST_RANDOM_TEST2_MIN 10 +#define BOOST_RANDOM_TEST2_MAX 19 + +#include "test_distribution.ipp" diff --git a/test/test_old_uniform_real.cpp b/test/test_old_uniform_real.cpp new file mode 100644 index 0000000..8755e38 --- /dev/null +++ b/test/test_old_uniform_real.cpp @@ -0,0 +1,26 @@ +/* test_old_uniform_real.cpp + * + * Copyright Steven Watanabe 2011 + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * $Id$ + * + */ + +#include +#include +#include + +#define BOOST_RANDOM_DISTRIBUTION boost::uniform_real<> +#define BOOST_RANDOM_DISTRIBUTION_NAME uniform_real +#define BOOST_MATH_DISTRIBUTION boost::math::uniform +#define BOOST_RANDOM_ARG1_TYPE double +#define BOOST_RANDOM_ARG1_NAME b +#define BOOST_RANDOM_ARG1_DEFAULT 1000 +#define BOOST_RANDOM_ARG1_DISTRIBUTION(n) boost::uniform_real<>(0, n) +#define BOOST_RANDOM_DISTRIBUTION_INIT (0, b) +#define BOOST_MATH_DISTRIBUTION_INIT (0, b) + +#include "test_real_distribution.ipp" diff --git a/test/test_old_uniform_real_distribution.cpp b/test/test_old_uniform_real_distribution.cpp new file mode 100644 index 0000000..1728fb3 --- /dev/null +++ b/test/test_old_uniform_real_distribution.cpp @@ -0,0 +1,38 @@ +/* test_old_uniform_real_distribution.cpp + * + * Copyright Steven Watanabe 2011 + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * $Id$ + * + */ + +#include +#include + +#define BOOST_RANDOM_DISTRIBUTION boost::uniform_real<> +#define BOOST_RANDOM_ARG1 a +#define BOOST_RANDOM_ARG2 b +#define BOOST_RANDOM_ARG1_DEFAULT 0.0 +#define BOOST_RANDOM_ARG2_DEFAULT 1.0 +#define BOOST_RANDOM_ARG1_VALUE -0.5 +#define BOOST_RANDOM_ARG2_VALUE 1.5 + +#define BOOST_RANDOM_DIST0_MIN 0.0 +#define BOOST_RANDOM_DIST0_MAX 1.0 +#define BOOST_RANDOM_DIST1_MIN -0.5 +#define BOOST_RANDOM_DIST1_MAX 1.0 +#define BOOST_RANDOM_DIST2_MIN -0.5 +#define BOOST_RANDOM_DIST2_MAX 1.5 + +#define BOOST_RANDOM_TEST1_PARAMS (-1.0, 0.0) +#define BOOST_RANDOM_TEST1_MIN -1.0 +#define BOOST_RANDOM_TEST1_MAX 0.0 + +#define BOOST_RANDOM_TEST2_PARAMS +#define BOOST_RANDOM_TEST2_MIN 0.0 +#define BOOST_RANDOM_TEST2_MAX 1.0 + +#include "test_distribution.ipp"