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>
53 concept uniform_random_bit_generator
54 = invocable<_Gen&> && unsigned_integral<invoke_result_t<_Gen&>>
57 { _Gen::min() } -> same_as<invoke_result_t<_Gen&>>;
58 { _Gen::max() } -> same_as<invoke_result_t<_Gen&>>;
59 requires bool_constant<(_Gen::min() < _Gen::max())>::value;
69 template<
typename _Tp>
73 return ((__x - 1) & __x) == 0;
87 template<
typename _IntType =
int>
91 "template argument must be an integral type");
101 param_type() : param_type(0) { }
104 param_type(_IntType __a,
105 _IntType __b = __gnu_cxx::__int_traits<_IntType>::__max)
106 : _M_a(__a), _M_b(__b)
108 __glibcxx_assert(_M_a <= _M_b);
120 operator==(
const param_type& __p1,
const param_type& __p2)
121 {
return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
124 operator!=(
const param_type& __p1,
const param_type& __p2)
125 {
return !(__p1 == __p2); }
144 = __gnu_cxx::__int_traits<_IntType>::__max)
163 {
return _M_param.a(); }
167 {
return _M_param.b(); }
182 { _M_param = __param; }
189 {
return this->a(); }
196 {
return this->b(); }
201 template<
typename _UniformRandomBitGenerator>
204 {
return this->
operator()(__urng, _M_param); }
206 template<
typename _UniformRandomBitGenerator>
208 operator()(_UniformRandomBitGenerator& __urng,
209 const param_type& __p);
211 template<
typename _ForwardIterator,
212 typename _UniformRandomBitGenerator>
214 __generate(_ForwardIterator __f, _ForwardIterator __t,
215 _UniformRandomBitGenerator& __urng)
216 { this->__generate(__f, __t, __urng, _M_param); }
218 template<
typename _ForwardIterator,
219 typename _UniformRandomBitGenerator>
221 __generate(_ForwardIterator __f, _ForwardIterator __t,
222 _UniformRandomBitGenerator& __urng,
223 const param_type& __p)
224 { this->__generate_impl(__f, __t, __urng, __p); }
226 template<
typename _UniformRandomBitGenerator>
229 _UniformRandomBitGenerator& __urng,
231 { this->__generate_impl(__f, __t, __urng, __p); }
240 {
return __d1._M_param == __d2._M_param; }
243 template<
typename _ForwardIterator,
244 typename _UniformRandomBitGenerator>
246 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
247 _UniformRandomBitGenerator& __urng,
248 const param_type& __p);
255 template<
typename _Wp,
typename _Urbg,
typename _Up>
257 _S_nd(_Urbg& __g, _Up __range)
261 static_assert(!_Up_traits::__is_signed,
"U must be unsigned");
262 static_assert(!_Wp_traits::__is_signed,
"W must be unsigned");
263 static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
264 "W must be twice as wide as U");
265 constexpr auto __min = _Urbg::min();
270 _Wp __product = _Wp(__g() - __min) * _Wp(__range);
271 _Up __low = _Up(__product);
274 _Up __threshold = -__range % __range;
275 while (__low < __threshold)
277 __product = _Wp(__g() - __min) * _Wp(__range);
278 __low = _Up(__product);
281 return __product >> _Up_traits::__digits;
285 template<
typename _IntType>
286 template<
typename _UniformRandomBitGenerator>
289 operator()(_UniformRandomBitGenerator& __urng,
290 const param_type& __param)
292 typedef typename _UniformRandomBitGenerator::result_type _Gresult_type;
293 typedef typename make_unsigned<result_type>::type __utype;
296 constexpr __uctype __urngmin = _UniformRandomBitGenerator::min();
297 constexpr __uctype __urngmax = _UniformRandomBitGenerator::max();
298 static_assert( __urngmin < __urngmax,
299 "Uniform random bit generator must define min() < max()");
300 constexpr __uctype __urngrange = __urngmax - __urngmin;
302 const __uctype __urange
303 = __uctype(__param.b()) - __uctype(__param.a());
306 if (__urngrange > __urange)
310 const __uctype __uerange = __urange + 1;
312#if defined __UINT64_TYPE__ && defined __UINT32_TYPE__
314 if _GLIBCXX17_CONSTEXPR (__urngrange == __UINT64_MAX__)
318 __UINT64_TYPE__ __u64erange = __uerange;
319 __ret = __extension__ _S_nd<unsigned __int128>(__urng,
324 if _GLIBCXX17_CONSTEXPR (__urngrange == __UINT32_MAX__)
328 __UINT32_TYPE__ __u32erange = __uerange;
329 __ret = _S_nd<__UINT64_TYPE__>(__urng, __u32erange);
335 const __uctype __scaling = __urngrange / __uerange;
336 const __uctype __past = __uerange * __scaling;
338 __ret = __uctype(__urng()) - __urngmin;
339 while (__ret >= __past);
343 else if (__urngrange < __urange)
363 const __uctype __uerngrange = __urngrange + 1;
364 __tmp = (__uerngrange * operator()
365 (__urng, param_type(0, __urange / __uerngrange)));
366 __ret = __tmp + (__uctype(__urng()) - __urngmin);
368 while (__ret > __urange || __ret < __tmp);
371 __ret = __uctype(__urng()) - __urngmin;
373 return __ret + __param.a();
377 template<
typename _IntType>
378 template<
typename _ForwardIterator,
379 typename _UniformRandomBitGenerator>
383 _UniformRandomBitGenerator& __urng,
384 const param_type& __param)
386 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
387 typedef typename _UniformRandomBitGenerator::result_type _Gresult_type;
388 typedef typename make_unsigned<result_type>::type __utype;
391 static_assert( __urng.min() < __urng.max(),
392 "Uniform random bit generator must define min() < max()");
394 constexpr __uctype __urngmin = __urng.min();
395 constexpr __uctype __urngmax = __urng.max();
396 constexpr __uctype __urngrange = __urngmax - __urngmin;
397 const __uctype __urange
398 = __uctype(__param.b()) - __uctype(__param.a());
402 if (__urngrange > __urange)
404 if (__detail::_Power_of_2(__urngrange + 1)
405 && __detail::_Power_of_2(__urange + 1))
409 __ret = __uctype(__urng()) - __urngmin;
410 *__f++ = (__ret & __urange) + __param.a();
416 const __uctype __uerange = __urange + 1;
417 const __uctype __scaling = __urngrange / __uerange;
418 const __uctype __past = __uerange * __scaling;
422 __ret = __uctype(__urng()) - __urngmin;
423 while (__ret >= __past);
424 *__f++ = __ret / __scaling + __param.a();
428 else if (__urngrange < __urange)
450 constexpr __uctype __uerngrange = __urngrange + 1;
451 __tmp = (__uerngrange * operator()
452 (__urng, param_type(0, __urange / __uerngrange)));
453 __ret = __tmp + (__uctype(__urng()) - __urngmin);
455 while (__ret > __urange || __ret < __tmp);
461 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
466_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.