31 #ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H 32 #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H 37 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 template<
typename _Tp>
48 return ((__x - 1) & __x) == 0;
50 _GLIBCXX_END_NAMESPACE_VERSION
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<
typename _IntType =
int>
67 "template argument not an integral type");
80 : _M_a(__a), _M_b(__b)
82 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
95 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
127 {
return _M_param.a(); }
131 {
return _M_param.b(); }
146 { _M_param = __param; }
153 {
return this->a(); }
160 {
return this->b(); }
165 template<
typename _UniformRandomNumberGenerator>
168 {
return this->operator()(__urng, _M_param); }
170 template<
typename _UniformRandomNumberGenerator>
172 operator()(_UniformRandomNumberGenerator& __urng,
175 template<
typename _ForwardIterator,
176 typename _UniformRandomNumberGenerator>
178 __generate(_ForwardIterator __f, _ForwardIterator __t,
179 _UniformRandomNumberGenerator& __urng)
180 { this->__generate(__f, __t, __urng, _M_param); }
182 template<
typename _ForwardIterator,
183 typename _UniformRandomNumberGenerator>
185 __generate(_ForwardIterator __f, _ForwardIterator __t,
186 _UniformRandomNumberGenerator& __urng,
188 { this->__generate_impl(__f, __t, __urng, __p); }
190 template<
typename _UniformRandomNumberGenerator>
192 __generate(result_type* __f, result_type* __t,
193 _UniformRandomNumberGenerator& __urng,
195 { this->__generate_impl(__f, __t, __urng, __p); }
204 {
return __d1._M_param == __d2._M_param; }
207 template<
typename _ForwardIterator,
208 typename _UniformRandomNumberGenerator>
210 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
211 _UniformRandomNumberGenerator& __urng,
217 template<
typename _IntType>
218 template<
typename _UniformRandomNumberGenerator>
221 operator()(_UniformRandomNumberGenerator& __urng,
224 typedef typename _UniformRandomNumberGenerator::result_type
226 typedef typename std::make_unsigned<result_type>::type __utype;
227 typedef typename std::common_type<_Gresult_type, __utype>::type
230 const __uctype __urngmin = __urng.min();
231 const __uctype __urngmax = __urng.max();
232 const __uctype __urngrange = __urngmax - __urngmin;
233 const __uctype __urange
234 = __uctype(__param.b()) - __uctype(__param.a());
238 if (__urngrange > __urange)
241 const __uctype __uerange = __urange + 1;
242 const __uctype __scaling = __urngrange / __uerange;
243 const __uctype __past = __uerange * __scaling;
245 __ret = __uctype(__urng()) - __urngmin;
246 while (__ret >= __past);
249 else if (__urngrange < __urange)
269 const __uctype __uerngrange = __urngrange + 1;
270 __tmp = (__uerngrange * operator()
271 (__urng,
param_type(0, __urange / __uerngrange)));
272 __ret = __tmp + (__uctype(__urng()) - __urngmin);
274 while (__ret > __urange || __ret < __tmp);
277 __ret = __uctype(__urng()) - __urngmin;
279 return __ret + __param.a();
283 template<
typename _IntType>
284 template<
typename _ForwardIterator,
285 typename _UniformRandomNumberGenerator>
289 _UniformRandomNumberGenerator& __urng,
292 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
293 typedef typename _UniformRandomNumberGenerator::result_type
295 typedef typename std::make_unsigned<result_type>::type __utype;
296 typedef typename std::common_type<_Gresult_type, __utype>::type
299 const __uctype __urngmin = __urng.min();
300 const __uctype __urngmax = __urng.max();
301 const __uctype __urngrange = __urngmax - __urngmin;
302 const __uctype __urange
303 = __uctype(__param.b()) - __uctype(__param.a());
307 if (__urngrange > __urange)
309 if (__detail::_Power_of_2(__urngrange + 1)
310 && __detail::_Power_of_2(__urange + 1))
314 __ret = __uctype(__urng()) - __urngmin;
315 *__f++ = (__ret & __urange) + __param.a();
321 const __uctype __uerange = __urange + 1;
322 const __uctype __scaling = __urngrange / __uerange;
323 const __uctype __past = __uerange * __scaling;
327 __ret = __uctype(__urng()) - __urngmin;
328 while (__ret >= __past);
329 *__f++ = __ret / __scaling + __param.a();
333 else if (__urngrange < __urange)
355 const __uctype __uerngrange = __urngrange + 1;
356 __tmp = (__uerngrange * operator()
357 (__urng,
param_type(0, __urange / __uerngrange)));
358 __ret = __tmp + (__uctype(__urng()) - __urngmin);
360 while (__ret > __urange || __ret < __tmp);
366 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
369 _GLIBCXX_END_NAMESPACE_VERSION
uniform_int_distribution(_IntType __a=0, _IntType __b=std::numeric_limits< _IntType >::max())
Constructs a uniform distribution object.
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
friend bool operator==(const uniform_int_distribution &__d1, const uniform_int_distribution &__d2)
Return true if two uniform integer distributions have the same parameters.
void param(const param_type &__param)
Sets the parameter set of the distribution.
Uniform discrete distribution for random numbers.
ISO C++ entities toplevel namespace is std.
result_type max() const
Returns the inclusive upper bound of the distribution range.
param_type param() const
Returns the parameter set of the distribution.
Properties of fundamental types.
void reset()
Resets the distribution state.
result_type min() const
Returns the inclusive lower bound of the distribution range.