1 // Class template uniform_int_distribution -*- C++ -*-
3 // Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * @file bits/uniform_int_dist.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
31 #ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H
32 #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H
34 #include <type_traits>
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 /* Determine whether number is a power of 2. */
44 template<typename _Tp
>
48 return ((__x
- 1) & __x
) == 0;
53 * @brief Uniform discrete distribution for random numbers.
54 * A discrete random distribution on the range @f$[min, max]@f$ with equal
55 * probability throughout the range.
57 template<typename _IntType
= int>
58 class uniform_int_distribution
60 static_assert(std::is_integral
<_IntType
>::value
,
61 "template argument not an integral type");
64 /** The type of the range of the distribution. */
65 typedef _IntType result_type
;
66 /** Parameter type. */
69 typedef uniform_int_distribution
<_IntType
> distribution_type
;
72 param_type(_IntType __a
= 0,
73 _IntType __b
= std::numeric_limits
<_IntType
>::max())
74 : _M_a(__a
), _M_b(__b
)
76 __glibcxx_assert(_M_a
<= _M_b
);
88 operator==(const param_type
& __p1
, const param_type
& __p2
)
89 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
98 * @brief Constructs a uniform distribution object.
101 uniform_int_distribution(_IntType __a
= 0,
102 _IntType __b
= std::numeric_limits
<_IntType
>::max())
107 uniform_int_distribution(const param_type
& __p
)
112 * @brief Resets the distribution state.
114 * Does nothing for the uniform integer distribution.
121 { return _M_param
.a(); }
125 { return _M_param
.b(); }
128 * @brief Returns the parameter set of the distribution.
135 * @brief Sets the parameter set of the distribution.
136 * @param __param The new parameter set of the distribution.
139 param(const param_type
& __param
)
140 { _M_param
= __param
; }
143 * @brief Returns the inclusive lower bound of the distribution range.
147 { return this->a(); }
150 * @brief Returns the inclusive upper bound of the distribution range.
154 { return this->b(); }
157 * @brief Generating functions.
159 template<typename _UniformRandomNumberGenerator
>
161 operator()(_UniformRandomNumberGenerator
& __urng
)
162 { return this->operator()(__urng
, _M_param
); }
164 template<typename _UniformRandomNumberGenerator
>
166 operator()(_UniformRandomNumberGenerator
& __urng
,
167 const param_type
& __p
);
169 template<typename _ForwardIterator
,
170 typename _UniformRandomNumberGenerator
>
172 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
173 _UniformRandomNumberGenerator
& __urng
)
174 { this->__generate(__f
, __t
, __urng
, _M_param
); }
176 template<typename _ForwardIterator
,
177 typename _UniformRandomNumberGenerator
>
179 __generate(_ForwardIterator __f
, _ForwardIterator __t
,
180 _UniformRandomNumberGenerator
& __urng
,
181 const param_type
& __p
)
182 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
184 template<typename _UniformRandomNumberGenerator
>
186 __generate(result_type
* __f
, result_type
* __t
,
187 _UniformRandomNumberGenerator
& __urng
,
188 const param_type
& __p
)
189 { this->__generate_impl(__f
, __t
, __urng
, __p
); }
192 * @brief Return true if two uniform integer distributions have
193 * the same parameters.
196 operator==(const uniform_int_distribution
& __d1
,
197 const uniform_int_distribution
& __d2
)
198 { return __d1
._M_param
== __d2
._M_param
; }
201 template<typename _ForwardIterator
,
202 typename _UniformRandomNumberGenerator
>
204 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
205 _UniformRandomNumberGenerator
& __urng
,
206 const param_type
& __p
);
211 template<typename _IntType
>
212 template<typename _UniformRandomNumberGenerator
>
213 typename uniform_int_distribution
<_IntType
>::result_type
214 uniform_int_distribution
<_IntType
>::
215 operator()(_UniformRandomNumberGenerator
& __urng
,
216 const param_type
& __param
)
218 typedef typename
_UniformRandomNumberGenerator::result_type
220 typedef typename
std::make_unsigned
<result_type
>::type __utype
;
221 typedef typename
std::common_type
<_Gresult_type
, __utype
>::type
224 const __uctype __urngmin
= __urng
.min();
225 const __uctype __urngmax
= __urng
.max();
226 const __uctype __urngrange
= __urngmax
- __urngmin
;
227 const __uctype __urange
228 = __uctype(__param
.b()) - __uctype(__param
.a());
232 if (__urngrange
> __urange
)
235 const __uctype __uerange
= __urange
+ 1; // __urange can be zero
236 const __uctype __scaling
= __urngrange
/ __uerange
;
237 const __uctype __past
= __uerange
* __scaling
;
239 __ret
= __uctype(__urng()) - __urngmin
;
240 while (__ret
>= __past
);
243 else if (__urngrange
< __urange
)
247 Note that every value in [0, urange]
248 can be written uniquely as
250 (urngrange + 1) * high + low
254 high in [0, urange / (urngrange + 1)]
258 low in [0, urngrange].
260 __uctype __tmp
; // wraparound control
263 const __uctype __uerngrange
= __urngrange
+ 1;
264 __tmp
= (__uerngrange
* operator()
265 (__urng
, param_type(0, __urange
/ __uerngrange
)));
266 __ret
= __tmp
+ (__uctype(__urng()) - __urngmin
);
268 while (__ret
> __urange
|| __ret
< __tmp
);
271 __ret
= __uctype(__urng()) - __urngmin
;
273 return __ret
+ __param
.a();
277 template<typename _IntType
>
278 template<typename _ForwardIterator
,
279 typename _UniformRandomNumberGenerator
>
281 uniform_int_distribution
<_IntType
>::
282 __generate_impl(_ForwardIterator __f
, _ForwardIterator __t
,
283 _UniformRandomNumberGenerator
& __urng
,
284 const param_type
& __param
)
286 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
287 typedef typename
_UniformRandomNumberGenerator::result_type
289 typedef typename
std::make_unsigned
<result_type
>::type __utype
;
290 typedef typename
std::common_type
<_Gresult_type
, __utype
>::type
293 const __uctype __urngmin
= __urng
.min();
294 const __uctype __urngmax
= __urng
.max();
295 const __uctype __urngrange
= __urngmax
- __urngmin
;
296 const __uctype __urange
297 = __uctype(__param
.b()) - __uctype(__param
.a());
301 if (__urngrange
> __urange
)
303 if (__detail::_Power_of_2(__urngrange
+ 1)
304 && __detail::_Power_of_2(__urange
+ 1))
308 __ret
= __uctype(__urng()) - __urngmin
;
309 *__f
++ = (__ret
& __urange
) + __param
.a();
315 const __uctype __uerange
= __urange
+ 1; // __urange can be zero
316 const __uctype __scaling
= __urngrange
/ __uerange
;
317 const __uctype __past
= __uerange
* __scaling
;
321 __ret
= __uctype(__urng()) - __urngmin
;
322 while (__ret
>= __past
);
323 *__f
++ = __ret
/ __scaling
+ __param
.a();
327 else if (__urngrange
< __urange
)
331 Note that every value in [0, urange]
332 can be written uniquely as
334 (urngrange + 1) * high + low
338 high in [0, urange / (urngrange + 1)]
342 low in [0, urngrange].
344 __uctype __tmp
; // wraparound control
349 const __uctype __uerngrange
= __urngrange
+ 1;
350 __tmp
= (__uerngrange
* operator()
351 (__urng
, param_type(0, __urange
/ __uerngrange
)));
352 __ret
= __tmp
+ (__uctype(__urng()) - __urngmin
);
354 while (__ret
> __urange
|| __ret
< __tmp
);
360 *__f
++ = __uctype(__urng()) - __urngmin
+ __param
.a();
363 _GLIBCXX_END_NAMESPACE_VERSION