33 _GLIBCXX_BEGIN_NAMESPACE_TR1
49 template<
typename _UIntType,
int __w,
50 bool = __w < std::numeric_limits<_UIntType>::digits>
52 {
static const _UIntType __value = 0; };
54 template<
typename _UIntType,
int __w>
55 struct _Shift<_UIntType, __w, true>
56 {
static const _UIntType __value = _UIntType(1) << __w; };
58 template<
typename _Tp, _Tp __a, _Tp __c, _Tp __m,
bool>
63 template<
typename _Tp, _Tp __a, _Tp __c, _Tp __m>
66 {
return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
68 typedef __gnu_cxx::__conditional_type<(
sizeof(unsigned) == 4),
69 unsigned,
unsigned long>::__type _UInt32Type;
75 template<
typename _Engine,
typename _Distribution>
78 typedef typename remove_reference<_Engine>::type _BEngine;
79 typedef typename _BEngine::result_type _Engine_result_type;
80 typedef typename _Distribution::input_type result_type;
83 _Adaptor(
const _Engine& __g)
89 result_type __return_value;
90 if (is_integral<_Engine_result_type>::value
91 && is_integral<result_type>::value)
92 __return_value = _M_g.min();
94 __return_value = result_type(0);
95 return __return_value;
101 result_type __return_value;
102 if (is_integral<_Engine_result_type>::value
103 && is_integral<result_type>::value)
104 __return_value = _M_g.max();
105 else if (!is_integral<result_type>::value)
106 __return_value = result_type(1);
109 return __return_value;
124 result_type __return_value;
125 if (is_integral<_Engine_result_type>::value
126 && is_integral<result_type>::value)
127 __return_value = _M_g();
128 else if (!is_integral<_Engine_result_type>::value
129 && !is_integral<result_type>::value)
130 __return_value = result_type(_M_g() - _M_g.min())
131 / result_type(_M_g.max() - _M_g.min());
132 else if (is_integral<_Engine_result_type>::value
133 && !is_integral<result_type>::value)
134 __return_value = result_type(_M_g() - _M_g.min())
135 / result_type(_M_g.max() - _M_g.min() + result_type(1));
137 __return_value = (((_M_g() - _M_g.min())
138 / (_M_g.max() - _M_g.min()))
140 return __return_value;
148 template<
typename _Engine,
typename _Distribution>
149 struct _Adaptor<_Engine*, _Distribution>
151 typedef typename _Engine::result_type _Engine_result_type;
152 typedef typename _Distribution::input_type result_type;
155 _Adaptor(_Engine* __g)
161 result_type __return_value;
162 if (is_integral<_Engine_result_type>::value
163 && is_integral<result_type>::value)
164 __return_value = _M_g->min();
166 __return_value = result_type(0);
167 return __return_value;
173 result_type __return_value;
174 if (is_integral<_Engine_result_type>::value
175 && is_integral<result_type>::value)
176 __return_value = _M_g->max();
177 else if (!is_integral<result_type>::value)
178 __return_value = result_type(1);
181 return __return_value;
187 result_type __return_value;
188 if (is_integral<_Engine_result_type>::value
189 && is_integral<result_type>::value)
190 __return_value = (*_M_g)();
191 else if (!is_integral<_Engine_result_type>::value
192 && !is_integral<result_type>::value)
193 __return_value = result_type((*_M_g)() - _M_g->min())
194 / result_type(_M_g->max() - _M_g->min());
195 else if (is_integral<_Engine_result_type>::value
196 && !is_integral<result_type>::value)
197 __return_value = result_type((*_M_g)() - _M_g->min())
198 / result_type(_M_g->max() - _M_g->min() + result_type(1));
200 __return_value = ((((*_M_g)() - _M_g->min())
201 / (_M_g->max() - _M_g->min()))
203 return __return_value;
217 template<
typename _Engine,
typename _Dist>
221 __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
226 typedef _Engine engine_type;
227 typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
228 typedef _Dist distribution_type;
229 typedef typename _Dist::result_type result_type;
232 typedef typename __gnu_cxx::__enable_if<
243 : _M_engine(__eng), _M_dist(__dist) { }
250 {
return _M_dist(_M_engine); }
255 template<
typename _Tp>
258 {
return _M_dist(_M_engine, __value); }
266 {
return _M_engine; }
272 const engine_value_type&
274 {
return _M_engine; }
286 const distribution_type&
305 engine_value_type _M_engine;
306 distribution_type _M_dist;
345 template<
class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
348 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
370 { this->
seed(__x0); }
389 seed(
unsigned long __s = 1);
410 {
return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
437 {
return __lhs._M_x == __rhs._M_x; }
451 {
return !(__lhs == __rhs); }
460 template<
class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
462 typename _CharT,
typename _Traits>
464 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
481 template<
class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
483 typename _CharT,
typename _Traits>
492 {
return seed(static_cast<unsigned long>(__g)); }
537 template<
class _UIntType,
int __w,
int __n,
int __m,
int __r,
538 _UIntType __a,
int __u,
int __s, _UIntType __b,
int __t,
539 _UIntType __c,
int __l>
540 class mersenne_twister
542 __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
546 typedef _UIntType result_type;
549 static const int word_size = __w;
550 static const int state_size = __n;
551 static const int shift_size = __m;
552 static const int mask_bits = __r;
553 static const _UIntType parameter_a = __a;
554 static const int output_u = __u;
555 static const int output_s = __s;
556 static const _UIntType output_b = __b;
557 static const int output_t = __t;
558 static const _UIntType output_c = __c;
559 static const int output_l = __l;
566 mersenne_twister(
unsigned long __value)
570 mersenne_twister(_Gen& __g)
578 seed(
unsigned long __value);
591 {
return __detail::_Shift<_UIntType, __w>::__value - 1; }
608 const mersenne_twister& __rhs)
609 {
return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
623 const mersenne_twister& __rhs)
624 {
return !(__lhs == __rhs); }
636 template<
class _UIntType1,
int __w1,
int __n1,
int __m1,
int __r1,
637 _UIntType1 __a1,
int __u1,
int __s1, _UIntType1 __b1,
int __t1,
638 _UIntType1 __c1,
int __l1,
639 typename _CharT,
typename _Traits>
641 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
642 const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
643 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
655 template<
class _UIntType1,
int __w1,
int __n1,
int __m1,
int __r1,
656 _UIntType1 __a1,
int __u1,
int __s1, _UIntType1 __b1,
int __t1,
657 _UIntType1 __c1,
int __l1,
658 typename _CharT,
typename _Traits>
661 mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
662 __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
668 {
return seed(static_cast<unsigned long>(__g)); }
674 _UIntType _M_x[state_size];
686 typedef mersenne_twister<
687 unsigned long, 32, 624, 397, 31,
714 template<
typename _IntType, _IntType __m,
int __s,
int __r>
715 class subtract_with_carry
717 __glibcxx_class_requires(_IntType, _IntegerConcept)
721 typedef _IntType result_type;
724 static const _IntType
modulus = __m;
725 static const int long_lag = __r;
726 static const int short_lag = __s;
732 subtract_with_carry()
740 subtract_with_carry(
unsigned long __value)
741 { this->seed(__value); }
750 subtract_with_carry(_Gen& __g)
765 seed(
unsigned long __value = 19780503);
810 const subtract_with_carry& __rhs)
811 {
return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
825 const subtract_with_carry& __rhs)
826 {
return !(__lhs == __rhs); }
838 template<
typename _IntType1, _IntType1 __m1,
int __s1,
int __r1,
839 typename _CharT,
typename _Traits>
841 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
842 const subtract_with_carry<_IntType1, __m1, __s1,
855 template<
typename _IntType1, _IntType1 __m1,
int __s1,
int __r1,
856 typename _CharT,
typename _Traits>
859 subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
865 {
return seed(static_cast<unsigned long>(__g)); }
871 typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
873 _UIntType _M_x[long_lag];
887 template<
typename _RealType,
int __w,
int __s,
int __r>
888 class subtract_with_carry_01
892 typedef _RealType result_type;
895 static const int word_size = __w;
896 static const int long_lag = __r;
897 static const int short_lag = __s;
903 subtract_with_carry_01()
906 _M_initialize_npows();
914 subtract_with_carry_01(
unsigned long __value)
917 _M_initialize_npows();
927 subtract_with_carry_01(_Gen& __g)
930 _M_initialize_npows();
937 seed(
unsigned long __value = 19780503);
946 { seed(__g,
typename is_fundamental<_Gen>::type()); }
982 operator==(
const subtract_with_carry_01& __lhs,
983 const subtract_with_carry_01& __rhs)
985 for (
int __i = 0; __i < long_lag; ++__i)
986 if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
1005 operator!=(
const subtract_with_carry_01& __lhs,
1006 const subtract_with_carry_01& __rhs)
1007 {
return !(__lhs == __rhs); }
1019 template<
typename _RealType1,
int __w1,
int __s1,
int __r1,
1020 typename _CharT,
typename _Traits>
1022 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1023 const subtract_with_carry_01<_RealType1, __w1, __s1,
1036 template<
typename _RealType1,
int __w1,
int __s1,
int __r1,
1037 typename _CharT,
typename _Traits>
1040 subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
1043 template<
class _Gen>
1046 {
return seed(static_cast<unsigned long>(__g)); }
1048 template<
class _Gen>
1053 _M_initialize_npows();
1055 static const int __n = (__w + 31) / 32;
1057 typedef __detail::_UInt32Type _UInt32Type;
1058 _UInt32Type _M_x[long_lag][__n];
1059 _RealType _M_npows[__n];
1060 _UInt32Type _M_carry;
1064 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
1068 typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
1077 template<
class _UniformRandomNumberGenerator,
int __p,
int __r>
1090 static const int block_size = __p;
1091 static const int used_block = __r;
1109 : _M_b(__rng), _M_n(0) { }
1119 : _M_b(__s), _M_n(0) { }
1126 template<
class _Gen>
1128 : _M_b(__g), _M_n(0) { }
1145 template<
class _Gen>
1164 {
return _M_b.min(); }
1171 {
return _M_b.max(); }
1191 {
return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
1205 {
return !(__lhs == __rhs); }
1217 template<
class _UniformRandomNumberGenerator1,
int __p1,
int __r1,
1218 typename _CharT,
typename _Traits>
1220 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1234 template<
class _UniformRandomNumberGenerator1,
int __p1,
int __r1,
1235 typename _CharT,
typename _Traits>
1250 typedef discard_block<
1251 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1260 subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
1266 subtract_with_carry_01<float, 24, 10, 24>,
1272 subtract_with_carry_01<float, 24, 10, 24>,
1282 template<
class _UniformRandomNumberGenerator1,
int __s1,
1283 class _UniformRandomNumberGenerator2,
int __s2>
1298 typedef typename base1_type::result_type _Result_type1;
1299 typedef typename base2_type::result_type _Result_type2;
1303 typedef typename __gnu_cxx::__conditional_type<(
sizeof(_Result_type1)
1304 >
sizeof(_Result_type2)),
1305 _Result_type1, _Result_type2>::__type result_type;
1308 static const int shift1 = __s1;
1309 static const int shift2 = __s2;
1314 { _M_initialize_max(); }
1317 : _M_b1(__rng1), _M_b2(__rng2)
1318 { _M_initialize_max(); }
1320 xor_combine(
unsigned long __s)
1321 : _M_b1(__s), _M_b2(__s + 1)
1322 { _M_initialize_max(); }
1324 template<
class _Gen>
1325 xor_combine(_Gen& __g)
1326 : _M_b1(__g), _M_b2(__g)
1327 { _M_initialize_max(); }
1336 template<
class _Gen>
1367 return ((result_type(_M_b1() - _M_b1.min()) << shift1)
1368 ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
1384 return (__lhs.base1() == __rhs.base1())
1385 && (__lhs.base2() == __rhs.base2());
1400 {
return !(__lhs == __rhs); }
1412 template<
class _UniformRandomNumberGenerator11,
int __s11,
1413 class _UniformRandomNumberGenerator21,
int __s21,
1414 typename _CharT,
typename _Traits>
1416 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1417 const xor_combine<_UniformRandomNumberGenerator11, __s11,
1418 _UniformRandomNumberGenerator21, __s21>& __x);
1430 template<
class _UniformRandomNumberGenerator11,
int __s11,
1431 class _UniformRandomNumberGenerator21,
int __s21,
1432 typename _CharT,
typename _Traits>
1435 xor_combine<_UniformRandomNumberGenerator11, __s11,
1436 _UniformRandomNumberGenerator21, __s21>& __x);
1440 _M_initialize_max();
1443 _M_initialize_max_aux(result_type, result_type,
int);
1459 typedef unsigned int result_type;
1463 #ifdef _GLIBCXX_USE_RANDOM_TR1
1468 if ((__token !=
"/dev/urandom" && __token !=
"/dev/random")
1469 || !(_M_file = std::fopen(__token.c_str(),
"rb")))
1470 std::__throw_runtime_error(__N(
"random_device::"
1471 "random_device(const std::string&)"));
1475 { std::fclose(_M_file); }
1481 : _M_mt(_M_strtoul(__token)) { }
1484 static unsigned long
1487 unsigned long __ret = 5489UL;
1488 if (__str !=
"mt19937")
1490 const char* __nptr = __str.
c_str();
1492 __ret = std::strtoul(__nptr, &__endptr, 0);
1493 if (*__nptr ==
'\0' || *__endptr !=
'\0')
1494 std::__throw_runtime_error(__N(
"random_device::_M_strtoul"
1495 "(const std::string&)"));
1519 #ifdef _GLIBCXX_USE_RANDOM_TR1
1521 std::fread(reinterpret_cast<void*>(&__ret),
sizeof(result_type),
1533 #ifdef _GLIBCXX_USE_RANDOM_TR1
1559 template<
typename _IntType =
int>
1562 __glibcxx_class_requires(_IntType, _IntegerConcept)
1576 : _M_min(__min), _M_max(__max)
1578 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
1607 template<
typename _UniformRandomNumberGenerator>
1611 typedef typename _UniformRandomNumberGenerator::result_type
1613 return _M_call(__urng, _M_min, _M_max,
1622 template<
typename _UniformRandomNumberGenerator>
1624 operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
1626 typedef typename _UniformRandomNumberGenerator::result_type
1628 return _M_call(__urng, 0, __n - 1,
1642 template<
typename _IntType1,
typename _CharT,
typename _Traits>
1644 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1656 template<
typename _IntType1,
typename _CharT,
typename _Traits>
1662 template<
typename _UniformRandomNumberGenerator>
1664 _M_call(_UniformRandomNumberGenerator& __urng,
1665 result_type __min, result_type __max,
true_type);
1667 template<
typename _UniformRandomNumberGenerator>
1669 _M_call(_UniformRandomNumberGenerator& __urng,
1670 result_type __min, result_type __max,
false_type)
1673 / (__urng.max() - __urng.min())
1674 * (__max - __min + 1)) + __min;
1691 typedef int input_type;
1692 typedef bool result_type;
1705 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
1726 template<
class _UniformRandomNumberGenerator>
1730 if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
1745 template<
typename _CharT,
typename _Traits>
1747 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1759 template<
typename _CharT,
typename _Traits>
1763 {
return __is >> __x._M_p; }
1777 template<
typename _IntType =
int,
typename _RealType =
double>
1782 typedef _RealType input_type;
1783 typedef _IntType result_type;
1790 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
1804 template<
class _UniformRandomNumberGenerator>
1806 operator()(_UniformRandomNumberGenerator& __urng);
1818 template<
typename _IntType1,
typename _RealType1,
1819 typename _CharT,
typename _Traits>
1821 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1822 const geometric_distribution<_IntType1, _RealType1>& __x);
1833 template<
typename _CharT,
typename _Traits>
1839 __x._M_initialize();
1853 template<
typename _RealType>
1854 class normal_distribution;
1863 template<
typename _IntType =
int,
typename _RealType =
double>
1868 typedef _RealType input_type;
1869 typedef _IntType result_type;
1874 : _M_mean(__mean), _M_nd()
1876 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
1891 template<
class _UniformRandomNumberGenerator>
1893 operator()(_UniformRandomNumberGenerator& __urng);
1905 template<
typename _IntType1,
typename _RealType1,
1906 typename _CharT,
typename _Traits>
1908 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1909 const poisson_distribution<_IntType1, _RealType1>& __x);
1920 template<
typename _IntType1,
typename _RealType1,
1921 typename _CharT,
typename _Traits>
1924 poisson_distribution<_IntType1, _RealType1>& __x);
1931 normal_distribution<_RealType> _M_nd;
1936 _RealType _M_lm_thr;
1937 #if _GLIBCXX_USE_C99_MATH_TR1
1938 _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
1950 template<
typename _IntType =
int,
typename _RealType =
double>
1955 typedef _RealType input_type;
1956 typedef _IntType result_type;
1961 const _RealType& __p = _RealType(0.5))
1962 : _M_t(__t), _M_p(__p), _M_nd()
1964 _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
1986 template<
class _UniformRandomNumberGenerator>
1988 operator()(_UniformRandomNumberGenerator& __urng);
2000 template<
typename _IntType1,
typename _RealType1,
2001 typename _CharT,
typename _Traits>
2003 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2004 const binomial_distribution<_IntType1, _RealType1>& __x);
2015 template<
typename _IntType1,
typename _RealType1,
2016 typename _CharT,
typename _Traits>
2019 binomial_distribution<_IntType1, _RealType1>& __x);
2025 template<
class _UniformRandomNumberGenerator>
2027 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
2030 normal_distribution<_RealType> _M_nd;
2033 #if _GLIBCXX_USE_C99_MATH_TR1
2034 _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
2035 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
2058 template<
typename _RealType =
double>
2063 typedef _RealType input_type;
2064 typedef _RealType result_type;
2075 _RealType __max = _RealType(1))
2076 : _M_min(__min), _M_max(__max)
2078 _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
2092 template<
class _UniformRandomNumberGenerator>
2094 operator()(_UniformRandomNumberGenerator& __urng)
2095 {
return (__urng() * (_M_max - _M_min)) + _M_min; }
2107 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2109 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2110 const uniform_real<_RealType1>& __x);
2121 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2124 uniform_real<_RealType1>& __x);
2147 template<
typename _RealType =
double>
2152 typedef _RealType input_type;
2153 typedef _RealType result_type;
2162 : _M_lambda(__lambda)
2164 _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
2172 {
return _M_lambda; }
2182 template<
class _UniformRandomNumberGenerator>
2184 operator()(_UniformRandomNumberGenerator& __urng)
2185 {
return -
std::log(__urng()) / _M_lambda; }
2197 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2199 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2200 const exponential_distribution<_RealType1>& __x);
2212 template<
typename _CharT,
typename _Traits>
2216 {
return __is >> __x._M_lambda; }
2219 result_type _M_lambda;
2230 template<
typename _RealType =
double>
2235 typedef _RealType input_type;
2236 typedef _RealType result_type;
2245 const result_type& __sigma = result_type(1))
2246 : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
2248 _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
2263 {
return _M_sigma; }
2270 { _M_saved_available =
false; }
2272 template<
class _UniformRandomNumberGenerator>
2274 operator()(_UniformRandomNumberGenerator& __urng);
2286 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2288 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2300 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2306 result_type _M_mean;
2307 result_type _M_sigma;
2308 result_type _M_saved;
2309 bool _M_saved_available;
2319 template<
typename _RealType =
double>
2324 typedef _RealType input_type;
2325 typedef _RealType result_type;
2333 : _M_alpha(__alpha_val)
2335 _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
2344 {
return _M_alpha; }
2352 template<
class _UniformRandomNumberGenerator>
2354 operator()(_UniformRandomNumberGenerator& __urng);
2366 template<
typename _RealType1,
typename _CharT,
typename _Traits>
2368 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2380 template<
typename _CharT,
typename _Traits>
2385 __is >> __x._M_alpha;
2386 __x._M_initialize();
2394 result_type _M_alpha;
2404 _GLIBCXX_END_NAMESPACE_TR1