1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
|
// random number generation -*- C++ -*-
// Copyright (C) 2009-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/**
* @file tr1/random.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{tr1/random}
*/
#ifndef _GLIBCXX_TR1_RANDOM_H
#define _GLIBCXX_TR1_RANDOM_H 1
#pragma GCC system_header
namespace std _GLIBCXX_VISIBILITY(default)
{
namespace tr1
{
// [5.1] Random number generation
/**
* @addtogroup tr1_random Random Number Generation
* A facility for generating random numbers on selected distributions.
* @{
*/
/*
* Implementation-space details.
*/
namespace __detail
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _UIntType, int __w,
bool = __w < std::numeric_limits<_UIntType>::digits>
struct _Shift
{ static const _UIntType __value = 0; };
template<typename _UIntType, int __w>
struct _Shift<_UIntType, __w, true>
{ static const _UIntType __value = _UIntType(1) << __w; };
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
struct _Mod;
// Dispatch based on modulus value to prevent divide-by-zero compile-time
// errors when m == 0.
template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
inline _Tp
__mod(_Tp __x)
{ return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
unsigned, unsigned long>::__type _UInt32Type;
/*
* An adaptor class for converting the output of any Generator into
* the input for a specific Distribution.
*/
template<typename _Engine, typename _Distribution>
struct _Adaptor
{
typedef typename remove_reference<_Engine>::type _BEngine;
typedef typename _BEngine::result_type _Engine_result_type;
typedef typename _Distribution::input_type result_type;
public:
_Adaptor(const _Engine& __g)
: _M_g(__g) { }
result_type
min() const
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = _M_g.min();
else
__return_value = result_type(0);
return __return_value;
}
result_type
max() const
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = _M_g.max();
else if (!is_integral<result_type>::value)
__return_value = result_type(1);
else
__return_value = std::numeric_limits<result_type>::max() - 1;
return __return_value;
}
/*
* Converts a value generated by the adapted random number generator
* into a value in the input domain for the dependent random number
* distribution.
*
* Because the type traits are compile time constants only the
* appropriate clause of the if statements will actually be emitted
* by the compiler.
*/
result_type
operator()()
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = _M_g();
else if (!is_integral<_Engine_result_type>::value
&& !is_integral<result_type>::value)
__return_value = result_type(_M_g() - _M_g.min())
/ result_type(_M_g.max() - _M_g.min());
else if (is_integral<_Engine_result_type>::value
&& !is_integral<result_type>::value)
__return_value = result_type(_M_g() - _M_g.min())
/ result_type(_M_g.max() - _M_g.min() + result_type(1));
else
__return_value = (((_M_g() - _M_g.min())
/ (_M_g.max() - _M_g.min()))
* std::numeric_limits<result_type>::max());
return __return_value;
}
private:
_Engine _M_g;
};
// Specialization for _Engine*.
template<typename _Engine, typename _Distribution>
struct _Adaptor<_Engine*, _Distribution>
{
typedef typename _Engine::result_type _Engine_result_type;
typedef typename _Distribution::input_type result_type;
public:
_Adaptor(_Engine* __g)
: _M_g(__g) { }
result_type
min() const
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = _M_g->min();
else
__return_value = result_type(0);
return __return_value;
}
result_type
max() const
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = _M_g->max();
else if (!is_integral<result_type>::value)
__return_value = result_type(1);
else
__return_value = std::numeric_limits<result_type>::max() - 1;
return __return_value;
}
result_type
operator()()
{
result_type __return_value;
if (is_integral<_Engine_result_type>::value
&& is_integral<result_type>::value)
__return_value = (*_M_g)();
else if (!is_integral<_Engine_result_type>::value
&& !is_integral<result_type>::value)
__return_value = result_type((*_M_g)() - _M_g->min())
/ result_type(_M_g->max() - _M_g->min());
else if (is_integral<_Engine_result_type>::value
&& !is_integral<result_type>::value)
__return_value = result_type((*_M_g)() - _M_g->min())
/ result_type(_M_g->max() - _M_g->min() + result_type(1));
else
__return_value = ((((*_M_g)() - _M_g->min())
/ (_M_g->max() - _M_g->min()))
* std::numeric_limits<result_type>::max());
return __return_value;
}
private:
_Engine* _M_g;
};
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace __detail
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* Produces random numbers on a given distribution function using a
* non-uniform random number generation engine.
*
* @todo the engine_value_type needs to be studied more carefully.
*/
template<typename _Engine, typename _Dist>
class variate_generator
{
// Concept requirements.
__glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
// __glibcxx_class_requires(_Engine, _EngineConcept)
// __glibcxx_class_requires(_Dist, _EngineConcept)
public:
typedef _Engine engine_type;
typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
typedef _Dist distribution_type;
typedef typename _Dist::result_type result_type;
// tr1:5.1.1 table 5.1 requirement
typedef typename __gnu_cxx::__enable_if<
is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
/**
* Constructs a variate generator with the uniform random number
* generator @p __eng for the random distribution @p __dist.
*
* @throws Any exceptions which may thrown by the copy constructors of
* the @p _Engine or @p _Dist objects.
*/
variate_generator(engine_type __eng, distribution_type __dist)
: _M_engine(__eng), _M_dist(__dist) { }
/**
* Gets the next generated value on the distribution.
*/
result_type
operator()()
{ return _M_dist(_M_engine); }
/**
* WTF?
*/
template<typename _Tp>
result_type
operator()(_Tp __value)
{ return _M_dist(_M_engine, __value); }
/**
* Gets a reference to the underlying uniform random number generator
* object.
*/
engine_value_type&
engine()
{ return _M_engine; }
/**
* Gets a const reference to the underlying uniform random number
* generator object.
*/
const engine_value_type&
engine() const
{ return _M_engine; }
/**
* Gets a reference to the underlying random distribution.
*/
distribution_type&
distribution()
{ return _M_dist; }
/**
* Gets a const reference to the underlying random distribution.
*/
const distribution_type&
distribution() const
{ return _M_dist; }
/**
* Gets the closed lower bound of the distribution interval.
*/
result_type
min() const
{ return this->distribution().min(); }
/**
* Gets the closed upper bound of the distribution interval.
*/
result_type
max() const
{ return this->distribution().max(); }
private:
engine_value_type _M_engine;
distribution_type _M_dist;
};
/**
* @addtogroup tr1_random_generators Random Number Generators
* @ingroup tr1_random
*
* These classes define objects which provide random or pseudorandom
* numbers, either from a discrete or a continuous interval. The
* random number generator supplied as a part of this library are
* all uniform random number generators which provide a sequence of
* random number uniformly distributed over their range.
*
* A number generator is a function object with an operator() that
* takes zero arguments and returns a number.
*
* A compliant random number generator must satisfy the following
* requirements. <table border=1 cellpadding=10 cellspacing=0>
* <caption align=top>Random Number Generator Requirements</caption>
* <tr><td>To be documented.</td></tr> </table>
*
* @{
*/
/**
* @brief A model of a linear congruential random number generator.
*
* A random number generator that produces pseudorandom numbers using the
* linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
*
* The template parameter @p _UIntType must be an unsigned integral type
* large enough to store values up to (__m-1). If the template parameter
* @p __m is 0, the modulus @p __m used is
* std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
* parameters @p __a and @p __c must be less than @p __m.
*
* The size of the state is @f$ 1 @f$.
*/
template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
class linear_congruential
{
__glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
// __glibcpp_class_requires(__a < __m && __c < __m)
public:
/** The type of the generated random value. */
typedef _UIntType result_type;
/** The multiplier. */
static const _UIntType multiplier = __a;
/** An increment. */
static const _UIntType increment = __c;
/** The modulus. */
static const _UIntType modulus = __m;
/**
* Constructs a %linear_congruential random number generator engine with
* seed @p __s. The default seed value is 1.
*
* @param __s The initial seed value.
*/
explicit
linear_congruential(unsigned long __x0 = 1)
{ this->seed(__x0); }
/**
* Constructs a %linear_congruential random number generator engine
* seeded from the generator function @p __g.
*
* @param __g The seed generator function.
*/
template<class _Gen>
linear_congruential(_Gen& __g)
{ this->seed(__g); }
/**
* Reseeds the %linear_congruential random number generator engine
* sequence to the seed @g __s.
*
* @param __s The new seed.
*/
void
seed(unsigned long __s = 1);
/**
* Reseeds the %linear_congruential random number generator engine
* sequence using values from the generator function @p __g.
*
* @param __g the seed generator function.
*/
template<class _Gen>
void
seed(_Gen& __g)
{ seed(__g, typename is_fundamental<_Gen>::type()); }
/**
* Gets the smallest possible value in the output range.
*
* The minimum depends on the @p __c parameter: if it is zero, the
* minimum generated must be > 0, otherwise 0 is allowed.
*/
result_type
min() const
{ return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
/**
* Gets the largest possible value in the output range.
*/
result_type
max() const
{ return __m - 1; }
/**
* Gets the next random number in the sequence.
*/
result_type
operator()();
/**
* Compares two linear congruential random number generator
* objects of the same type for equality.
*
* @param __lhs A linear congruential random number generator object.
* @param __rhs Another linear congruential random number generator obj.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const linear_congruential& __lhs,
const linear_congruential& __rhs)
{ return __lhs._M_x == __rhs._M_x; }
/**
* Compares two linear congruential random number generator
* objects of the same type for inequality.
*
* @param __lhs A linear congruential random number generator object.
* @param __rhs Another linear congruential random number generator obj.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const linear_congruential& __lhs,
const linear_congruential& __rhs)
{ return !(__lhs == __rhs); }
/**
* Writes the textual representation of the state x(i) of x to @p __os.
*
* @param __os The output stream.
* @param __lcr A % linear_congruential random number generator.
* @returns __os.
*/
template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const linear_congruential<_UIntType1, __a1, __c1,
__m1>& __lcr);
/**
* Sets the state of the engine by reading its textual
* representation from @p __is.
*
* The textual representation must have been previously written using an
* output stream whose imbued locale and whose type's template
* specialization arguments _CharT and _Traits were the same as those of
* @p __is.
*
* @param __is The input stream.
* @param __lcr A % linear_congruential random number generator.
* @returns __is.
*/
template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
private:
template<class _Gen>
void
seed(_Gen& __g, true_type)
{ return seed(static_cast<unsigned long>(__g)); }
template<class _Gen>
void
seed(_Gen& __g, false_type);
_UIntType _M_x;
};
/**
* The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
*/
typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
/**
* An alternative LCR (Lehmer Generator function) .
*/
typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
/**
* A generalized feedback shift register discrete random number generator.
*
* This algorithm avoids multiplication and division and is designed to be
* friendly to a pipelined architecture. If the parameters are chosen
* correctly, this generator will produce numbers with a very long period and
* fairly good apparent entropy, although still not cryptographically strong.
*
* The best way to use this generator is with the predefined mt19937 class.
*
* This algorithm was originally invented by Makoto Matsumoto and
* Takuji Nishimura.
*
* @var word_size The number of bits in each element of the state vector.
* @var state_size The degree of recursion.
* @var shift_size The period parameter.
* @var mask_bits The separation point bit index.
* @var parameter_a The last row of the twist matrix.
* @var output_u The first right-shift tempering matrix parameter.
* @var output_s The first left-shift tempering matrix parameter.
* @var output_b The first left-shift tempering matrix mask.
* @var output_t The second left-shift tempering matrix parameter.
* @var output_c The second left-shift tempering matrix mask.
* @var output_l The second right-shift tempering matrix parameter.
*/
template<class _UIntType, int __w, int __n, int __m, int __r,
_UIntType __a, int __u, int __s, _UIntType __b, int __t,
_UIntType __c, int __l>
class mersenne_twister
{
__glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
public:
// types
typedef _UIntType result_type;
// parameter values
static const int word_size = __w;
static const int state_size = __n;
static const int shift_size = __m;
static const int mask_bits = __r;
static const _UIntType parameter_a = __a;
static const int output_u = __u;
static const int output_s = __s;
static const _UIntType output_b = __b;
static const int output_t = __t;
static const _UIntType output_c = __c;
static const int output_l = __l;
// constructors and member function
mersenne_twister()
{ seed(); }
explicit
mersenne_twister(unsigned long __value)
{ seed(__value); }
template<class _Gen>
mersenne_twister(_Gen& __g)
{ seed(__g); }
void
seed()
{ seed(5489UL); }
void
seed(unsigned long __value);
template<class _Gen>
void
seed(_Gen& __g)
{ seed(__g, typename is_fundamental<_Gen>::type()); }
result_type
min() const
{ return 0; };
result_type
max() const
{ return __detail::_Shift<_UIntType, __w>::__value - 1; }
result_type
operator()();
/**
* Compares two % mersenne_twister random number generator objects of
* the same type for equality.
*
* @param __lhs A % mersenne_twister random number generator object.
* @param __rhs Another % mersenne_twister random number generator
* object.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const mersenne_twister& __lhs,
const mersenne_twister& __rhs)
{ return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
/**
* Compares two % mersenne_twister random number generator objects of
* the same type for inequality.
*
* @param __lhs A % mersenne_twister random number generator object.
* @param __rhs Another % mersenne_twister random number generator
* object.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const mersenne_twister& __lhs,
const mersenne_twister& __rhs)
{ return !(__lhs == __rhs); }
/**
* Inserts the current state of a % mersenne_twister random number
* generator engine @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A % mersenne_twister random number generator engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
_UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
_UIntType1 __c1, int __l1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
__a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
/**
* Extracts the current state of a % mersenne_twister random number
* generator engine @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A % mersenne_twister random number generator engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
_UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
_UIntType1 __c1, int __l1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
__a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
private:
template<class _Gen>
void
seed(_Gen& __g, true_type)
{ return seed(static_cast<unsigned long>(__g)); }
template<class _Gen>
void
seed(_Gen& __g, false_type);
_UIntType _M_x[state_size];
int _M_p;
};
/**
* The classic Mersenne Twister.
*
* Reference:
* M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
* Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
* on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
*/
typedef mersenne_twister<
unsigned long, 32, 624, 397, 31,
0x9908b0dful, 11, 7,
0x9d2c5680ul, 15,
0xefc60000ul, 18
> mt19937;
/**
* @brief The Marsaglia-Zaman generator.
*
* This is a model of a Generalized Fibonacci discrete random number
* generator, sometimes referred to as the SWC generator.
*
* A discrete random number generator that produces pseudorandom
* numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
* carry_{i-1}) \bmod m @f$.
*
* The size of the state is @f$ r @f$
* and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
*
* N1688[4.13] says <em>the template parameter _IntType shall denote
* an integral type large enough to store values up to m</em>.
*
* @var _M_x The state of the generator. This is a ring buffer.
* @var _M_carry The carry.
* @var _M_p Current index of x(i - r).
*/
template<typename _IntType, _IntType __m, int __s, int __r>
class subtract_with_carry
{
__glibcxx_class_requires(_IntType, _IntegerConcept)
public:
/** The type of the generated random value. */
typedef _IntType result_type;
// parameter values
static const _IntType modulus = __m;
static const int long_lag = __r;
static const int short_lag = __s;
/**
* Constructs a default-initialized % subtract_with_carry random number
* generator.
*/
subtract_with_carry()
{ this->seed(); }
/**
* Constructs an explicitly seeded % subtract_with_carry random number
* generator.
*/
explicit
subtract_with_carry(unsigned long __value)
{ this->seed(__value); }
/**
* Constructs a %subtract_with_carry random number generator engine
* seeded from the generator function @p __g.
*
* @param __g The seed generator function.
*/
template<class _Gen>
subtract_with_carry(_Gen& __g)
{ this->seed(__g); }
/**
* Seeds the initial state @f$ x_0 @f$ of the random number generator.
*
* N1688[4.19] modifies this as follows. If @p __value == 0,
* sets value to 19780503. In any case, with a linear
* congruential generator lcg(i) having parameters @f$ m_{lcg} =
* 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
* @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
* \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
* set carry to 1, otherwise sets carry to 0.
*/
void
seed(unsigned long __value = 19780503);
/**
* Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
* random number generator.
*/
template<class _Gen>
void
seed(_Gen& __g)
{ seed(__g, typename is_fundamental<_Gen>::type()); }
/**
* Gets the inclusive minimum value of the range of random integers
* returned by this generator.
*/
result_type
min() const
{ return 0; }
/**
* Gets the inclusive maximum value of the range of random integers
* returned by this generator.
*/
result_type
max() const
{ return this->modulus - 1; }
/**
* Gets the next random number in the sequence.
*/
result_type
operator()();
/**
* Compares two % subtract_with_carry random number generator objects of
* the same type for equality.
*
* @param __lhs A % subtract_with_carry random number generator object.
* @param __rhs Another % subtract_with_carry random number generator
* object.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const subtract_with_carry& __lhs,
const subtract_with_carry& __rhs)
{ return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
/**
* Compares two % subtract_with_carry random number generator objects of
* the same type for inequality.
*
* @param __lhs A % subtract_with_carry random number generator object.
* @param __rhs Another % subtract_with_carry random number generator
* object.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const subtract_with_carry& __lhs,
const subtract_with_carry& __rhs)
{ return !(__lhs == __rhs); }
/**
* Inserts the current state of a % subtract_with_carry random number
* generator engine @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A % subtract_with_carry random number generator engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const subtract_with_carry<_IntType1, __m1, __s1,
__r1>& __x);
/**
* Extracts the current state of a % subtract_with_carry random number
* generator engine @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A % subtract_with_carry random number generator engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
private:
template<class _Gen>
void
seed(_Gen& __g, true_type)
{ return seed(static_cast<unsigned long>(__g)); }
template<class _Gen>
void
seed(_Gen& __g, false_type);
typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
_UIntType _M_x[long_lag];
_UIntType _M_carry;
int _M_p;
};
/**
* @brief The Marsaglia-Zaman generator (floats version).
*
* @var _M_x The state of the generator. This is a ring buffer.
* @var _M_carry The carry.
* @var _M_p Current index of x(i - r).
* @var _M_npows Precomputed negative powers of 2.
*/
template<typename _RealType, int __w, int __s, int __r>
class subtract_with_carry_01
{
public:
/** The type of the generated random value. */
typedef _RealType result_type;
// parameter values
static const int word_size = __w;
static const int long_lag = __r;
static const int short_lag = __s;
/**
* Constructs a default-initialized % subtract_with_carry_01 random
* number generator.
*/
subtract_with_carry_01()
{
this->seed();
_M_initialize_npows();
}
/**
* Constructs an explicitly seeded % subtract_with_carry_01 random number
* generator.
*/
explicit
subtract_with_carry_01(unsigned long __value)
{
this->seed(__value);
_M_initialize_npows();
}
/**
* Constructs a % subtract_with_carry_01 random number generator engine
* seeded from the generator function @p __g.
*
* @param __g The seed generator function.
*/
template<class _Gen>
subtract_with_carry_01(_Gen& __g)
{
this->seed(__g);
_M_initialize_npows();
}
/**
* Seeds the initial state @f$ x_0 @f$ of the random number generator.
*/
void
seed(unsigned long __value = 19780503);
/**
* Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
* random number generator.
*/
template<class _Gen>
void
seed(_Gen& __g)
{ seed(__g, typename is_fundamental<_Gen>::type()); }
/**
* Gets the minimum value of the range of random floats
* returned by this generator.
*/
result_type
min() const
{ return 0.0; }
/**
* Gets the maximum value of the range of random floats
* returned by this generator.
*/
result_type
max() const
{ return 1.0; }
/**
* Gets the next random number in the sequence.
*/
result_type
operator()();
/**
* Compares two % subtract_with_carry_01 random number generator objects
* of the same type for equality.
*
* @param __lhs A % subtract_with_carry_01 random number
* generator object.
* @param __rhs Another % subtract_with_carry_01 random number generator
* object.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const subtract_with_carry_01& __lhs,
const subtract_with_carry_01& __rhs)
{
for (int __i = 0; __i < long_lag; ++__i)
if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
__rhs._M_x[__i]))
return false;
return true;
}
/**
* Compares two % subtract_with_carry_01 random number generator objects
* of the same type for inequality.
*
* @param __lhs A % subtract_with_carry_01 random number
* generator object.
*
* @param __rhs Another % subtract_with_carry_01 random number generator
* object.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const subtract_with_carry_01& __lhs,
const subtract_with_carry_01& __rhs)
{ return !(__lhs == __rhs); }
/**
* Inserts the current state of a % subtract_with_carry_01 random number
* generator engine @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A % subtract_with_carry_01 random number generator engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _RealType1, int __w1, int __s1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const subtract_with_carry_01<_RealType1, __w1, __s1,
__r1>& __x);
/**
* Extracts the current state of a % subtract_with_carry_01 random number
* generator engine @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A % subtract_with_carry_01 random number generator engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<typename _RealType1, int __w1, int __s1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
private:
template<class _Gen>
void
seed(_Gen& __g, true_type)
{ return seed(static_cast<unsigned long>(__g)); }
template<class _Gen>
void
seed(_Gen& __g, false_type);
void
_M_initialize_npows();
static const int __n = (__w + 31) / 32;
typedef __detail::_UInt32Type _UInt32Type;
_UInt32Type _M_x[long_lag][__n];
_RealType _M_npows[__n];
_UInt32Type _M_carry;
int _M_p;
};
typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 508. Bad parameters for ranlux64_base_01.
typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
/**
* Produces random numbers from some base engine by discarding blocks of
* data.
*
* 0 <= @p __r <= @p __p
*/
template<class _UniformRandomNumberGenerator, int __p, int __r>
class discard_block
{
// __glibcxx_class_requires(typename base_type::result_type,
// ArithmeticTypeConcept)
public:
/** The type of the underlying generator engine. */
typedef _UniformRandomNumberGenerator base_type;
/** The type of the generated random value. */
typedef typename base_type::result_type result_type;
// parameter values
static const int block_size = __p;
static const int used_block = __r;
/**
* Constructs a default %discard_block engine.
*
* The underlying engine is default constructed as well.
*/
discard_block()
: _M_n(0) { }
/**
* Copy constructs a %discard_block engine.
*
* Copies an existing base class random number generator.
* @param rng An existing (base class) engine object.
*/
explicit
discard_block(const base_type& __rng)
: _M_b(__rng), _M_n(0) { }
/**
* Seed constructs a %discard_block engine.
*
* Constructs the underlying generator engine seeded with @p __s.
* @param __s A seed value for the base class engine.
*/
explicit
discard_block(unsigned long __s)
: _M_b(__s), _M_n(0) { }
/**
* Generator construct a %discard_block engine.
*
* @param __g A seed generator function.
*/
template<class _Gen>
discard_block(_Gen& __g)
: _M_b(__g), _M_n(0) { }
/**
* Reseeds the %discard_block object with the default seed for the
* underlying base class generator engine.
*/
void seed()
{
_M_b.seed();
_M_n = 0;
}
/**
* Reseeds the %discard_block object with the given seed generator
* function.
* @param __g A seed generator function.
*/
template<class _Gen>
void seed(_Gen& __g)
{
_M_b.seed(__g);
_M_n = 0;
}
/**
* Gets a const reference to the underlying generator engine object.
*/
const base_type&
base() const
{ return _M_b; }
/**
* Gets the minimum value in the generated random number range.
*/
result_type
min() const
{ return _M_b.min(); }
/**
* Gets the maximum value in the generated random number range.
*/
result_type
max() const
{ return _M_b.max(); }
/**
* Gets the next value in the generated random number sequence.
*/
result_type
operator()();
/**
* Compares two %discard_block random number generator objects of
* the same type for equality.
*
* @param __lhs A %discard_block random number generator object.
* @param __rhs Another %discard_block random number generator
* object.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const discard_block& __lhs, const discard_block& __rhs)
{ return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
/**
* Compares two %discard_block random number generator objects of
* the same type for inequality.
*
* @param __lhs A %discard_block random number generator object.
* @param __rhs Another %discard_block random number generator
* object.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const discard_block& __lhs, const discard_block& __rhs)
{ return !(__lhs == __rhs); }
/**
* Inserts the current state of a %discard_block random number
* generator engine @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %discard_block random number generator engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const discard_block<_UniformRandomNumberGenerator1,
__p1, __r1>& __x);
/**
* Extracts the current state of a % subtract_with_carry random number
* generator engine @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %discard_block random number generator engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
discard_block<_UniformRandomNumberGenerator1,
__p1, __r1>& __x);
private:
base_type _M_b;
int _M_n;
};
/**
* James's luxury-level-3 integer adaptation of Luescher's generator.
*/
typedef discard_block<
subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
223,
24
> ranlux3;
/**
* James's luxury-level-4 integer adaptation of Luescher's generator.
*/
typedef discard_block<
subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
389,
24
> ranlux4;
typedef discard_block<
subtract_with_carry_01<float, 24, 10, 24>,
223,
24
> ranlux3_01;
typedef discard_block<
subtract_with_carry_01<float, 24, 10, 24>,
389,
24
> ranlux4_01;
/**
* A random number generator adaptor class that combines two random number
* generator engines into a single output sequence.
*/
template<class _UniformRandomNumberGenerator1, int __s1,
class _UniformRandomNumberGenerator2, int __s2>
class xor_combine
{
// __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
// result_type, ArithmeticTypeConcept)
// __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
// result_type, ArithmeticTypeConcept)
public:
/** The type of the first underlying generator engine. */
typedef _UniformRandomNumberGenerator1 base1_type;
/** The type of the second underlying generator engine. */
typedef _UniformRandomNumberGenerator2 base2_type;
private:
typedef typename base1_type::result_type _Result_type1;
typedef typename base2_type::result_type _Result_type2;
public:
/** The type of the generated random value. */
typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
> sizeof(_Result_type2)),
_Result_type1, _Result_type2>::__type result_type;
// parameter values
static const int shift1 = __s1;
static const int shift2 = __s2;
// constructors and member function
xor_combine()
: _M_b1(), _M_b2()
{ _M_initialize_max(); }
xor_combine(const base1_type& __rng1, const base2_type& __rng2)
: _M_b1(__rng1), _M_b2(__rng2)
{ _M_initialize_max(); }
xor_combine(unsigned long __s)
: _M_b1(__s), _M_b2(__s + 1)
{ _M_initialize_max(); }
template<class _Gen>
xor_combine(_Gen& __g)
: _M_b1(__g), _M_b2(__g)
{ _M_initialize_max(); }
void
seed()
{
_M_b1.seed();
_M_b2.seed();
}
template<class _Gen>
void
seed(_Gen& __g)
{
_M_b1.seed(__g);
_M_b2.seed(__g);
}
const base1_type&
base1() const
{ return _M_b1; }
const base2_type&
base2() const
{ return _M_b2; }
result_type
min() const
{ return 0; }
result_type
max() const
{ return _M_max; }
/**
* Gets the next random number in the sequence.
*/
// NB: Not exactly the TR1 formula, per N2079 instead.
result_type
operator()()
{
return ((result_type(_M_b1() - _M_b1.min()) << shift1)
^ (result_type(_M_b2() - _M_b2.min()) << shift2));
}
/**
* Compares two %xor_combine random number generator objects of
* the same type for equality.
*
* @param __lhs A %xor_combine random number generator object.
* @param __rhs Another %xor_combine random number generator
* object.
*
* @returns true if the two objects are equal, false otherwise.
*/
friend bool
operator==(const xor_combine& __lhs, const xor_combine& __rhs)
{
return (__lhs.base1() == __rhs.base1())
&& (__lhs.base2() == __rhs.base2());
}
/**
* Compares two %xor_combine random number generator objects of
* the same type for inequality.
*
* @param __lhs A %xor_combine random number generator object.
* @param __rhs Another %xor_combine random number generator
* object.
*
* @returns true if the two objects are not equal, false otherwise.
*/
friend bool
operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
{ return !(__lhs == __rhs); }
/**
* Inserts the current state of a %xor_combine random number
* generator engine @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %xor_combine random number generator engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<class _UniformRandomNumberGenerator11, int __s11,
class _UniformRandomNumberGenerator21, int __s21,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const xor_combine<_UniformRandomNumberGenerator11, __s11,
_UniformRandomNumberGenerator21, __s21>& __x);
/**
* Extracts the current state of a %xor_combine random number
* generator engine @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %xor_combine random number generator engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<class _UniformRandomNumberGenerator11, int __s11,
class _UniformRandomNumberGenerator21, int __s21,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
xor_combine<_UniformRandomNumberGenerator11, __s11,
_UniformRandomNumberGenerator21, __s21>& __x);
private:
void
_M_initialize_max();
result_type
_M_initialize_max_aux(result_type, result_type, int);
base1_type _M_b1;
base2_type _M_b2;
result_type _M_max;
};
/**
* A standard interface to a platform-specific non-deterministic
* random number generator (if any are available).
*/
class random_device
{
public:
// types
typedef unsigned int result_type;
// constructors, destructors and member functions
#ifdef _GLIBCXX_USE_RANDOM_TR1
explicit
random_device(const std::string& __token = "/dev/urandom")
{
if ((__token != "/dev/urandom" && __token != "/dev/random")
|| !(_M_file = std::fopen(__token.c_str(), "rb")))
std::__throw_runtime_error(__N("random_device::"
"random_device(const std::string&)"));
}
~random_device()
{ std::fclose(_M_file); }
#else
explicit
random_device(const std::string& __token = "mt19937")
: _M_mt(_M_strtoul(__token)) { }
private:
static unsigned long
_M_strtoul(const std::string& __str)
{
unsigned long __ret = 5489UL;
if (__str != "mt19937")
{
const char* __nptr = __str.c_str();
char* __endptr;
__ret = std::strtoul(__nptr, &__endptr, 0);
if (*__nptr == '\0' || *__endptr != '\0')
std::__throw_runtime_error(__N("random_device::_M_strtoul"
"(const std::string&)"));
}
return __ret;
}
public:
#endif
result_type
min() const
{ return std::numeric_limits<result_type>::min(); }
result_type
max() const
{ return std::numeric_limits<result_type>::max(); }
double
entropy() const
{ return 0.0; }
result_type
operator()()
{
#ifdef _GLIBCXX_USE_RANDOM_TR1
result_type __ret;
std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1, _M_file);
return __ret;
#else
return _M_mt();
#endif
}
private:
random_device(const random_device&);
void operator=(const random_device&);
#ifdef _GLIBCXX_USE_RANDOM_TR1
FILE* _M_file;
#else
mt19937 _M_mt;
#endif
};
/* @} */ // group tr1_random_generators
/**
* @addtogroup tr1_random_distributions Random Number Distributions
* @ingroup tr1_random
* @{
*/
/**
* @addtogroup tr1_random_distributions_discrete Discrete Distributions
* @ingroup tr1_random_distributions
* @{
*/
/**
* @brief Uniform discrete distribution for random numbers.
* A discrete random distribution on the range @f$[min, max]@f$ with equal
* probability throughout the range.
*/
template<typename _IntType = int>
class uniform_int
{
__glibcxx_class_requires(_IntType, _IntegerConcept)
public:
/** The type of the parameters of the distribution. */
typedef _IntType input_type;
/** The type of the range of the distribution. */
typedef _IntType result_type;
public:
/**
* Constructs a uniform distribution object.
*/
explicit
uniform_int(_IntType __min = 0, _IntType __max = 9)
: _M_min(__min), _M_max(__max)
{
_GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
}
/**
* Gets the inclusive lower bound of the distribution range.
*/
result_type
min() const
{ return _M_min; }
/**
* Gets the inclusive upper bound of the distribution range.
*/
result_type
max() const
{ return _M_max; }
/**
* Resets the distribution state.
*
* Does nothing for the uniform integer distribution.
*/
void
reset() { }
/**
* Gets a uniformly distributed random number in the range
* @f$(min, max)@f$.
*/
template<typename _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng)
{
typedef typename _UniformRandomNumberGenerator::result_type
_UResult_type;
return _M_call(__urng, _M_min, _M_max,
typename is_integral<_UResult_type>::type());
}
/**
* Gets a uniform random number in the range @f$[0, n)@f$.
*
* This function is aimed at use with std::random_shuffle.
*/
template<typename _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
{
typedef typename _UniformRandomNumberGenerator::result_type
_UResult_type;
return _M_call(__urng, 0, __n - 1,
typename is_integral<_UResult_type>::type());
}
/**
* Inserts a %uniform_int random number distribution @p __x into the
* output stream @p os.
*
* @param __os An output stream.
* @param __x A %uniform_int random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const uniform_int<_IntType1>& __x);
/**
* Extracts a %uniform_int random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %uniform_int random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _IntType1, typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
uniform_int<_IntType1>& __x);
private:
template<typename _UniformRandomNumberGenerator>
result_type
_M_call(_UniformRandomNumberGenerator& __urng,
result_type __min, result_type __max, true_type);
template<typename _UniformRandomNumberGenerator>
result_type
_M_call(_UniformRandomNumberGenerator& __urng,
result_type __min, result_type __max, false_type)
{
return result_type((__urng() - __urng.min())
/ (__urng.max() - __urng.min())
* (__max - __min + 1)) + __min;
}
_IntType _M_min;
_IntType _M_max;
};
/**
* @brief A Bernoulli random number distribution.
*
* Generates a sequence of true and false values with likelihood @f$ p @f$
* that true will come up and @f$ (1 - p) @f$ that false will appear.
*/
class bernoulli_distribution
{
public:
typedef int input_type;
typedef bool result_type;
public:
/**
* Constructs a Bernoulli distribution with likelihood @p p.
*
* @param __p [IN] The likelihood of a true result being returned. Must
* be in the interval @f$ [0, 1] @f$.
*/
explicit
bernoulli_distribution(double __p = 0.5)
: _M_p(__p)
{
_GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
}
/**
* Gets the @p p parameter of the distribution.
*/
double
p() const
{ return _M_p; }
/**
* Resets the distribution state.
*
* Does nothing for a Bernoulli distribution.
*/
void
reset() { }
/**
* Gets the next value in the Bernoullian sequence.
*/
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng)
{
if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
return true;
return false;
}
/**
* Inserts a %bernoulli_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %bernoulli_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const bernoulli_distribution& __x);
/**
* Extracts a %bernoulli_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %bernoulli_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
bernoulli_distribution& __x)
{ return __is >> __x._M_p; }
private:
double _M_p;
};
/**
* @brief A discrete geometric random number distribution.
*
* The formula for the geometric probability mass function is
* @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
* distribution.
*/
template<typename _IntType = int, typename _RealType = double>
class geometric_distribution
{
public:
// types
typedef _RealType input_type;
typedef _IntType result_type;
// constructors and member function
explicit
geometric_distribution(const _RealType& __p = _RealType(0.5))
: _M_p(__p)
{
_GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
_M_initialize();
}
/**
* Gets the distribution parameter @p p.
*/
_RealType
p() const
{ return _M_p; }
void
reset() { }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %geometric_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %geometric_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const geometric_distribution<_IntType1, _RealType1>& __x);
/**
* Extracts a %geometric_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %geometric_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
geometric_distribution& __x)
{
__is >> __x._M_p;
__x._M_initialize();
return __is;
}
private:
void
_M_initialize()
{ _M_log_p = std::log(_M_p); }
_RealType _M_p;
_RealType _M_log_p;
};
template<typename _RealType>
class normal_distribution;
/**
* @brief A discrete Poisson random number distribution.
*
* The formula for the Poisson probability mass function is
* @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
* parameter of the distribution.
*/
template<typename _IntType = int, typename _RealType = double>
class poisson_distribution
{
public:
// types
typedef _RealType input_type;
typedef _IntType result_type;
// constructors and member function
explicit
poisson_distribution(const _RealType& __mean = _RealType(1))
: _M_mean(__mean), _M_nd()
{
_GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
_M_initialize();
}
/**
* Gets the distribution parameter @p mean.
*/
_RealType
mean() const
{ return _M_mean; }
void
reset()
{ _M_nd.reset(); }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %poisson_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %poisson_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const poisson_distribution<_IntType1, _RealType1>& __x);
/**
* Extracts a %poisson_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %poisson_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
poisson_distribution<_IntType1, _RealType1>& __x);
private:
void
_M_initialize();
// NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
normal_distribution<_RealType> _M_nd;
_RealType _M_mean;
// Hosts either log(mean) or the threshold of the simple method.
_RealType _M_lm_thr;
#if _GLIBCXX_USE_C99_MATH_TR1
_RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
#endif
};
/**
* @brief A discrete binomial random number distribution.
*
* The formula for the binomial probability mass function is
* @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
* and @f$ p @f$ are the parameters of the distribution.
*/
template<typename _IntType = int, typename _RealType = double>
class binomial_distribution
{
public:
// types
typedef _RealType input_type;
typedef _IntType result_type;
// constructors and member function
explicit
binomial_distribution(_IntType __t = 1,
const _RealType& __p = _RealType(0.5))
: _M_t(__t), _M_p(__p), _M_nd()
{
_GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
_M_initialize();
}
/**
* Gets the distribution @p t parameter.
*/
_IntType
t() const
{ return _M_t; }
/**
* Gets the distribution @p p parameter.
*/
_RealType
p() const
{ return _M_p; }
void
reset()
{ _M_nd.reset(); }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %binomial_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %binomial_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const binomial_distribution<_IntType1, _RealType1>& __x);
/**
* Extracts a %binomial_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %binomial_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
binomial_distribution<_IntType1, _RealType1>& __x);
private:
void
_M_initialize();
template<class _UniformRandomNumberGenerator>
result_type
_M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
// NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
normal_distribution<_RealType> _M_nd;
_RealType _M_q;
#if _GLIBCXX_USE_C99_MATH_TR1
_RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
_M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
#endif
_RealType _M_p;
_IntType _M_t;
bool _M_easy;
};
/* @} */ // group tr1_random_distributions_discrete
/**
* @addtogroup tr1_random_distributions_continuous Continuous Distributions
* @ingroup tr1_random_distributions
* @{
*/
/**
* @brief Uniform continuous distribution for random numbers.
*
* A continuous random distribution on the range [min, max) with equal
* probability throughout the range. The URNG should be real-valued and
* deliver number in the range [0, 1).
*/
template<typename _RealType = double>
class uniform_real
{
public:
// types
typedef _RealType input_type;
typedef _RealType result_type;
public:
/**
* Constructs a uniform_real object.
*
* @param __min [IN] The lower bound of the distribution.
* @param __max [IN] The upper bound of the distribution.
*/
explicit
uniform_real(_RealType __min = _RealType(0),
_RealType __max = _RealType(1))
: _M_min(__min), _M_max(__max)
{
_GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
}
result_type
min() const
{ return _M_min; }
result_type
max() const
{ return _M_max; }
void
reset() { }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng)
{ return (__urng() * (_M_max - _M_min)) + _M_min; }
/**
* Inserts a %uniform_real random number distribution @p __x into the
* output stream @p __os.
*
* @param __os An output stream.
* @param __x A %uniform_real random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const uniform_real<_RealType1>& __x);
/**
* Extracts a %uniform_real random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %uniform_real random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
uniform_real<_RealType1>& __x);
private:
_RealType _M_min;
_RealType _M_max;
};
/**
* @brief An exponential continuous distribution for random numbers.
*
* The formula for the exponential probability mass function is
* @f$ p(x) = \lambda e^{-\lambda x} @f$.
*
* <table border=1 cellpadding=10 cellspacing=0>
* <caption align=top>Distribution Statistics</caption>
* <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
* <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
* <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
* <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
* <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
* </table>
*/
template<typename _RealType = double>
class exponential_distribution
{
public:
// types
typedef _RealType input_type;
typedef _RealType result_type;
public:
/**
* Constructs an exponential distribution with inverse scale parameter
* @f$ \lambda @f$.
*/
explicit
exponential_distribution(const result_type& __lambda = result_type(1))
: _M_lambda(__lambda)
{
_GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
}
/**
* Gets the inverse scale parameter of the distribution.
*/
_RealType
lambda() const
{ return _M_lambda; }
/**
* Resets the distribution.
*
* Has no effect on exponential distributions.
*/
void
reset() { }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng)
{ return -std::log(__urng()) / _M_lambda; }
/**
* Inserts a %exponential_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %exponential_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const exponential_distribution<_RealType1>& __x);
/**
* Extracts a %exponential_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %exponential_distribution random number
* generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
exponential_distribution& __x)
{ return __is >> __x._M_lambda; }
private:
result_type _M_lambda;
};
/**
* @brief A normal continuous distribution for random numbers.
*
* The formula for the normal probability mass function is
* @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
* e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
*/
template<typename _RealType = double>
class normal_distribution
{
public:
// types
typedef _RealType input_type;
typedef _RealType result_type;
public:
/**
* Constructs a normal distribution with parameters @f$ mean @f$ and
* @f$ \sigma @f$.
*/
explicit
normal_distribution(const result_type& __mean = result_type(0),
const result_type& __sigma = result_type(1))
: _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
{
_GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
}
/**
* Gets the mean of the distribution.
*/
_RealType
mean() const
{ return _M_mean; }
/**
* Gets the @f$ \sigma @f$ of the distribution.
*/
_RealType
sigma() const
{ return _M_sigma; }
/**
* Resets the distribution.
*/
void
reset()
{ _M_saved_available = false; }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %normal_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %normal_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const normal_distribution<_RealType1>& __x);
/**
* Extracts a %normal_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %normal_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
normal_distribution<_RealType1>& __x);
private:
result_type _M_mean;
result_type _M_sigma;
result_type _M_saved;
bool _M_saved_available;
};
/**
* @brief A gamma continuous distribution for random numbers.
*
* The formula for the gamma probability mass function is
* @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
*/
template<typename _RealType = double>
class gamma_distribution
{
public:
// types
typedef _RealType input_type;
typedef _RealType result_type;
public:
/**
* Constructs a gamma distribution with parameters @f$ \alpha @f$.
*/
explicit
gamma_distribution(const result_type& __alpha_val = result_type(1))
: _M_alpha(__alpha_val)
{
_GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
_M_initialize();
}
/**
* Gets the @f$ \alpha @f$ of the distribution.
*/
_RealType
alpha() const
{ return _M_alpha; }
/**
* Resets the distribution.
*/
void
reset() { }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %gamma_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %gamma_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _RealType1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const gamma_distribution<_RealType1>& __x);
/**
* Extracts a %gamma_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %gamma_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
gamma_distribution& __x)
{
__is >> __x._M_alpha;
__x._M_initialize();
return __is;
}
private:
void
_M_initialize();
result_type _M_alpha;
// Hosts either lambda of GB or d of modified Vaduva's.
result_type _M_l_d;
};
/* @} */ // group tr1_random_distributions_continuous
/* @} */ // group tr1_random_distributions
/* @} */ // group tr1_random
_GLIBCXX_END_NAMESPACE_VERSION
}
}
#endif // _GLIBCXX_TR1_RANDOM_H
|