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
52 template<
typename _Gen>
59 requires bool_constant<(_Gen::min() < _Gen::max())>::value;
69 template<
typename _Tp>
73 return ((__x - 1) & __x) == 0;
77 int __which = ((__s <= __CHAR_BIT__ *
sizeof (int))
78 + (__s <= __CHAR_BIT__ *
sizeof (long))
79 + (__s <= __CHAR_BIT__ *
sizeof (
long long))
82 struct _Select_uint_least_t
84 static_assert(__which < 0,
85 "sorry, would be too much trouble for a slow result");
89 struct _Select_uint_least_t<__s, 4>
90 {
using type =
unsigned int; };
93 struct _Select_uint_least_t<__s, 3>
94 {
using type =
unsigned long; };
97 struct _Select_uint_least_t<__s, 2>
98 {
using type =
unsigned long long; };
100#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
102 struct _Select_uint_least_t<__s, 1>
103 { __extension__
using type =
unsigned __int128; };
104#elif __has_builtin(__builtin_add_overflow) \
105 && __has_builtin(__builtin_sub_overflow) \
106 && defined __UINT64_TYPE__
108 struct _Select_uint_least_t<__s, 1>;
122 template<
typename _IntType =
int>
126 "template argument must be an integral type");
136 param_type() : param_type(0) { }
139 param_type(_IntType __a,
140 _IntType __b = __gnu_cxx::__int_traits<_IntType>::__max)
141 : _M_a(__a), _M_b(__b)
143 __glibcxx_assert(_M_a <= _M_b);
155 operator==(
const param_type& __p1,
const param_type& __p2)
156 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
159 operator!=(
const param_type& __p1,
const param_type& __p2)
160 {
return !(__p1 == __p2); }
179 = __gnu_cxx::__int_traits<_IntType>::__max)
198 {
return _M_param.a(); }
202 {
return _M_param.b(); }
217 { _M_param = __param; }
224 {
return this->a(); }
231 {
return this->b(); }
236 template<
typename _UniformRandomBitGenerator>
239 {
return this->
operator()(__urng, _M_param); }
241 template<
typename _UniformRandomBitGenerator>
243 operator()(_UniformRandomBitGenerator& __urng,
244 const param_type& __p);
246 template<
typename _ForwardIterator,
247 typename _UniformRandomBitGenerator>
249 __generate(_ForwardIterator __f, _ForwardIterator __t,
250 _UniformRandomBitGenerator& __urng)
251 { this->__generate(__f, __t, __urng, _M_param); }
253 template<
typename _ForwardIterator,
254 typename _UniformRandomBitGenerator>
256 __generate(_ForwardIterator __f, _ForwardIterator __t,
257 _UniformRandomBitGenerator& __urng,
258 const param_type& __p)
259 { this->__generate_impl(__f, __t, __urng, __p); }
261 template<
typename _UniformRandomBitGenerator>
263 __generate(result_type* __f, result_type* __t,
264 _UniformRandomBitGenerator& __urng,
265 const param_type& __p)
266 { this->__generate_impl(__f, __t, __urng, __p); }
275 {
return __d1._M_param == __d2._M_param; }
278 template<
typename _ForwardIterator,
279 typename _UniformRandomBitGenerator>
281 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
282 _UniformRandomBitGenerator& __urng,
283 const param_type& __p);
290 template<
size_t _Bits,
typename _Urbg>
291 static typename __detail::_Select_uint_least_t<_Bits>::type
292 _S_nd(_Urbg& __g,
typename __detail::_Select_uint_least_t<_Bits>::type __range)
294 using _Up =
typename __detail::_Select_uint_least_t<_Bits>::type;
295 using _Wp =
typename __detail::_Select_uint_least_t<2 * _Bits>::type;
298 constexpr auto __min = _Urbg::min();
299 constexpr _Up __mask = (_Bits < _Up_traits::__digits)
300 ? (_Up(1) << _Bits) - 1 : ~_Up(0);
305 _Wp __product = _Wp(__g() - __min) * _Wp(__range);
306 _Up __low = _Up(__product) & __mask;
309 const _Up __threshold = -__range % __range;
310 while (__low < __threshold)
312 __product = _Wp(__g() - __min) * _Wp(__range);
313 __low = _Up(__product) & __mask;
319 const _Up __back_threshold = __mask - __threshold;
320 if (__low <= __back_threshold)
323 __product = _Wp(__g() - __min) * _Wp(__range);
324 __low = _Up(__product) & __mask;
327 return _Up(__product >> _Bits) & __mask;
331 template<
typename _IntType>
332 template<
typename _UniformRandomBitGenerator>
335 operator()(_UniformRandomBitGenerator& __urng,
336 const param_type& __param)
338 typedef decltype(__urng()) _Gresult_type;
339 typedef typename make_unsigned<result_type>::type __utype;
342 constexpr __uctype __urngmin = _UniformRandomBitGenerator::min();
343 constexpr __uctype __urngmax = _UniformRandomBitGenerator::max();
344 static_assert( __urngmin < __urngmax,
345 "Uniform random bit generator must define min() < max()");
346 constexpr __uctype __urngrange = __urngmax - __urngmin;
348 const __uctype __urange
349 = __uctype(__param.b()) - __uctype(__param.a());
352 if (__urngrange > __urange)
356 const __uctype __uerange = __urange + 1;
358#pragma GCC diagnostic push
359#pragma GCC diagnostic ignored "-Wc++17-extensions"
360#if __has_builtin(__builtin_popcountg)
361 constexpr auto __bits = __builtin_popcountg(__urngrange);
362 if constexpr (__detail::_Power_of_2(__urngrange + 1) && __bits <= 32)
366 __UINT32_TYPE__ __u32erange = __uerange;
367 __ret = _S_nd<__bits>(__urng, __u32erange);
369# if __SIZEOF_INT128__
370 else if constexpr (__detail::_Power_of_2(__urngrange + 1) && __bits <= 64)
374 __UINT64_TYPE__ __u64erange = __uerange;
375 __ret = _S_nd<__bits>(__urng, __u64erange);
382 const __uctype __scaling = __urngrange / __uerange;
383 const __uctype __past = __uerange * __scaling;
385 __ret = __uctype(__urng()) - __urngmin;
386 while (__ret >= __past);
389#pragma GCC diagnostic pop
391 else if (__urngrange < __urange)
411 const __uctype __uerngrange = __urngrange + 1;
412 __tmp = (__uerngrange * operator()
413 (__urng, param_type(0, __urange / __uerngrange)));
414 __ret = __tmp + (__uctype(__urng()) - __urngmin);
416 while (__ret > __urange || __ret < __tmp);
419 __ret = __uctype(__urng()) - __urngmin;
421 return __ret + __param.a();
425 template<
typename _IntType>
426 template<
typename _ForwardIterator,
427 typename _UniformRandomBitGenerator>
431 _UniformRandomBitGenerator& __urng,
432 const param_type& __param)
434 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
435 typedef decltype(__urng()) _Gresult_type;
436 typedef typename make_unsigned<result_type>::type __utype;
439 static_assert( __urng.min() < __urng.max(),
440 "Uniform random bit generator must define min() < max()");
442 constexpr __uctype __urngmin = __urng.min();
443 constexpr __uctype __urngmax = __urng.max();
444 constexpr __uctype __urngrange = __urngmax - __urngmin;
445 const __uctype __urange
446 = __uctype(__param.b()) - __uctype(__param.a());
450 if (__urngrange > __urange)
452 if (__detail::_Power_of_2(__urngrange + 1)
453 && __detail::_Power_of_2(__urange + 1))
457 __ret = __uctype(__urng()) - __urngmin;
458 *__f++ = (__ret & __urange) + __param.a();
464 const __uctype __uerange = __urange + 1;
465 const __uctype __scaling = __urngrange / __uerange;
466 const __uctype __past = __uerange * __scaling;
470 __ret = __uctype(__urng()) - __urngmin;
471 while (__ret >= __past);
472 *__f++ = __ret / __scaling + __param.a();
476 else if (__urngrange < __urange)
498 constexpr __uctype __uerngrange = __urngrange + 1;
499 __tmp = (__uerngrange * operator()
500 (__urng, param_type(0, __urange / __uerngrange)));
501 __ret = __tmp + (__uctype(__urng()) - __urngmin);
503 while (__ret > __urange || __ret < __tmp);
509 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
514_GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
Implementation details not part of the namespace std interface.
__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.
[concept.same], concept same_as
[concept.invocable], concept invocable
Requirements for a uniform random bit generator.