31#ifndef _GLIBCXX_BITS_UNIFORM_INT_DIST_H
32#define _GLIBCXX_BITS_UNIFORM_INT_DIST_H
36#if __cplusplus > 201703L
41namespace std _GLIBCXX_VISIBILITY(default)
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
45#ifdef __cpp_lib_concepts
47 template<
typename _Gen>
48 concept uniform_random_bit_generator
49 = invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>>
52 { _Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
53 { _Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
63 template<
typename _Tp>
67 return ((__x - 1) & __x) == 0;
76 template<
typename _IntType =
int>
80 "template argument must be an integral type");
95 : _M_a(__a), _M_b(__b)
97 __glibcxx_assert(_M_a <= _M_b);
110 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
114 {
return !(__p1 == __p2); }
152 {
return _M_param.a(); }
156 {
return _M_param.b(); }
171 { _M_param = __param; }
178 {
return this->a(); }
185 {
return this->b(); }
190 template<
typename _UniformRandomBitGenerator>
193 {
return this->
operator()(__urng, _M_param); }
195 template<
typename _UniformRandomBitGenerator>
197 operator()(_UniformRandomBitGenerator& __urng,
198 const param_type& __p);
200 template<
typename _ForwardIterator,
201 typename _UniformRandomBitGenerator>
203 __generate(_ForwardIterator __f, _ForwardIterator __t,
204 _UniformRandomBitGenerator& __urng)
205 { this->__generate(__f, __t, __urng, _M_param); }
207 template<
typename _ForwardIterator,
208 typename _UniformRandomBitGenerator>
210 __generate(_ForwardIterator __f, _ForwardIterator __t,
211 _UniformRandomBitGenerator& __urng,
212 const param_type& __p)
213 { this->__generate_impl(__f, __t, __urng, __p); }
215 template<
typename _UniformRandomBitGenerator>
218 _UniformRandomBitGenerator& __urng,
219 const param_type& __p)
220 { this->__generate_impl(__f, __t, __urng, __p); }
229 {
return __d1._M_param == __d2._M_param; }
232 template<
typename _ForwardIterator,
233 typename _UniformRandomBitGenerator>
235 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
236 _UniformRandomBitGenerator& __urng,
237 const param_type& __p);
244 template<
typename _Wp,
typename _Urbg,
typename _Up>
246 _S_nd(_Urbg& __g, _Up __range)
250 static_assert(!_Up_traits::__is_signed,
"U must be unsigned");
251 static_assert(!_Wp_traits::__is_signed,
"W must be unsigned");
252 static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
253 "W must be twice as wide as U");
258 _Wp __product = _Wp(__g()) * _Wp(__range);
259 _Up __low = _Up(__product);
262 _Up __threshold = -__range % __range;
263 while (__low < __threshold)
265 __product = _Wp(__g()) * _Wp(__range);
266 __low = _Up(__product);
269 return __product >> _Up_traits::__digits;
273 template<
typename _IntType>
274 template<
typename _UniformRandomBitGenerator>
277 operator()(_UniformRandomBitGenerator& __urng,
278 const param_type& __param)
280 typedef typename _UniformRandomBitGenerator::result_type _Gresult_type;
281 typedef typename make_unsigned<result_type>::type __utype;
282 typedef typename common_type<_Gresult_type, __utype>::type __uctype;
284 constexpr __uctype __urngmin = _UniformRandomBitGenerator::min();
285 constexpr __uctype __urngmax = _UniformRandomBitGenerator::max();
286 static_assert( __urngmin < __urngmax,
287 "Uniform random bit generator must define min() < max()");
288 constexpr __uctype __urngrange = __urngmax - __urngmin;
290 const __uctype __urange
291 = __uctype(__param.b()) - __uctype(__param.a());
294 if (__urngrange > __urange)
298 const __uctype __uerange = __urange + 1;
300#if defined __UINT64_TYPE__ && defined __UINT32_TYPE__
302 if _GLIBCXX17_CONSTEXPR (__urngrange == __UINT64_MAX__)
306 __UINT64_TYPE__ __u64erange = __uerange;
307 __ret = _S_nd<unsigned __int128>(__urng, __u64erange);
311 if _GLIBCXX17_CONSTEXPR (__urngrange == __UINT32_MAX__)
315 __UINT32_TYPE__ __u32erange = __uerange;
316 __ret = _S_nd<__UINT64_TYPE__>(__urng, __u32erange);
322 const __uctype __scaling = __urngrange / __uerange;
323 const __uctype __past = __uerange * __scaling;
325 __ret = __uctype(__urng()) - __urngmin;
326 while (__ret >= __past);
330 else if (__urngrange < __urange)
350 const __uctype __uerngrange = __urngrange + 1;
351 __tmp = (__uerngrange * operator()
352 (__urng, param_type(0, __urange / __uerngrange)));
353 __ret = __tmp + (__uctype(__urng()) - __urngmin);
355 while (__ret > __urange || __ret < __tmp);
358 __ret = __uctype(__urng()) - __urngmin;
360 return __ret + __param.a();
364 template<
typename _IntType>
365 template<
typename _ForwardIterator,
366 typename _UniformRandomBitGenerator>
368 uniform_int_distribution<_IntType>::
369 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
370 _UniformRandomBitGenerator& __urng,
371 const param_type& __param)
373 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
374 typedef typename _UniformRandomBitGenerator::result_type _Gresult_type;
375 typedef typename make_unsigned<result_type>::type __utype;
376 typedef typename common_type<_Gresult_type, __utype>::type __uctype;
378 static_assert( __urng.min() < __urng.max(),
379 "Uniform random bit generator must define min() < max()");
381 constexpr __uctype __urngmin = __urng.min();
382 constexpr __uctype __urngmax = __urng.max();
383 constexpr __uctype __urngrange = __urngmax - __urngmin;
384 const __uctype __urange
385 = __uctype(__param.b()) - __uctype(__param.a());
389 if (__urngrange > __urange)
391 if (__detail::_Power_of_2(__urngrange + 1)
392 && __detail::_Power_of_2(__urange + 1))
396 __ret = __uctype(__urng()) - __urngmin;
397 *__f++ = (__ret & __urange) + __param.a();
403 const __uctype __uerange = __urange + 1;
404 const __uctype __scaling = __urngrange / __uerange;
405 const __uctype __past = __uerange * __scaling;
409 __ret = __uctype(__urng()) - __urngmin;
410 while (__ret >= __past);
411 *__f++ = __ret / __scaling + __param.a();
415 else if (__urngrange < __urange)
437 constexpr __uctype __uerngrange = __urngrange + 1;
438 __tmp = (__uerngrange * operator()
439 (__urng, param_type(0, __urange / __uerngrange)));
440 __ret = __tmp + (__uctype(__urng()) - __urngmin);
442 while (__ret > __urange || __ret < __tmp);
448 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
453_GLIBCXX_END_NAMESPACE_VERSION
integral_constant< bool, __v > bool_constant
Alias template for compile-time boolean constant types.
ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
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.
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.
result_type max() const
Returns the inclusive upper bound of the distribution range.
result_type operator()(_UniformRandomBitGenerator &__urng)
Generating functions.
uniform_int_distribution(_IntType __a, _IntType __b=__gnu_cxx::__int_traits< _IntType >::__max)
Constructs a uniform distribution object.
param_type param() const
Returns the parameter set of the distribution.