1 | /* boost random/detail/uniform_int_float.hpp header file
|
---|
2 | *
|
---|
3 | * Copyright Jens Maurer 2000-2001
|
---|
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: uniform_int_float.hpp,v 1.3 2004/07/27 03:43:32 dgregor Exp $
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | #ifndef BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
---|
15 | #define BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
---|
16 |
|
---|
17 | #include <boost/config.hpp>
|
---|
18 | #include <boost/random/uniform_01.hpp>
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace boost {
|
---|
22 | namespace random {
|
---|
23 | namespace detail {
|
---|
24 |
|
---|
25 | template<class UniformRandomNumberGenerator, class IntType = unsigned long>
|
---|
26 | class uniform_int_float
|
---|
27 | {
|
---|
28 | public:
|
---|
29 | typedef UniformRandomNumberGenerator base_type;
|
---|
30 | typedef IntType result_type;
|
---|
31 |
|
---|
32 | uniform_int_float(base_type rng, IntType min = 0, IntType max = 0xffffffff)
|
---|
33 | : _rng(rng), _min(min), _max(max)
|
---|
34 | {
|
---|
35 | init();
|
---|
36 | }
|
---|
37 |
|
---|
38 | result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
|
---|
39 | result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
|
---|
40 | base_type& base() { return _rng.base(); }
|
---|
41 | const base_type& base() const { return _rng.base(); }
|
---|
42 |
|
---|
43 | result_type operator()()
|
---|
44 | {
|
---|
45 | return static_cast<IntType>(_rng() * _range) + _min;
|
---|
46 | }
|
---|
47 |
|
---|
48 | #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
---|
49 | template<class CharT, class Traits>
|
---|
50 | friend std::basic_ostream<CharT,Traits>&
|
---|
51 | operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_int_float& ud)
|
---|
52 | {
|
---|
53 | os << ud._min << " " << ud._max;
|
---|
54 | return os;
|
---|
55 | }
|
---|
56 |
|
---|
57 | template<class CharT, class Traits>
|
---|
58 | friend std::basic_istream<CharT,Traits>&
|
---|
59 | operator>>(std::basic_istream<CharT,Traits>& is, uniform_int_float& ud)
|
---|
60 | {
|
---|
61 | is >> std::ws >> ud._min >> std::ws >> ud._max;
|
---|
62 | ud.init();
|
---|
63 | return is;
|
---|
64 | }
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | private:
|
---|
68 | void init()
|
---|
69 | {
|
---|
70 | _range = static_cast<base_result>(_max-_min)+1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | typedef typename base_type::result_type base_result;
|
---|
74 | uniform_01<base_type> _rng;
|
---|
75 | result_type _min, _max;
|
---|
76 | base_result _range;
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | } // namespace detail
|
---|
81 | } // namespace random
|
---|
82 | } // namespace boost
|
---|
83 |
|
---|
84 | #endif // BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
---|