31 #ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H 32 #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H 37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 template<
typename _Tp>
48 return ((__x - 1) & __x) == 0;
57 template<
typename _IntType =
int>
61 "template argument not an integral type");
74 : _M_a(__a), _M_b(__b)
76 __glibcxx_assert(_M_a <= _M_b);
89 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
121 {
return _M_param.a(); }
125 {
return _M_param.b(); }
140 { _M_param = __param; }
147 {
return this->a(); }
154 {
return this->b(); }
159 template<
typename _UniformRandomNumberGenerator>
162 {
return this->operator()(__urng, _M_param); }
164 template<
typename _UniformRandomNumberGenerator>
166 operator()(_UniformRandomNumberGenerator& __urng,
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,
182 { this->__generate_impl(__f, __t, __urng, __p); }
184 template<
typename _UniformRandomNumberGenerator>
186 __generate(result_type* __f, result_type* __t,
187 _UniformRandomNumberGenerator& __urng,
189 { this->__generate_impl(__f, __t, __urng, __p); }
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,
211 template<
typename _IntType>
212 template<
typename _UniformRandomNumberGenerator>
215 operator()(_UniformRandomNumberGenerator& __urng,
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;
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)
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>
283 _UniformRandomNumberGenerator& __urng,
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;
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)
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
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
uniform_int_distribution(_IntType __a=0, _IntType __b=std::numeric_limits< _IntType >::max())
Constructs a uniform distribution object.
friend bool operator==(const uniform_int_distribution &__d1, const uniform_int_distribution &__d2)
Return true if two uniform integer distributions have the same parameters.
param_type param() const
Returns the parameter set of the distribution.
void param(const param_type &__param)
Sets the parameter set of the distribution.
result_type max() const
Returns the inclusive upper bound of the distribution range.
Properties of fundamental types.
result_type min() const
Returns the inclusive lower bound of the distribution range.
ISO C++ entities toplevel namespace is std.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...
void reset()
Resets the distribution state.