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; };
101#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
103 struct _Select_uint_least_t<__s, 1>
104 { __extension__
using type =
unsigned __int128; };
105#elif __has_builtin(__builtin_add_overflow) \
106 && __has_builtin(__builtin_sub_overflow) \
107 && defined __UINT64_TYPE__
109 struct _Select_uint_least_t<__s, 1>;
123 template<
typename _IntType =
int>
127 "template argument must be an integral type");
137 param_type() : param_type(0) { }
140 param_type(_IntType __a,
141 _IntType __b = __gnu_cxx::__int_traits<_IntType>::__max)
142 : _M_a(__a), _M_b(__b)
144 __glibcxx_assert(_M_a <= _M_b);
156 operator==(
const param_type& __p1,
const param_type& __p2)
157 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
160 operator!=(
const param_type& __p1,
const param_type& __p2)
161 {
return !(__p1 == __p2); }
180 = __gnu_cxx::__int_traits<_IntType>::__max)
199 {
return _M_param.a(); }
203 {
return _M_param.b(); }
218 { _M_param = __param; }
225 {
return this->a(); }
232 {
return this->b(); }
237 template<
typename _UniformRandomBitGenerator>
240 {
return this->
operator()(__urng, _M_param); }
242 template<
typename _UniformRandomBitGenerator>
244 operator()(_UniformRandomBitGenerator& __urng,
245 const param_type& __p);
247 template<
typename _ForwardIterator,
248 typename _UniformRandomBitGenerator>
250 __generate(_ForwardIterator __f, _ForwardIterator __t,
251 _UniformRandomBitGenerator& __urng)
252 { this->__generate(__f, __t, __urng, _M_param); }
254 template<
typename _ForwardIterator,
255 typename _UniformRandomBitGenerator>
257 __generate(_ForwardIterator __f, _ForwardIterator __t,
258 _UniformRandomBitGenerator& __urng,
259 const param_type& __p)
260 { this->__generate_impl(__f, __t, __urng, __p); }
262 template<
typename _UniformRandomBitGenerator>
264 __generate(result_type* __f, result_type* __t,
265 _UniformRandomBitGenerator& __urng,
266 const param_type& __p)
267 { this->__generate_impl(__f, __t, __urng, __p); }
276 {
return __d1._M_param == __d2._M_param; }
279 template<
typename _ForwardIterator,
280 typename _UniformRandomBitGenerator>
282 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
283 _UniformRandomBitGenerator& __urng,
284 const param_type& __p);
291 template<
size_t _Bits,
typename _Urbg>
292 static typename __detail::_Select_uint_least_t<_Bits>::type
293 _S_nd(_Urbg& __g,
typename __detail::_Select_uint_least_t<_Bits>::type __range)
295 using _Up =
typename __detail::_Select_uint_least_t<_Bits>::type;
296 using _Wp =
typename __detail::_Select_uint_least_t<2 * _Bits>::type;
299 constexpr auto __min = _Urbg::min();
300 constexpr _Up __mask = (_Bits < _Up_traits::__digits)
301 ? (_Up(1) << _Bits) - 1 : ~_Up(0);
306 _Wp __product = _Wp(__g() - __min) * _Wp(__range);
307 _Up __low = _Up(__product) & __mask;
310 const _Up __threshold = -__range % __range;
311 while (__low < __threshold)
313 __product = _Wp(__g() - __min) * _Wp(__range);
314 __low = _Up(__product) & __mask;
320 const _Up __back_threshold = __mask - __threshold;
321 if (__low <= __back_threshold)
324 __product = _Wp(__g() - __min) * _Wp(__range);
325 __low = _Up(__product) & __mask;
328 return _Up(__product >> _Bits) & __mask;
332 template<
typename _IntType>
333 template<
typename _UniformRandomBitGenerator>
336 operator()(_UniformRandomBitGenerator& __urng,
337 const param_type& __param)
339 typedef decltype(__urng()) _Gresult_type;
340 typedef typename make_unsigned<result_type>::type __utype;
343 constexpr __uctype __urngmin = _UniformRandomBitGenerator::min();
344 constexpr __uctype __urngmax = _UniformRandomBitGenerator::max();
345 static_assert( __urngmin < __urngmax,
346 "Uniform random bit generator must define min() < max()");
347 constexpr __uctype __urngrange = __urngmax - __urngmin;
349 const __uctype __urange
350 = __uctype(__param.b()) - __uctype(__param.a());
353 if (__urngrange > __urange)
357 const __uctype __uerange = __urange + 1;
359#pragma GCC diagnostic push
360#pragma GCC diagnostic ignored "-Wc++17-extensions"
361#if __has_builtin(__builtin_popcountg)
362 constexpr auto __bits = __builtin_popcountg(__urngrange);
363 if constexpr (__detail::_Power_of_2(__urngrange + 1) && __bits <= 32)
367 __UINT32_TYPE__ __u32erange = __uerange;
368 __ret = _S_nd<__bits>(__urng, __u32erange);
370# if __SIZEOF_INT128__
371 else if constexpr (__detail::_Power_of_2(__urngrange + 1) && __bits <= 64)
375 __UINT64_TYPE__ __u64erange = __uerange;
376 __ret = _S_nd<__bits>(__urng, __u64erange);
383 const __uctype __scaling = __urngrange / __uerange;
384 const __uctype __past = __uerange * __scaling;
386 __ret = __uctype(__urng()) - __urngmin;
387 while (__ret >= __past);
390#pragma GCC diagnostic pop
392 else if (__urngrange < __urange)
412 const __uctype __uerngrange = __urngrange + 1;
413 __tmp = (__uerngrange * operator()
414 (__urng, param_type(0, __urange / __uerngrange)));
415 __ret = __tmp + (__uctype(__urng()) - __urngmin);
417 while (__ret > __urange || __ret < __tmp);
420 __ret = __uctype(__urng()) - __urngmin;
422 return __ret + __param.a();
426 template<
typename _IntType>
427 template<
typename _ForwardIterator,
428 typename _UniformRandomBitGenerator>
432 _UniformRandomBitGenerator& __urng,
433 const param_type& __param)
435 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
436 typedef decltype(__urng()) _Gresult_type;
437 typedef typename make_unsigned<result_type>::type __utype;
440 static_assert( __urng.min() < __urng.max(),
441 "Uniform random bit generator must define min() < max()");
443 constexpr __uctype __urngmin = __urng.min();
444 constexpr __uctype __urngmax = __urng.max();
445 constexpr __uctype __urngrange = __urngmax - __urngmin;
446 const __uctype __urange
447 = __uctype(__param.b()) - __uctype(__param.a());
451 if (__urngrange > __urange)
453 if (__detail::_Power_of_2(__urngrange + 1)
454 && __detail::_Power_of_2(__urange + 1))
458 __ret = __uctype(__urng()) - __urngmin;
459 *__f++ = (__ret & __urange) + __param.a();
465 const __uctype __uerange = __urange + 1;
466 const __uctype __scaling = __urngrange / __uerange;
467 const __uctype __past = __uerange * __scaling;
471 __ret = __uctype(__urng()) - __urngmin;
472 while (__ret >= __past);
473 *__f++ = __ret / __scaling + __param.a();
477 else if (__urngrange < __urange)
499 constexpr __uctype __uerngrange = __urngrange + 1;
500 __tmp = (__uerngrange * operator()
501 (__urng, param_type(0, __urange / __uerngrange)));
502 __ret = __tmp + (__uctype(__urng()) - __urngmin);
504 while (__ret > __urange || __ret < __tmp);
510 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
515_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.