1 | /* boost random/detail/uniform_int_float.hpp header file |
---|---|
2 | * |
3 | * Copyright Jens Maurer 2000-2001 |
4 | * Copyright Steven Watanabe 2011 |
5 | * Distributed under the Boost Software License, Version 1.0. (See |
6 | * accompanying file LICENSE_1_0.txt or copy at |
7 | * http://www.boost.org/LICENSE_1_0.txt) |
8 | * |
9 | * See http://www.boost.org for most recent version including documentation. |
10 | * |
11 | * $Id$ |
12 | * |
13 | */ |
14 | |
15 | #ifndef BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP |
16 | #define BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP |
17 | |
18 | #include <boost/limits.hpp> |
19 | #include <boost/config.hpp> |
20 | #include <boost/integer.hpp> |
21 | #include <boost/random/detail/config.hpp> |
22 | #include <boost/random/detail/generator_bits.hpp> |
23 | |
24 | #include <boost/random/detail/disable_warnings.hpp> |
25 | |
26 | namespace boost { |
27 | namespace random { |
28 | namespace detail { |
29 | |
30 | template<class URNG> |
31 | class uniform_int_float |
32 | { |
33 | public: |
34 | typedef URNG base_type; |
35 | typedef typename base_type::result_type base_result; |
36 | |
37 | typedef typename boost::uint_t< |
38 | (std::numeric_limits<boost::uintmax_t>::digits < |
39 | std::numeric_limits<base_result>::digits)? |
40 | std::numeric_limits<boost::uintmax_t>::digits : |
41 | std::numeric_limits<base_result>::digits |
42 | >::fast result_type; |
43 | |
44 | uniform_int_float(base_type& rng) |
45 | : _rng(rng) {} |
46 | |
47 | static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () |
48 | { return 0; } |
49 | static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () |
50 | { |
51 | std::size_t digits = std::numeric_limits<result_type>::digits; |
52 | if(detail::generator_bits<URNG>::value() < digits) { |
53 | digits = detail::generator_bits<URNG>::value(); |
54 | } |
55 | return (result_type(2) << (digits - 1)) - 1; |
56 | } |
57 | base_type& base() { return _rng; } |
58 | const base_type& base() const { return _rng; } |
59 | |
60 | result_type operator()() |
61 | { |
62 | base_result range = static_cast<base_result>((max)())+1; |
63 | return static_cast<result_type>(_rng() * range); |
64 | } |
65 | |
66 | private: |
67 | base_type& _rng; |
68 | }; |
69 | |
70 | } // namespace detail |
71 | } // namespace random |
72 | } // namespace boost |
73 | |
74 | #include <boost/random/detail/enable_warnings.hpp> |
75 | |
76 | #endif // BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP |
77 |