X-Git-Url: https://git.donarmstrong.com/?p=rsem.git;a=blobdiff_plain;f=boost%2Frandom%2Fgenerate_canonical.hpp;fp=boost%2Frandom%2Fgenerate_canonical.hpp;h=1bba44f0d2ec6b74a0ef5124837822e06e6b7e5e;hp=0000000000000000000000000000000000000000;hb=2d71eb92104693ca9baa5a2e1c23eeca776d8fd3;hpb=da57529b92adbb7ae74a89861cb39fb35ac7c62d;ds=sidebyside diff --git a/boost/random/generate_canonical.hpp b/boost/random/generate_canonical.hpp new file mode 100644 index 0000000..1bba44f --- /dev/null +++ b/boost/random/generate_canonical.hpp @@ -0,0 +1,97 @@ +/* boost random/generate_canonical.hpp header file + * + * 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) + * + * See http://www.boost.org for most recent version including documentation. + * + * $Id: generate_canonical.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $ + * + */ + +#ifndef BOOST_RANDOM_GENERATE_CANONICAL_HPP +#define BOOST_RANDOM_GENERATE_CANONICAL_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace random { + +namespace detail { + +template +RealType generate_canonical_impl(URNG& g, boost::mpl::true_ /*is_integral*/) +{ + using std::pow; + typedef typename URNG::result_type base_result; + std::size_t digits = std::numeric_limits::digits; + RealType R = RealType((g.max)()) - RealType((g.min)()) + 1; + RealType mult = R; + RealType limit = + pow(RealType(2), + RealType((std::min)(static_cast(bits), digits))); + RealType S = RealType(detail::subtract()(g(), (g.min)())); + while(mult < limit) { + RealType inc = RealType(detail::subtract()(g(), (g.min)())); + S += inc * mult; + mult *= R; + } + return S / mult; +} + +template +RealType generate_canonical_impl(URNG& g, boost::mpl::false_ /*is_integral*/) +{ + using std::pow; + using std::floor; + BOOST_ASSERT((g.min)() == 0); + BOOST_ASSERT((g.max)() == 1); + typedef typename URNG::result_type base_result; + std::size_t digits = std::numeric_limits::digits; + std::size_t engine_bits = detail::generator_bits::value(); + std::size_t b = (std::min)(bits, digits); + RealType R = pow(RealType(2), RealType(engine_bits)); + RealType mult = R; + RealType limit = pow(RealType(2), RealType(b)); + RealType S = RealType(g() - (g.min)()); + while(mult < limit) { + RealType inc(floor((RealType(g()) - RealType((g.min)())) * R)); + S += inc * mult; + mult *= R; + } + return S / mult; +} + +} + +/** + * Returns a value uniformly distributed in the range [0, 1) + * with at least @c bits random bits. + */ +template +RealType generate_canonical(URNG& g) +{ + RealType result = detail::generate_canonical_impl( + g, boost::is_integral()); + BOOST_ASSERT(result >= 0); + BOOST_ASSERT(result <= 1); + if(result == 1) { + result -= std::numeric_limits::epsilon() / 2; + BOOST_ASSERT(result != 1); + } + return result; +} + +} // namespace random +} // namespace boost + +#endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP