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 must be an integral type");
76 : _M_a(__a), _M_b(__b)
78 __glibcxx_assert(_M_a <= _M_b);
91 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
95 {
return !(__p1 == __p2); }
132 {
return _M_param.a(); }
136 {
return _M_param.b(); }
151 { _M_param = __param; }
158 {
return this->a(); }
165 {
return this->b(); }
170 template<
typename _UniformRandomNumberGenerator>
173 {
return this->
operator()(__urng, _M_param); }
175 template<
typename _UniformRandomNumberGenerator>
177 operator()(_UniformRandomNumberGenerator& __urng,
178 const param_type& __p);
180 template<
typename _ForwardIterator,
181 typename _UniformRandomNumberGenerator>
183 __generate(_ForwardIterator __f, _ForwardIterator __t,
184 _UniformRandomNumberGenerator& __urng)
185 { this->__generate(__f, __t, __urng, _M_param); }
187 template<
typename _ForwardIterator,
188 typename _UniformRandomNumberGenerator>
190 __generate(_ForwardIterator __f, _ForwardIterator __t,
191 _UniformRandomNumberGenerator& __urng,
192 const param_type& __p)
193 { this->__generate_impl(__f, __t, __urng, __p); }
195 template<
typename _UniformRandomNumberGenerator>
198 _UniformRandomNumberGenerator& __urng,
199 const param_type& __p)
200 { this->__generate_impl(__f, __t, __urng, __p); }
209 {
return __d1._M_param == __d2._M_param; }
212 template<
typename _ForwardIterator,
213 typename _UniformRandomNumberGenerator>
215 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
216 _UniformRandomNumberGenerator& __urng,
217 const param_type& __p);
222 template<
typename _IntType>
223 template<
typename _UniformRandomNumberGenerator>
226 operator()(_UniformRandomNumberGenerator& __urng,
227 const param_type& __param)
229 typedef typename _UniformRandomNumberGenerator::result_type
231 typedef typename std::make_unsigned<result_type>::type __utype;
235 const __uctype __urngmin = __urng.min();
236 const __uctype __urngmax = __urng.max();
237 const __uctype __urngrange = __urngmax - __urngmin;
238 const __uctype __urange
239 = __uctype(__param.b()) - __uctype(__param.a());
243 if (__urngrange > __urange)
246 const __uctype __uerange = __urange + 1;
247 const __uctype __scaling = __urngrange / __uerange;
248 const __uctype __past = __uerange * __scaling;
250 __ret = __uctype(__urng()) - __urngmin;
251 while (__ret >= __past);
254 else if (__urngrange < __urange)
274 const __uctype __uerngrange = __urngrange + 1;
275 __tmp = (__uerngrange * operator()
276 (__urng, param_type(0, __urange / __uerngrange)));
277 __ret = __tmp + (__uctype(__urng()) - __urngmin);
279 while (__ret > __urange || __ret < __tmp);
282 __ret = __uctype(__urng()) - __urngmin;
284 return __ret + __param.a();
288 template<
typename _IntType>
289 template<
typename _ForwardIterator,
290 typename _UniformRandomNumberGenerator>
292 uniform_int_distribution<_IntType>::
293 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
294 _UniformRandomNumberGenerator& __urng,
295 const param_type& __param)
297 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
298 typedef typename _UniformRandomNumberGenerator::result_type
300 typedef typename std::make_unsigned<result_type>::type __utype;
304 const __uctype __urngmin = __urng.min();
305 const __uctype __urngmax = __urng.max();
306 const __uctype __urngrange = __urngmax - __urngmin;
307 const __uctype __urange
308 = __uctype(__param.b()) - __uctype(__param.a());
312 if (__urngrange > __urange)
314 if (__detail::_Power_of_2(__urngrange + 1)
315 && __detail::_Power_of_2(__urange + 1))
319 __ret = __uctype(__urng()) - __urngmin;
320 *__f++ = (__ret & __urange) + __param.a();
326 const __uctype __uerange = __urange + 1;
327 const __uctype __scaling = __urngrange / __uerange;
328 const __uctype __past = __uerange * __scaling;
332 __ret = __uctype(__urng()) - __urngmin;
333 while (__ret >= __past);
334 *__f++ = __ret / __scaling + __param.a();
338 else if (__urngrange < __urange)
360 const __uctype __uerngrange = __urngrange + 1;
361 __tmp = (__uerngrange * operator()
362 (__urng, param_type(0, __urange / __uerngrange)));
363 __ret = __tmp + (__uctype(__urng()) - __urngmin);
365 while (__ret > __urange || __ret < __tmp);
371 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
376 _GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
Properties of fundamental types.
uniform_int_distribution(_IntType __a, _IntType __b=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.
uniform_int_distribution()
Constructs a uniform distribution object.
void reset()
Resets the distribution state.
result_type min() const
Returns the inclusive lower bound of the distribution range.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
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.
param_type param() const
Returns the parameter set of the distribution.