31 #ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H
32 #define _GLIBCXX_BITS_UNIFORM_INT_DIST_H
36 #if __cplusplus > 201703L
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 #ifdef __cpp_lib_concepts
46 template<
typename _Gen>
47 concept uniform_random_bit_generator
48 = invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>>
51 {
_Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
52 {
_Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
60 template<
typename _Tp>
64 return ((__x - 1) & __x) == 0;
73 template<
typename _IntType =
int>
77 "template argument must be an integral type");
92 : _M_a(__a), _M_b(__b)
94 __glibcxx_assert(_M_a <= _M_b);
107 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
111 {
return !(__p1 == __p2); }
148 {
return _M_param.a(); }
152 {
return _M_param.b(); }
167 { _M_param = __param; }
174 {
return this->a(); }
181 {
return this->b(); }
186 template<
typename _UniformRandomNumberGenerator>
189 {
return this->
operator()(__urng, _M_param); }
191 template<
typename _UniformRandomNumberGenerator>
193 operator()(_UniformRandomNumberGenerator& __urng,
194 const param_type& __p);
196 template<
typename _ForwardIterator,
197 typename _UniformRandomNumberGenerator>
199 __generate(_ForwardIterator __f, _ForwardIterator __t,
200 _UniformRandomNumberGenerator& __urng)
201 { this->__generate(__f, __t, __urng, _M_param); }
203 template<
typename _ForwardIterator,
204 typename _UniformRandomNumberGenerator>
206 __generate(_ForwardIterator __f, _ForwardIterator __t,
207 _UniformRandomNumberGenerator& __urng,
208 const param_type& __p)
209 { this->__generate_impl(__f, __t, __urng, __p); }
211 template<
typename _UniformRandomNumberGenerator>
214 _UniformRandomNumberGenerator& __urng,
215 const param_type& __p)
216 { this->__generate_impl(__f, __t, __urng, __p); }
225 {
return __d1._M_param == __d2._M_param; }
228 template<
typename _ForwardIterator,
229 typename _UniformRandomNumberGenerator>
231 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
232 _UniformRandomNumberGenerator& __urng,
233 const param_type& __p);
238 template<
typename _IntType>
239 template<
typename _UniformRandomNumberGenerator>
242 operator()(_UniformRandomNumberGenerator& __urng,
243 const param_type& __param)
245 typedef typename _UniformRandomNumberGenerator::result_type
247 typedef typename std::make_unsigned<result_type>::type __utype;
251 const __uctype __urngmin = __urng.min();
252 const __uctype __urngmax = __urng.max();
253 const __uctype __urngrange = __urngmax - __urngmin;
254 const __uctype __urange
255 = __uctype(__param.b()) - __uctype(__param.a());
259 if (__urngrange > __urange)
262 const __uctype __uerange = __urange + 1;
263 const __uctype __scaling = __urngrange / __uerange;
264 const __uctype __past = __uerange * __scaling;
266 __ret = __uctype(__urng()) - __urngmin;
267 while (__ret >= __past);
270 else if (__urngrange < __urange)
290 const __uctype __uerngrange = __urngrange + 1;
291 __tmp = (__uerngrange * operator()
292 (__urng, param_type(0, __urange / __uerngrange)));
293 __ret = __tmp + (__uctype(__urng()) - __urngmin);
295 while (__ret > __urange || __ret < __tmp);
298 __ret = __uctype(__urng()) - __urngmin;
300 return __ret + __param.a();
304 template<
typename _IntType>
305 template<
typename _ForwardIterator,
306 typename _UniformRandomNumberGenerator>
308 uniform_int_distribution<_IntType>::
309 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
310 _UniformRandomNumberGenerator& __urng,
311 const param_type& __param)
313 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
314 typedef typename _UniformRandomNumberGenerator::result_type
316 typedef typename std::make_unsigned<result_type>::type __utype;
320 const __uctype __urngmin = __urng.min();
321 const __uctype __urngmax = __urng.max();
322 const __uctype __urngrange = __urngmax - __urngmin;
323 const __uctype __urange
324 = __uctype(__param.b()) - __uctype(__param.a());
328 if (__urngrange > __urange)
330 if (__detail::_Power_of_2(__urngrange + 1)
331 && __detail::_Power_of_2(__urange + 1))
335 __ret = __uctype(__urng()) - __urngmin;
336 *__f++ = (__ret & __urange) + __param.a();
342 const __uctype __uerange = __urange + 1;
343 const __uctype __scaling = __urngrange / __uerange;
344 const __uctype __past = __uerange * __scaling;
348 __ret = __uctype(__urng()) - __urngmin;
349 while (__ret >= __past);
350 *__f++ = __ret / __scaling + __param.a();
354 else if (__urngrange < __urange)
376 const __uctype __uerngrange = __urngrange + 1;
377 __tmp = (__uerngrange * operator()
378 (__urng, param_type(0, __urange / __uerngrange)));
379 __ret = __tmp + (__uctype(__urng()) - __urngmin);
381 while (__ret > __urange || __ret < __tmp);
387 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
392 _GLIBCXX_END_NAMESPACE_VERSION
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
Properties of fundamental types.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...
void reset()
Resets the distribution state.
uniform_int_distribution()
Constructs a uniform distribution object.
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
void param(const param_type &__param)
Sets the parameter set of the distribution.
result_type min() const
Returns the inclusive lower bound of the distribution range.
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(_IntType __a, _IntType __b=numeric_limits< _IntType >::max())
Constructs a uniform distribution object.
result_type max() const
Returns the inclusive upper bound of the distribution range.
param_type param() const
Returns the parameter set of the distribution.