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