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;
60 template<
typename _IntType =
int>
64 "template argument not an integral type");
77 : _M_a(__a), _M_b(__b)
79 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
92 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
124 {
return _M_param.a(); }
128 {
return _M_param.b(); }
143 { _M_param = __param; }
150 {
return this->a(); }
157 {
return this->b(); }
162 template<
typename _UniformRandomNumberGenerator>
165 {
return this->operator()(__urng, _M_param); }
167 template<
typename _UniformRandomNumberGenerator>
169 operator()(_UniformRandomNumberGenerator& __urng,
172 template<
typename _ForwardIterator,
173 typename _UniformRandomNumberGenerator>
175 __generate(_ForwardIterator __f, _ForwardIterator __t,
176 _UniformRandomNumberGenerator& __urng)
177 { this->__generate(__f, __t, __urng, _M_param); }
179 template<
typename _ForwardIterator,
180 typename _UniformRandomNumberGenerator>
182 __generate(_ForwardIterator __f, _ForwardIterator __t,
183 _UniformRandomNumberGenerator& __urng,
185 { this->__generate_impl(__f, __t, __urng, __p); }
187 template<
typename _UniformRandomNumberGenerator>
189 __generate(result_type* __f, result_type* __t,
190 _UniformRandomNumberGenerator& __urng,
192 { this->__generate_impl(__f, __t, __urng, __p); }
201 {
return __d1._M_param == __d2._M_param; }
204 template<
typename _ForwardIterator,
205 typename _UniformRandomNumberGenerator>
207 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
208 _UniformRandomNumberGenerator& __urng,
214 template<
typename _IntType>
215 template<
typename _UniformRandomNumberGenerator>
218 operator()(_UniformRandomNumberGenerator& __urng,
221 typedef typename _UniformRandomNumberGenerator::result_type
223 typedef typename std::make_unsigned<result_type>::type __utype;
224 typedef typename std::common_type<_Gresult_type, __utype>::type
227 const __uctype __urngmin = __urng.min();
228 const __uctype __urngmax = __urng.max();
229 const __uctype __urngrange = __urngmax - __urngmin;
230 const __uctype __urange
231 = __uctype(__param.b()) - __uctype(__param.a());
235 if (__urngrange > __urange)
238 const __uctype __uerange = __urange + 1;
239 const __uctype __scaling = __urngrange / __uerange;
240 const __uctype __past = __uerange * __scaling;
242 __ret = __uctype(__urng()) - __urngmin;
243 while (__ret >= __past);
246 else if (__urngrange < __urange)
266 const __uctype __uerngrange = __urngrange + 1;
267 __tmp = (__uerngrange * operator()
268 (__urng,
param_type(0, __urange / __uerngrange)));
269 __ret = __tmp + (__uctype(__urng()) - __urngmin);
271 while (__ret > __urange || __ret < __tmp);
274 __ret = __uctype(__urng()) - __urngmin;
276 return __ret + __param.a();
280 template<
typename _IntType>
281 template<
typename _ForwardIterator,
282 typename _UniformRandomNumberGenerator>
286 _UniformRandomNumberGenerator& __urng,
289 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
290 typedef typename _UniformRandomNumberGenerator::result_type
292 typedef typename std::make_unsigned<result_type>::type __utype;
293 typedef typename std::common_type<_Gresult_type, __utype>::type
296 const __uctype __urngmin = __urng.min();
297 const __uctype __urngmax = __urng.max();
298 const __uctype __urngrange = __urngmax - __urngmin;
299 const __uctype __urange
300 = __uctype(__param.b()) - __uctype(__param.a());
304 if (__urngrange > __urange)
306 if (__detail::_Power_of_2(__urngrange + 1)
307 && __detail::_Power_of_2(__urange + 1))
311 __ret = __uctype(__urng()) - __urngmin;
312 *__f++ = (__ret & __urange) + __param.a();
318 const __uctype __uerange = __urange + 1;
319 const __uctype __scaling = __urngrange / __uerange;
320 const __uctype __past = __uerange * __scaling;
324 __ret = __uctype(__urng()) - __urngmin;
325 while (__ret >= __past);
326 *__f++ = __ret / __scaling + __param.a();
330 else if (__urngrange < __urange)
352 const __uctype __uerngrange = __urngrange + 1;
353 __tmp = (__uerngrange * operator()
354 (__urng,
param_type(0, __urange / __uerngrange)));
355 __ret = __tmp + (__uctype(__urng()) - __urngmin);
357 while (__ret > __urange || __ret < __tmp);
363 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
366 _GLIBCXX_END_NAMESPACE_VERSION
result_type min() const
Returns the inclusive lower bound of the distribution range.
param_type param() const
Returns the parameter set of the distribution.
result_type max() const
Returns the inclusive upper bound of the distribution range.
uniform_int_distribution(_IntType __a=0, _IntType __b=std::numeric_limits< _IntType >::max())
Constructs a uniform distribution object.
void param(const param_type &__param)
Sets the parameter set of the distribution.
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 discrete distribution for random numbers.
Properties of fundamental types.
void reset()
Resets the distribution state.
ISO C++ entities toplevel namespace is std.
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.