williamr@2
|
1 |
/* boost random/uniform_01.hpp header file
|
williamr@2
|
2 |
*
|
williamr@2
|
3 |
* Copyright Jens Maurer 2000-2001
|
williamr@2
|
4 |
* Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
5 |
* accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
6 |
* http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
7 |
*
|
williamr@2
|
8 |
* See http://www.boost.org for most recent version including documentation.
|
williamr@2
|
9 |
*
|
williamr@2
|
10 |
* $Id: uniform_01.hpp,v 1.18 2004/07/27 03:43:32 dgregor Exp $
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
* Revision history
|
williamr@2
|
13 |
* 2001-02-18 moved to individual header files
|
williamr@2
|
14 |
*/
|
williamr@2
|
15 |
|
williamr@2
|
16 |
#ifndef BOOST_RANDOM_UNIFORM_01_HPP
|
williamr@2
|
17 |
#define BOOST_RANDOM_UNIFORM_01_HPP
|
williamr@2
|
18 |
|
williamr@2
|
19 |
#include <iostream>
|
williamr@2
|
20 |
#include <boost/config.hpp>
|
williamr@2
|
21 |
#include <boost/limits.hpp>
|
williamr@2
|
22 |
#include <boost/static_assert.hpp>
|
williamr@2
|
23 |
|
williamr@2
|
24 |
namespace boost {
|
williamr@2
|
25 |
|
williamr@2
|
26 |
// Because it is so commonly used: uniform distribution on the real [0..1)
|
williamr@2
|
27 |
// range. This allows for specializations to avoid a costly int -> float
|
williamr@2
|
28 |
// conversion plus float multiplication
|
williamr@2
|
29 |
template<class UniformRandomNumberGenerator, class RealType = double>
|
williamr@2
|
30 |
class uniform_01
|
williamr@2
|
31 |
{
|
williamr@2
|
32 |
public:
|
williamr@2
|
33 |
typedef UniformRandomNumberGenerator base_type;
|
williamr@2
|
34 |
typedef RealType result_type;
|
williamr@2
|
35 |
|
williamr@2
|
36 |
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
williamr@2
|
37 |
|
williamr@2
|
38 |
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
|
williamr@2
|
39 |
BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
|
williamr@2
|
40 |
#endif
|
williamr@2
|
41 |
|
williamr@2
|
42 |
explicit uniform_01(base_type rng)
|
williamr@2
|
43 |
: _rng(rng),
|
williamr@2
|
44 |
_factor(result_type(1) /
|
williamr@2
|
45 |
(result_type((_rng.max)()-(_rng.min)()) +
|
williamr@2
|
46 |
result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
|
williamr@2
|
47 |
{
|
williamr@2
|
48 |
}
|
williamr@2
|
49 |
// compiler-generated copy ctor and copy assignment are fine
|
williamr@2
|
50 |
|
williamr@2
|
51 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
|
williamr@2
|
52 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
|
williamr@2
|
53 |
base_type& base() { return _rng; }
|
williamr@2
|
54 |
const base_type& base() const { return _rng; }
|
williamr@2
|
55 |
void reset() { }
|
williamr@2
|
56 |
|
williamr@2
|
57 |
result_type operator()() {
|
williamr@2
|
58 |
return result_type(_rng() - (_rng.min)()) * _factor;
|
williamr@2
|
59 |
}
|
williamr@2
|
60 |
|
williamr@2
|
61 |
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
williamr@2
|
62 |
template<class CharT, class Traits>
|
williamr@2
|
63 |
friend std::basic_ostream<CharT,Traits>&
|
williamr@2
|
64 |
operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
|
williamr@2
|
65 |
{
|
williamr@2
|
66 |
os << u._rng;
|
williamr@2
|
67 |
return os;
|
williamr@2
|
68 |
}
|
williamr@2
|
69 |
|
williamr@2
|
70 |
template<class CharT, class Traits>
|
williamr@2
|
71 |
friend std::basic_istream<CharT,Traits>&
|
williamr@2
|
72 |
operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
|
williamr@2
|
73 |
{
|
williamr@2
|
74 |
is >> u._rng;
|
williamr@2
|
75 |
return is;
|
williamr@2
|
76 |
}
|
williamr@2
|
77 |
#endif
|
williamr@2
|
78 |
|
williamr@2
|
79 |
private:
|
williamr@2
|
80 |
typedef typename base_type::result_type base_result;
|
williamr@2
|
81 |
base_type _rng;
|
williamr@2
|
82 |
result_type _factor;
|
williamr@2
|
83 |
};
|
williamr@2
|
84 |
|
williamr@2
|
85 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
86 |
// A definition is required even for integral static constants
|
williamr@2
|
87 |
template<class UniformRandomNumberGenerator, class RealType>
|
williamr@2
|
88 |
const bool uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
|
williamr@2
|
89 |
#endif
|
williamr@2
|
90 |
|
williamr@2
|
91 |
} // namespace boost
|
williamr@2
|
92 |
|
williamr@2
|
93 |
#endif // BOOST_RANDOM_UNIFORM_01_HPP
|