blob: 1a141105ff6f2935b7b78501bed462f05ea0cf5e [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/* boost random/uniform_01.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_01.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
11 *
12 * Revision history
13 * 2001-02-18 moved to individual header files
14 */
15
16#ifndef NDNBOOST_RANDOM_UNIFORM_01_HPP
17#define NDNBOOST_RANDOM_UNIFORM_01_HPP
18
19#include <iostream>
20#include <ndnboost/config.hpp>
21#include <ndnboost/limits.hpp>
22#include <ndnboost/static_assert.hpp>
23#include <ndnboost/random/detail/config.hpp>
24#include <ndnboost/random/detail/ptr_helper.hpp>
25
26#include <ndnboost/random/detail/disable_warnings.hpp>
27
28namespace ndnboost {
29namespace random {
30
31#ifdef NDNBOOST_RANDOM_DOXYGEN
32
33/**
34 * The distribution function uniform_01 models a \random_distribution.
35 * On each invocation, it returns a random floating-point value
36 * uniformly distributed in the range [0..1).
37 *
38 * The template parameter RealType shall denote a float-like value type
39 * with support for binary operators +, -, and /.
40 *
41 * Note: The current implementation is buggy, because it may not fill
42 * all of the mantissa with random bits. I'm unsure how to fill a
43 * (to-be-invented) @c ndnboost::bigfloat class with random bits efficiently.
44 * It's probably time for a traits class.
45 */
46template<class RealType = double>
47class uniform_01
48{
49public:
50 typedef RealType input_type;
51 typedef RealType result_type;
52 result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const;
53 result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const;
54 void reset();
55
56 template<class Engine>
57 result_type operator()(Engine& eng);
58
59#ifndef NDNBOOST_RANDOM_NO_STREAM_OPERATORS
60 template<class CharT, class Traits>
61 friend std::basic_ostream<CharT,Traits>&
62 operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
63 {
64 return os;
65 }
66
67 template<class CharT, class Traits>
68 friend std::basic_istream<CharT,Traits>&
69 operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
70 {
71 return is;
72 }
73#endif
74};
75
76#else
77
78namespace detail {
79
80template<class RealType>
81class new_uniform_01
82{
83public:
84 typedef RealType input_type;
85 typedef RealType result_type;
86 // compiler-generated copy ctor and copy assignment are fine
87 result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
88 result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
89 void reset() { }
90
91 template<class Engine>
92 result_type operator()(Engine& eng) {
93 for (;;) {
94 typedef typename Engine::result_type base_result;
95 result_type factor = result_type(1) /
96 (result_type((eng.max)()-(eng.min)()) +
97 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
98 result_type result = result_type(eng() - (eng.min)()) * factor;
99 if (result < result_type(1))
100 return result;
101 }
102 }
103
104#ifndef NDNBOOST_RANDOM_NO_STREAM_OPERATORS
105 template<class CharT, class Traits>
106 friend std::basic_ostream<CharT,Traits>&
107 operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
108 {
109 return os;
110 }
111
112 template<class CharT, class Traits>
113 friend std::basic_istream<CharT,Traits>&
114 operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
115 {
116 return is;
117 }
118#endif
119};
120
121template<class UniformRandomNumberGenerator, class RealType>
122class backward_compatible_uniform_01
123{
124 typedef ndnboost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
125public:
126 typedef UniformRandomNumberGenerator base_type;
127 typedef RealType result_type;
128
129 NDNBOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
130
131#if !defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(NDNBOOST_MSVC) && NDNBOOST_MSVC <= 1300)
132 NDNBOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
133#endif
134
135 explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
136 : _rng(rng),
137 _factor(result_type(1) /
138 (result_type((base().max)()-(base().min)()) +
139 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
140 {
141 }
142 // compiler-generated copy ctor and copy assignment are fine
143
144 result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
145 result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
146 typename traits::value_type& base() { return traits::ref(_rng); }
147 const typename traits::value_type& base() const { return traits::ref(_rng); }
148 void reset() { }
149
150 result_type operator()() {
151 for (;;) {
152 result_type result = result_type(base()() - (base().min)()) * _factor;
153 if (result < result_type(1))
154 return result;
155 }
156 }
157
158#if !defined(NDNBOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS)
159 template<class CharT, class Traits>
160 friend std::basic_ostream<CharT,Traits>&
161 operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
162 {
163 os << u._rng;
164 return os;
165 }
166
167 template<class CharT, class Traits>
168 friend std::basic_istream<CharT,Traits>&
169 operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
170 {
171 is >> u._rng;
172 return is;
173 }
174#endif
175
176private:
177 typedef typename traits::value_type::result_type base_result;
178 UniformRandomNumberGenerator _rng;
179 result_type _factor;
180};
181
182#ifndef NDNBOOST_NO_INCLASS_MEMBER_INITIALIZATION
183// A definition is required even for integral static constants
184template<class UniformRandomNumberGenerator, class RealType>
185const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
186#endif
187
188template<class UniformRandomNumberGenerator>
189struct select_uniform_01
190{
191 template<class RealType>
192 struct apply
193 {
194 typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
195 };
196};
197
198template<>
199struct select_uniform_01<float>
200{
201 template<class RealType>
202 struct apply
203 {
204 typedef new_uniform_01<float> type;
205 };
206};
207
208template<>
209struct select_uniform_01<double>
210{
211 template<class RealType>
212 struct apply
213 {
214 typedef new_uniform_01<double> type;
215 };
216};
217
218template<>
219struct select_uniform_01<long double>
220{
221 template<class RealType>
222 struct apply
223 {
224 typedef new_uniform_01<long double> type;
225 };
226};
227
228}
229
230// Because it is so commonly used: uniform distribution on the real [0..1)
231// range. This allows for specializations to avoid a costly int -> float
232// conversion plus float multiplication
233template<class UniformRandomNumberGenerator = double, class RealType = double>
234class uniform_01
235 : public detail::select_uniform_01<UniformRandomNumberGenerator>::NDNBOOST_NESTED_TEMPLATE apply<RealType>::type
236{
237 typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::NDNBOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
238 typedef ndnboost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
239public:
240
241 uniform_01() {}
242
243 explicit uniform_01(typename traits::rvalue_type rng)
244 : impl_type(rng)
245 {
246 }
247
248#if !defined(NDNBOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS)
249 template<class CharT, class Traits>
250 friend std::basic_ostream<CharT,Traits>&
251 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
252 {
253 os << static_cast<const impl_type&>(u);
254 return os;
255 }
256
257 template<class CharT, class Traits>
258 friend std::basic_istream<CharT,Traits>&
259 operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
260 {
261 is >> static_cast<impl_type&>(u);
262 return is;
263 }
264#endif
265};
266
267#endif
268
269} // namespace random
270
271using random::uniform_01;
272
273} // namespace ndnboost
274
275#include <ndnboost/random/detail/enable_warnings.hpp>
276
277#endif // NDNBOOST_RANDOM_UNIFORM_01_HPP