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