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
60 template<
typename _IntType =
int>
64 "template argument must be an integral type");
77 : _M_a(__a), _M_b(__b)
79 __glibcxx_assert(_M_a <= _M_b);
92 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
96 {
return !(__p1 == __p2); }
128 {
return _M_param.a(); }
132 {
return _M_param.b(); }
147 { _M_param = __param; }
154 {
return this->a(); }
161 {
return this->b(); }
166 template<
typename _UniformRandomNumberGenerator>
169 {
return this->operator()(__urng, _M_param); }
171 template<
typename _UniformRandomNumberGenerator>
173 operator()(_UniformRandomNumberGenerator& __urng,
176 template<
typename _ForwardIterator,
177 typename _UniformRandomNumberGenerator>
179 __generate(_ForwardIterator __f, _ForwardIterator __t,
180 _UniformRandomNumberGenerator& __urng)
181 { this->__generate(__f, __t, __urng, _M_param); }
183 template<
typename _ForwardIterator,
184 typename _UniformRandomNumberGenerator>
186 __generate(_ForwardIterator __f, _ForwardIterator __t,
187 _UniformRandomNumberGenerator& __urng,
189 { this->__generate_impl(__f, __t, __urng, __p); }
191 template<
typename _UniformRandomNumberGenerator>
193 __generate(result_type* __f, result_type* __t,
194 _UniformRandomNumberGenerator& __urng,
196 { this->__generate_impl(__f, __t, __urng, __p); }
205 {
return __d1._M_param == __d2._M_param; }
208 template<
typename _ForwardIterator,
209 typename _UniformRandomNumberGenerator>
211 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
212 _UniformRandomNumberGenerator& __urng,
218 template<
typename _IntType>
219 template<
typename _UniformRandomNumberGenerator>
222 operator()(_UniformRandomNumberGenerator& __urng,
225 typedef typename _UniformRandomNumberGenerator::result_type
227 typedef typename std::make_unsigned<result_type>::type __utype;
228 typedef typename std::common_type<_Gresult_type, __utype>::type
231 const __uctype __urngmin = __urng.min();
232 const __uctype __urngmax = __urng.max();
233 const __uctype __urngrange = __urngmax - __urngmin;
234 const __uctype __urange
235 = __uctype(__param.b()) - __uctype(__param.a());
239 if (__urngrange > __urange)
242 const __uctype __uerange = __urange + 1;
243 const __uctype __scaling = __urngrange / __uerange;
244 const __uctype __past = __uerange * __scaling;
246 __ret = __uctype(__urng()) - __urngmin;
247 while (__ret >= __past);
250 else if (__urngrange < __urange)
270 const __uctype __uerngrange = __urngrange + 1;
271 __tmp = (__uerngrange * operator()
272 (__urng,
param_type(0, __urange / __uerngrange)));
273 __ret = __tmp + (__uctype(__urng()) - __urngmin);
275 while (__ret > __urange || __ret < __tmp);
278 __ret = __uctype(__urng()) - __urngmin;
280 return __ret + __param.a();
284 template<
typename _IntType>
285 template<
typename _ForwardIterator,
286 typename _UniformRandomNumberGenerator>
290 _UniformRandomNumberGenerator& __urng,
293 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
294 typedef typename _UniformRandomNumberGenerator::result_type
296 typedef typename std::make_unsigned<result_type>::type __utype;
297 typedef typename std::common_type<_Gresult_type, __utype>::type
300 const __uctype __urngmin = __urng.min();
301 const __uctype __urngmax = __urng.max();
302 const __uctype __urngrange = __urngmax - __urngmin;
303 const __uctype __urange
304 = __uctype(__param.b()) - __uctype(__param.a());
308 if (__urngrange > __urange)
310 if (__detail::_Power_of_2(__urngrange + 1)
311 && __detail::_Power_of_2(__urange + 1))
315 __ret = __uctype(__urng()) - __urngmin;
316 *__f++ = (__ret & __urange) + __param.a();
322 const __uctype __uerange = __urange + 1;
323 const __uctype __scaling = __urngrange / __uerange;
324 const __uctype __past = __uerange * __scaling;
328 __ret = __uctype(__urng()) - __urngmin;
329 while (__ret >= __past);
330 *__f++ = __ret / __scaling + __param.a();
334 else if (__urngrange < __urange)
356 const __uctype __uerngrange = __urngrange + 1;
357 __tmp = (__uerngrange * operator()
358 (__urng,
param_type(0, __urange / __uerngrange)));
359 __ret = __tmp + (__uctype(__urng()) - __urngmin);
361 while (__ret > __urange || __ret < __tmp);
367 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
372 _GLIBCXX_END_NAMESPACE_VERSION
uniform_int_distribution(_IntType __a=0, _IntType __b=std::numeric_limits< _IntType >::max())
Constructs a uniform distribution object.
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 max() const
Returns the inclusive upper bound of the distribution range.
ISO C++ entities toplevel namespace is std.
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 reset()
Resets the distribution state.
param_type param() const
Returns the parameter set of the distribution.
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
void param(const param_type &__param)
Sets the parameter set of the distribution.
Properties of fundamental types.