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");
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; }
93 {
return !(__p1 == __p2); }
125 {
return _M_param.a(); }
129 {
return _M_param.b(); }
144 { _M_param = __param; }
151 {
return this->a(); }
158 {
return this->b(); }
163 template<
typename _UniformRandomNumberGenerator>
166 {
return this->
operator()(__urng, _M_param); }
168 template<
typename _UniformRandomNumberGenerator>
170 operator()(_UniformRandomNumberGenerator& __urng,
171 const param_type& __p);
173 template<
typename _ForwardIterator,
174 typename _UniformRandomNumberGenerator>
176 __generate(_ForwardIterator __f, _ForwardIterator __t,
177 _UniformRandomNumberGenerator& __urng)
178 { this->__generate(__f, __t, __urng, _M_param); }
180 template<
typename _ForwardIterator,
181 typename _UniformRandomNumberGenerator>
183 __generate(_ForwardIterator __f, _ForwardIterator __t,
184 _UniformRandomNumberGenerator& __urng,
185 const param_type& __p)
186 { this->__generate_impl(__f, __t, __urng, __p); }
188 template<
typename _UniformRandomNumberGenerator>
191 _UniformRandomNumberGenerator& __urng,
192 const param_type& __p)
193 { this->__generate_impl(__f, __t, __urng, __p); }
202 {
return __d1._M_param == __d2._M_param; }
205 template<
typename _ForwardIterator,
206 typename _UniformRandomNumberGenerator>
208 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
209 _UniformRandomNumberGenerator& __urng,
210 const param_type& __p);
215 template<
typename _IntType>
216 template<
typename _UniformRandomNumberGenerator>
219 operator()(_UniformRandomNumberGenerator& __urng,
220 const param_type& __param)
222 typedef typename _UniformRandomNumberGenerator::result_type
224 typedef typename std::make_unsigned<result_type>::type __utype;
228 const __uctype __urngmin = __urng.min();
229 const __uctype __urngmax = __urng.max();
230 const __uctype __urngrange = __urngmax - __urngmin;
231 const __uctype __urange
232 = __uctype(__param.b()) - __uctype(__param.a());
236 if (__urngrange > __urange)
239 const __uctype __uerange = __urange + 1;
240 const __uctype __scaling = __urngrange / __uerange;
241 const __uctype __past = __uerange * __scaling;
243 __ret = __uctype(__urng()) - __urngmin;
244 while (__ret >= __past);
247 else if (__urngrange < __urange)
267 const __uctype __uerngrange = __urngrange + 1;
268 __tmp = (__uerngrange * operator()
269 (__urng, param_type(0, __urange / __uerngrange)));
270 __ret = __tmp + (__uctype(__urng()) - __urngmin);
272 while (__ret > __urange || __ret < __tmp);
275 __ret = __uctype(__urng()) - __urngmin;
277 return __ret + __param.a();
281 template<
typename _IntType>
282 template<
typename _ForwardIterator,
283 typename _UniformRandomNumberGenerator>
285 uniform_int_distribution<_IntType>::
286 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
287 _UniformRandomNumberGenerator& __urng,
288 const param_type& __param)
290 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
291 typedef typename _UniformRandomNumberGenerator::result_type
293 typedef typename std::make_unsigned<result_type>::type __utype;
297 const __uctype __urngmin = __urng.min();
298 const __uctype __urngmax = __urng.max();
299 const __uctype __urngrange = __urngmax - __urngmin;
300 const __uctype __urange
301 = __uctype(__param.b()) - __uctype(__param.a());
305 if (__urngrange > __urange)
307 if (__detail::_Power_of_2(__urngrange + 1)
308 && __detail::_Power_of_2(__urange + 1))
312 __ret = __uctype(__urng()) - __urngmin;
313 *__f++ = (__ret & __urange) + __param.a();
319 const __uctype __uerange = __urange + 1;
320 const __uctype __scaling = __urngrange / __uerange;
321 const __uctype __past = __uerange * __scaling;
325 __ret = __uctype(__urng()) - __urngmin;
326 while (__ret >= __past);
327 *__f++ = __ret / __scaling + __param.a();
331 else if (__urngrange < __urange)
353 const __uctype __uerngrange = __urngrange + 1;
354 __tmp = (__uerngrange * operator()
355 (__urng, param_type(0, __urange / __uerngrange)));
356 __ret = __tmp + (__uctype(__urng()) - __urngmin);
358 while (__ret > __urange || __ret < __tmp);
364 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
369 _GLIBCXX_END_NAMESPACE_VERSION
void reset()
Resets the distribution state.
result_type min() const
Returns the inclusive lower bound of the distribution range.
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.
Properties of fundamental types.
result_type max() const
Returns the inclusive upper bound of the distribution range.
ISO C++ entities toplevel namespace is std.
void param(const param_type &__param)
Sets the parameter set of the distribution.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...
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.