1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <random> |
10 | |
11 | // template<class IntType = int> |
12 | // class binomial_distribution |
13 | |
14 | // template<class _URNG> result_type operator()(_URNG& g); |
15 | |
16 | // Test the fix for https://llvm.org/PR44847. |
17 | |
18 | // Serializing/deserializing the state of the RNG requires iostreams |
19 | // UNSUPPORTED: no-localization |
20 | |
21 | // This test appears to hang with picolibc & qemu. |
22 | // UNSUPPORTED: LIBCXX-PICOLIBC-FIXME |
23 | |
24 | #include <random> |
25 | #include <numeric> |
26 | #include <vector> |
27 | #include <cassert> |
28 | #include <sstream> |
29 | |
30 | #include "test_macros.h" |
31 | |
32 | int main(int, char**) { |
33 | typedef std::binomial_distribution<> D; |
34 | typedef std::mt19937 G; |
35 | |
36 | G g; |
37 | D d(128738942, 1.6941441471907126e-08); |
38 | |
39 | std::string state = " 1740222423 1665913615 1140355283 124152834 434145240" |
40 | " 2553002688 4143320714 1810519474 447745536 1439409640 1596060396 1243637295" |
41 | " 452117361 734967774 3276935081 35650473 682607275 4208082251 3209082916" |
42 | " 638915489 4127185595 2859436515 309105096 837982734 796854873 4271538185" |
43 | " 2447193692 607594006 4035165093 4230150671 2567368782 1000242037 2469514821" |
44 | " 1843373462 1751084370 1033341643 3506396674 4169541123 1191187784 3479797390" |
45 | " 3785371742 1475391851 878730063 2661164420 63166678 4127393159 2797714867" |
46 | " 1295211604 2717051330 1009514623 1963164571 561646784 819612826 3340955171" |
47 | " 1338523647 1675643732 458583760 698472119 3233594836 2901754568 4222994242" |
48 | " 51167459 2501563254 2175997686 1673326467 3722097469 2183287831 2155925807" |
49 | " 1071447253 1857934241 320830903 1514449149 103571877 839083116 3893321384" |
50 | " 4236495022 766393502 2729490440 290181118 3191537542 1077578150 3066185245" |
51 | " 3193085445 3786728494 2938418649 3410121447 1453867071 698346001 3037921161" |
52 | " 839425565 2245305640 2806447261 3196149514 1071872132 2337761397 3632554165" |
53 | " 190093341 4248613644 2372806256 3290113603 3852853233 272818390 3168842643" |
54 | " 25788407 4197010683 3864965812 1635548247 2364439227 3344377087 4284620573" |
55 | " 3351117493 3398532219 2757166123 1127999905 2988564217 3707129726 3652489018" |
56 | " 4035370271 475801332 2109377392 2128345729 3920803035 4271338685 509459802" |
57 | " 4158256844 1850467175 1579214935 8921175 4068350958 2951987840 506827330" |
58 | " 3520651040 3359838267 1120109827 3917280670 2748947423 3672973280 1566164613" |
59 | " 2986317531 1204099196 3080678121 3574913280 4009316336 3034181160 1818230129" |
60 | " 3757769877 2464713972 2812294843 782960615 1228678223 2571358051 4260066020" |
61 | " 2439643840 3500737183 1433940923 1031851687 190066625 3777385171 4142770213" |
62 | " 3539275502 1622933657 425231043 3715607557 260333136 4198959706 358418" |
63 | " 1799817566 2839827743 437785672 2967249029 949856347 2081447702 1102224171" |
64 | " 479701434 1781895167 2965560025 264797633 2564778619 2515037023 1320978995" |
65 | " 780140943 2372404879 3823445604 2917613108 143505740 3507288260 2803553229" |
66 | " 4195962819 4072604717 3155823087 323755011 601944215 1840441037 2850820195" |
67 | " 915623058 3306124208 3069788039 1553985704 411632899 3200645375 2973968812" |
68 | " 4263574437 3360058162 144760024 845487010 3508028432 4091510967 3925394277" |
69 | " 71566492 3432433113 3266920114 3539050491 51719451 1245373835 1469278112" |
70 | " 3298302496 753088653 2942352102 1565378440 494477947 971879195 482756304" |
71 | " 2475493857 143180757 324876427 1610205542 1829295320 1937949038 3733336232" |
72 | " 2542145235 3636527510 2347609126 2343078538 2526896356 167862270 2299577281" |
73 | " 3382958264 1911078293 531208917 3588214476 1086101513 1838672874 2119663667" |
74 | " 491092052 2961424745 3048925589 486607333 3505822195 3888367 2949031946" |
75 | " 2684841832 433147539 2333660325 3142554719 435207743 3063000516 4043979879" |
76 | " 3290075088 1114755542 18368971 876637247 3352816011 1421909753 3339898083" |
77 | " 740553432 3682683666 2699730850 2861403632 1971653904 900380480 2635160544" |
78 | " 1318218867 411940 2141321523 2349820793 280562368 3816712514 3790707429" |
79 | " 1619023591 2858103376 1462886666 1723686126 3766879240 1918781537 2792938366" |
80 | " 166155425 803108075 722833545 220020495 880029214 2901984266 609985526" |
81 | " 1367283597 132804580 903066665 131582208 64374393 2006102725 3422930158" |
82 | " 4209296423 380263053 3978926691 3310851236 4245770487 4166043866 4080757525" |
83 | " 3329599259 258706185 2452129516 3191265966 2958285912 1070664670 921876197" |
84 | " 2421722823 2568477756 382467393 2196144533 213270233 116974426 2230947214" |
85 | " 2576421741 393776471 2796472698 3647710433 3264988906 726903864 817800486" |
86 | " 628224092 2707785007 3517963926 596980027 2466711387 3156540408 2517803670" |
87 | " 3408123552 4142066739 3779818910 2988899011 2732117432 3579427018 1513070048" |
88 | " 1566052861 20225341 997297613 3219855094 2777075639 1656025420 3670325076" |
89 | " 1469330501 3061438653 4264717436 1305791144 1237197751 2943926634 1566843825" |
90 | " 3359878993 4037226997 4044024653 3611863927 1375344610 211134383 2406252392" |
91 | " 1349912770 1023874273 1912665158 1762983936 407124872 2936278199 1821966634" |
92 | " 3337187112 2546090236 2594870585 823411965 126464686 4041388220 1686530706" |
93 | " 2780657745 1945569350 203691199 2532411242 1830339266 674003798 2192329968" |
94 | " 2425624005 2819484460 3743368462 2565769418 4179439526 216134386 2880090718" |
95 | " 623297558 3913067470 745959159 2499436157 373025119 3423124368 2522302278" |
96 | " 3719518513 999390119 159673547 228111094 3391079061 1761352720 2549048062" |
97 | " 1125219697 2052834337 3743842626 2433549637 3636723358 1860785315 2387664013" |
98 | " 581140755 11086848 4199179079 1180488689 2060816030 2550665319 1314472090" |
99 | " 1402807876 304522082 3382175195 4260677857 1724818219 2183493354 1004322779" |
100 | " 4166984056 889220724 553883566 582971548 2046113107 2080208105 3473121134" |
101 | " 1959681858 1840897428 2595714120 855065022 4191762128 3679914005 3623561445" |
102 | " 3437337182 3269107597 2019021510 2112281155 985458687 1364815423 514093990" |
103 | " 3711847302 704129707 3398127374 517373404 2646977457 3048605419 1372350917" |
104 | " 3831335422 2263542968 2283942504 2193996512 824623017 1707815852 3337156739" |
105 | " 2301398895 2077322758 193542893 869960695 3878520140 403616946 3228943765" |
106 | " 1037753596 2949947821 379992823 2251850209 1614533146 1704886337 108361232" |
107 | " 3840616436 2932809257 2375700648 596391307 4226846855 191943050 1271990524" |
108 | " 1335422537 3085696177 2030313449 3272604577 1148556450 1184357181 3558074012" |
109 | " 3259720214 2755915415 2720703536 31861322 1740307221 1860884298 3922103763" |
110 | " 4066872392 1756734488 394294796 2505236387 2456914682 639788702 52063410" |
111 | " 2855173018 3307964490 556762160 3624145788 3793504468 4252003358 3690335184" |
112 | " 688245281 2259823605 2617950220 1045718164 3091539813 1330130477 736722350" |
113 | " 3100437052 3900855736 4183439368 1735720081 2644768495 819274730 2364834023" |
114 | " 1393374098 981219339 3969251105 332522940 850159909 646738867 3413137687" |
115 | " 1646732884 80027487 2196948979 5295580 3530173036 767814907 2573204209" |
116 | " 491686200 1287955820 3095830596 2152743903 1738320986 1900678059 3613699900" |
117 | " 3076191184 2917243255 2236492002 3504114019 604643631 3324769580 4078090927" |
118 | " 1245379462 2026215662 1566278916 2832509655 3010562339 1269806412 835199342" |
119 | " 2561789927 163108895 524878390 833167775 3551760739 3008059185 1133970834" |
120 | " 26821616 2846321927 3803209991 581001826 3764614926 3893778555 617853085" |
121 | " 183809431 3510530944 350044681 429839558 1238110552 265276207 205294443" |
122 | " 3092821176 2003027316 2577165836 1277274629 87531073 63821123 354781812" |
123 | " 3700767000 3451421881 990626144 3763226681 3373715717 2360928651 2412110189" |
124 | " 3362121672 3080578947 1604861935 3186376735 2989392261 3550022914 756392571" |
125 | " 1580512570 2584626785 2727753459 730699388 3379402897 3050444856 2244390108" |
126 | " 3941150486 1990708800 2462735 195459645 761670582 1067695927 984662039" |
127 | " 2678082647 1839150009 2113552968 406021267 2193154754 720977131 2445722325" |
128 | " 2482507181 2062595810 1015226482" ; |
129 | |
130 | std::istringstream iss(state); |
131 | iss >> g; |
132 | |
133 | const int N = 1000000; |
134 | std::vector<D::result_type> u; |
135 | for (int i = 0; i < N; ++i) |
136 | { |
137 | D::result_type v = d(g); |
138 | assert(d.min() <= v && v <= d.max()); |
139 | u.push_back(v); |
140 | } |
141 | double mean = std::accumulate(u.begin(), u.end(), |
142 | double(0)) / u.size(); |
143 | double var = 0; |
144 | double skew = 0; |
145 | double kurtosis = 0; |
146 | for (unsigned i = 0; i < u.size(); ++i) |
147 | { |
148 | double dbl = (u[i] - mean); |
149 | double d2 = dbl * dbl; |
150 | var += d2; |
151 | skew += dbl * d2; |
152 | kurtosis += d2 * d2; |
153 | } |
154 | var /= u.size(); |
155 | double dev = std::sqrt(x: var); |
156 | skew /= u.size() * dev * var; |
157 | kurtosis /= u.size() * var * var; |
158 | kurtosis -= 3; |
159 | double x_mean = d.t() * d.p(); |
160 | double x_var = x_mean*(1-d.p()); |
161 | double x_skew = (1-2*d.p()) / std::sqrt(x: x_var); |
162 | double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var; |
163 | assert(std::abs((mean - x_mean) / x_mean) < 0.01); |
164 | assert(std::abs((var - x_var) / x_var) < 0.01); |
165 | assert(std::abs((skew - x_skew) / x_skew) < 0.01); |
166 | assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.04); |
167 | |
168 | return 0; |
169 | } |
170 | |