1 /* boost random/generate_canonical.hpp header file
2 *
3 * Copyright Steven Watanabe 2011
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)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id$
11 *
12 */
13
14 #ifndef BOOST_RANDOM_GENERATE_CANONICAL_HPP
15 #define BOOST_RANDOM_GENERATE_CANONICAL_HPP
16
17 #include <algorithm>
18 #include <boost/assert.hpp>
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/type_traits/is_integral.hpp>
22 #include <boost/mpl/bool.hpp>
23 #include <boost/random/detail/signed_unsigned_tools.hpp>
24 #include <boost/random/detail/generator_bits.hpp>
25
26 namespace boost {
27 namespace random {
28
29 namespace detail {
30
31 template<class RealType, std::size_t bits, class URNG>
generate_canonical_impl(URNG & g,boost::mpl::true_)32 RealType generate_canonical_impl(URNG& g, boost::mpl::true_ /*is_integral*/)
33 {
34 using std::pow;
35 typedef typename URNG::result_type base_result;
36 std::size_t digits = std::numeric_limits<RealType>::digits;
37 RealType R = RealType((g.max)()) - RealType((g.min)()) + 1;
38 RealType mult = R;
39 RealType limit =
40 pow(RealType(2),
41 RealType((std::min)(static_cast<std::size_t>(bits), digits)));
42 RealType S = RealType(detail::subtract<base_result>()(g(), (g.min)()));
43 while(mult < limit) {
44 RealType inc = RealType(detail::subtract<base_result>()(g(), (g.min)()));
45 S += inc * mult;
46 mult *= R;
47 }
48 return S / mult;
49 }
50
51 template<class RealType, std::size_t bits, class URNG>
generate_canonical_impl(URNG & g,boost::mpl::false_)52 RealType generate_canonical_impl(URNG& g, boost::mpl::false_ /*is_integral*/)
53 {
54 using std::pow;
55 using std::floor;
56 BOOST_ASSERT((g.min)() == 0);
57 BOOST_ASSERT((g.max)() == 1);
58 std::size_t digits = std::numeric_limits<RealType>::digits;
59 std::size_t engine_bits = detail::generator_bits<URNG>::value();
60 std::size_t b = (std::min)(bits, digits);
61 RealType R = pow(RealType(2), RealType(engine_bits));
62 RealType mult = R;
63 RealType limit = pow(RealType(2), RealType(b));
64 RealType S = RealType(g() - (g.min)());
65 while(mult < limit) {
66 RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
67 S += inc * mult;
68 mult *= R;
69 }
70 return S / mult;
71 }
72
73 }
74
75 /**
76 * Returns a value uniformly distributed in the range [0, 1)
77 * with at least @c bits random bits.
78 */
79 template<class RealType, std::size_t bits, class URNG>
generate_canonical(URNG & g)80 RealType generate_canonical(URNG& g)
81 {
82 RealType result = detail::generate_canonical_impl<RealType, bits>(
83 g, boost::random::traits::is_integral<typename URNG::result_type>());
84 BOOST_ASSERT(result >= 0);
85 BOOST_ASSERT(result <= 1);
86 if(result == 1) {
87 result -= std::numeric_limits<RealType>::epsilon() / 2;
88 BOOST_ASSERT(result != 1);
89 }
90 return result;
91 }
92
93 } // namespace random
94 } // namespace boost
95
96 #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP
97