random.tcc 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381
  1. // random number generation (out of line) -*- C++ -*-
  2. // Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/random.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{random}
  23. */
  24. #ifndef _RANDOM_TCC
  25. #define _RANDOM_TCC 1
  26. #include <numeric> // std::accumulate and std::partial_sum
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. /*
  31. * (Further) implementation-space details.
  32. */
  33. namespace __detail
  34. {
  35. // General case for x = (ax + c) mod m -- use Schrage's algorithm
  36. // to avoid integer overflow.
  37. //
  38. // Preconditions: a > 0, m > 0.
  39. //
  40. // Note: only works correctly for __m % __a < __m / __a.
  41. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
  42. _Tp
  43. _Mod<_Tp, __m, __a, __c, false, true>::
  44. __calc(_Tp __x)
  45. {
  46. if (__a == 1)
  47. __x %= __m;
  48. else
  49. {
  50. static const _Tp __q = __m / __a;
  51. static const _Tp __r = __m % __a;
  52. _Tp __t1 = __a * (__x % __q);
  53. _Tp __t2 = __r * (__x / __q);
  54. if (__t1 >= __t2)
  55. __x = __t1 - __t2;
  56. else
  57. __x = __m - __t2 + __t1;
  58. }
  59. if (__c != 0)
  60. {
  61. const _Tp __d = __m - __x;
  62. if (__d > __c)
  63. __x += __c;
  64. else
  65. __x = __c - __d;
  66. }
  67. return __x;
  68. }
  69. template<typename _InputIterator, typename _OutputIterator,
  70. typename _Tp>
  71. _OutputIterator
  72. __normalize(_InputIterator __first, _InputIterator __last,
  73. _OutputIterator __result, const _Tp& __factor)
  74. {
  75. for (; __first != __last; ++__first, ++__result)
  76. *__result = *__first / __factor;
  77. return __result;
  78. }
  79. } // namespace __detail
  80. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  81. constexpr _UIntType
  82. linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
  83. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  84. constexpr _UIntType
  85. linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
  86. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  87. constexpr _UIntType
  88. linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
  89. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  90. constexpr _UIntType
  91. linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
  92. /**
  93. * Seeds the LCR with integral value @p __s, adjusted so that the
  94. * ring identity is never a member of the convergence set.
  95. */
  96. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  97. void
  98. linear_congruential_engine<_UIntType, __a, __c, __m>::
  99. seed(result_type __s)
  100. {
  101. if ((__detail::__mod<_UIntType, __m>(__c) == 0)
  102. && (__detail::__mod<_UIntType, __m>(__s) == 0))
  103. _M_x = 1;
  104. else
  105. _M_x = __detail::__mod<_UIntType, __m>(__s);
  106. }
  107. /**
  108. * Seeds the LCR engine with a value generated by @p __q.
  109. */
  110. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  111. template<typename _Sseq>
  112. auto
  113. linear_congruential_engine<_UIntType, __a, __c, __m>::
  114. seed(_Sseq& __q)
  115. -> _If_seed_seq<_Sseq>
  116. {
  117. const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
  118. : std::__lg(__m);
  119. const _UIntType __k = (__k0 + 31) / 32;
  120. uint_least32_t __arr[__k + 3];
  121. __q.generate(__arr + 0, __arr + __k + 3);
  122. _UIntType __factor = 1u;
  123. _UIntType __sum = 0u;
  124. for (size_t __j = 0; __j < __k; ++__j)
  125. {
  126. __sum += __arr[__j + 3] * __factor;
  127. __factor *= __detail::_Shift<_UIntType, 32>::__value;
  128. }
  129. seed(__sum);
  130. }
  131. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
  132. typename _CharT, typename _Traits>
  133. std::basic_ostream<_CharT, _Traits>&
  134. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  135. const linear_congruential_engine<_UIntType,
  136. __a, __c, __m>& __lcr)
  137. {
  138. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  139. const typename __ios_base::fmtflags __flags = __os.flags();
  140. const _CharT __fill = __os.fill();
  141. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  142. __os.fill(__os.widen(' '));
  143. __os << __lcr._M_x;
  144. __os.flags(__flags);
  145. __os.fill(__fill);
  146. return __os;
  147. }
  148. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
  149. typename _CharT, typename _Traits>
  150. std::basic_istream<_CharT, _Traits>&
  151. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  152. linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
  153. {
  154. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  155. const typename __ios_base::fmtflags __flags = __is.flags();
  156. __is.flags(__ios_base::dec);
  157. __is >> __lcr._M_x;
  158. __is.flags(__flags);
  159. return __is;
  160. }
  161. template<typename _UIntType,
  162. size_t __w, size_t __n, size_t __m, size_t __r,
  163. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  164. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  165. _UIntType __f>
  166. constexpr size_t
  167. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  168. __s, __b, __t, __c, __l, __f>::word_size;
  169. template<typename _UIntType,
  170. size_t __w, size_t __n, size_t __m, size_t __r,
  171. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  172. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  173. _UIntType __f>
  174. constexpr size_t
  175. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  176. __s, __b, __t, __c, __l, __f>::state_size;
  177. template<typename _UIntType,
  178. size_t __w, size_t __n, size_t __m, size_t __r,
  179. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  180. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  181. _UIntType __f>
  182. constexpr size_t
  183. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  184. __s, __b, __t, __c, __l, __f>::shift_size;
  185. template<typename _UIntType,
  186. size_t __w, size_t __n, size_t __m, size_t __r,
  187. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  188. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  189. _UIntType __f>
  190. constexpr size_t
  191. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  192. __s, __b, __t, __c, __l, __f>::mask_bits;
  193. template<typename _UIntType,
  194. size_t __w, size_t __n, size_t __m, size_t __r,
  195. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  196. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  197. _UIntType __f>
  198. constexpr _UIntType
  199. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  200. __s, __b, __t, __c, __l, __f>::xor_mask;
  201. template<typename _UIntType,
  202. size_t __w, size_t __n, size_t __m, size_t __r,
  203. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  204. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  205. _UIntType __f>
  206. constexpr size_t
  207. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  208. __s, __b, __t, __c, __l, __f>::tempering_u;
  209. template<typename _UIntType,
  210. size_t __w, size_t __n, size_t __m, size_t __r,
  211. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  212. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  213. _UIntType __f>
  214. constexpr _UIntType
  215. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  216. __s, __b, __t, __c, __l, __f>::tempering_d;
  217. template<typename _UIntType,
  218. size_t __w, size_t __n, size_t __m, size_t __r,
  219. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  220. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  221. _UIntType __f>
  222. constexpr size_t
  223. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  224. __s, __b, __t, __c, __l, __f>::tempering_s;
  225. template<typename _UIntType,
  226. size_t __w, size_t __n, size_t __m, size_t __r,
  227. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  228. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  229. _UIntType __f>
  230. constexpr _UIntType
  231. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  232. __s, __b, __t, __c, __l, __f>::tempering_b;
  233. template<typename _UIntType,
  234. size_t __w, size_t __n, size_t __m, size_t __r,
  235. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  236. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  237. _UIntType __f>
  238. constexpr size_t
  239. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  240. __s, __b, __t, __c, __l, __f>::tempering_t;
  241. template<typename _UIntType,
  242. size_t __w, size_t __n, size_t __m, size_t __r,
  243. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  244. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  245. _UIntType __f>
  246. constexpr _UIntType
  247. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  248. __s, __b, __t, __c, __l, __f>::tempering_c;
  249. template<typename _UIntType,
  250. size_t __w, size_t __n, size_t __m, size_t __r,
  251. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  252. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  253. _UIntType __f>
  254. constexpr size_t
  255. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  256. __s, __b, __t, __c, __l, __f>::tempering_l;
  257. template<typename _UIntType,
  258. size_t __w, size_t __n, size_t __m, size_t __r,
  259. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  260. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  261. _UIntType __f>
  262. constexpr _UIntType
  263. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  264. __s, __b, __t, __c, __l, __f>::
  265. initialization_multiplier;
  266. template<typename _UIntType,
  267. size_t __w, size_t __n, size_t __m, size_t __r,
  268. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  269. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  270. _UIntType __f>
  271. constexpr _UIntType
  272. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  273. __s, __b, __t, __c, __l, __f>::default_seed;
  274. template<typename _UIntType,
  275. size_t __w, size_t __n, size_t __m, size_t __r,
  276. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  277. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  278. _UIntType __f>
  279. void
  280. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  281. __s, __b, __t, __c, __l, __f>::
  282. seed(result_type __sd)
  283. {
  284. _M_x[0] = __detail::__mod<_UIntType,
  285. __detail::_Shift<_UIntType, __w>::__value>(__sd);
  286. for (size_t __i = 1; __i < state_size; ++__i)
  287. {
  288. _UIntType __x = _M_x[__i - 1];
  289. __x ^= __x >> (__w - 2);
  290. __x *= __f;
  291. __x += __detail::__mod<_UIntType, __n>(__i);
  292. _M_x[__i] = __detail::__mod<_UIntType,
  293. __detail::_Shift<_UIntType, __w>::__value>(__x);
  294. }
  295. _M_p = state_size;
  296. }
  297. template<typename _UIntType,
  298. size_t __w, size_t __n, size_t __m, size_t __r,
  299. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  300. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  301. _UIntType __f>
  302. template<typename _Sseq>
  303. auto
  304. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  305. __s, __b, __t, __c, __l, __f>::
  306. seed(_Sseq& __q)
  307. -> _If_seed_seq<_Sseq>
  308. {
  309. const _UIntType __upper_mask = (~_UIntType()) << __r;
  310. const size_t __k = (__w + 31) / 32;
  311. uint_least32_t __arr[__n * __k];
  312. __q.generate(__arr + 0, __arr + __n * __k);
  313. bool __zero = true;
  314. for (size_t __i = 0; __i < state_size; ++__i)
  315. {
  316. _UIntType __factor = 1u;
  317. _UIntType __sum = 0u;
  318. for (size_t __j = 0; __j < __k; ++__j)
  319. {
  320. __sum += __arr[__k * __i + __j] * __factor;
  321. __factor *= __detail::_Shift<_UIntType, 32>::__value;
  322. }
  323. _M_x[__i] = __detail::__mod<_UIntType,
  324. __detail::_Shift<_UIntType, __w>::__value>(__sum);
  325. if (__zero)
  326. {
  327. if (__i == 0)
  328. {
  329. if ((_M_x[0] & __upper_mask) != 0u)
  330. __zero = false;
  331. }
  332. else if (_M_x[__i] != 0u)
  333. __zero = false;
  334. }
  335. }
  336. if (__zero)
  337. _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
  338. _M_p = state_size;
  339. }
  340. template<typename _UIntType, size_t __w,
  341. size_t __n, size_t __m, size_t __r,
  342. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  343. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  344. _UIntType __f>
  345. void
  346. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  347. __s, __b, __t, __c, __l, __f>::
  348. _M_gen_rand(void)
  349. {
  350. const _UIntType __upper_mask = (~_UIntType()) << __r;
  351. const _UIntType __lower_mask = ~__upper_mask;
  352. for (size_t __k = 0; __k < (__n - __m); ++__k)
  353. {
  354. _UIntType __y = ((_M_x[__k] & __upper_mask)
  355. | (_M_x[__k + 1] & __lower_mask));
  356. _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
  357. ^ ((__y & 0x01) ? __a : 0));
  358. }
  359. for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
  360. {
  361. _UIntType __y = ((_M_x[__k] & __upper_mask)
  362. | (_M_x[__k + 1] & __lower_mask));
  363. _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
  364. ^ ((__y & 0x01) ? __a : 0));
  365. }
  366. _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
  367. | (_M_x[0] & __lower_mask));
  368. _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
  369. ^ ((__y & 0x01) ? __a : 0));
  370. _M_p = 0;
  371. }
  372. template<typename _UIntType, size_t __w,
  373. size_t __n, size_t __m, size_t __r,
  374. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  375. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  376. _UIntType __f>
  377. void
  378. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  379. __s, __b, __t, __c, __l, __f>::
  380. discard(unsigned long long __z)
  381. {
  382. while (__z > state_size - _M_p)
  383. {
  384. __z -= state_size - _M_p;
  385. _M_gen_rand();
  386. }
  387. _M_p += __z;
  388. }
  389. template<typename _UIntType, size_t __w,
  390. size_t __n, size_t __m, size_t __r,
  391. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  392. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  393. _UIntType __f>
  394. typename
  395. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  396. __s, __b, __t, __c, __l, __f>::result_type
  397. mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
  398. __s, __b, __t, __c, __l, __f>::
  399. operator()()
  400. {
  401. // Reload the vector - cost is O(n) amortized over n calls.
  402. if (_M_p >= state_size)
  403. _M_gen_rand();
  404. // Calculate o(x(i)).
  405. result_type __z = _M_x[_M_p++];
  406. __z ^= (__z >> __u) & __d;
  407. __z ^= (__z << __s) & __b;
  408. __z ^= (__z << __t) & __c;
  409. __z ^= (__z >> __l);
  410. return __z;
  411. }
  412. template<typename _UIntType, size_t __w,
  413. size_t __n, size_t __m, size_t __r,
  414. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  415. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  416. _UIntType __f, typename _CharT, typename _Traits>
  417. std::basic_ostream<_CharT, _Traits>&
  418. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  419. const mersenne_twister_engine<_UIntType, __w, __n, __m,
  420. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
  421. {
  422. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  423. const typename __ios_base::fmtflags __flags = __os.flags();
  424. const _CharT __fill = __os.fill();
  425. const _CharT __space = __os.widen(' ');
  426. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  427. __os.fill(__space);
  428. for (size_t __i = 0; __i < __n; ++__i)
  429. __os << __x._M_x[__i] << __space;
  430. __os << __x._M_p;
  431. __os.flags(__flags);
  432. __os.fill(__fill);
  433. return __os;
  434. }
  435. template<typename _UIntType, size_t __w,
  436. size_t __n, size_t __m, size_t __r,
  437. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  438. _UIntType __b, size_t __t, _UIntType __c, size_t __l,
  439. _UIntType __f, typename _CharT, typename _Traits>
  440. std::basic_istream<_CharT, _Traits>&
  441. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  442. mersenne_twister_engine<_UIntType, __w, __n, __m,
  443. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
  444. {
  445. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  446. const typename __ios_base::fmtflags __flags = __is.flags();
  447. __is.flags(__ios_base::dec | __ios_base::skipws);
  448. for (size_t __i = 0; __i < __n; ++__i)
  449. __is >> __x._M_x[__i];
  450. __is >> __x._M_p;
  451. __is.flags(__flags);
  452. return __is;
  453. }
  454. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  455. constexpr size_t
  456. subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
  457. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  458. constexpr size_t
  459. subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
  460. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  461. constexpr size_t
  462. subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
  463. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  464. constexpr _UIntType
  465. subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
  466. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  467. void
  468. subtract_with_carry_engine<_UIntType, __w, __s, __r>::
  469. seed(result_type __value)
  470. {
  471. std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
  472. __lcg(__value == 0u ? default_seed : __value);
  473. const size_t __n = (__w + 31) / 32;
  474. for (size_t __i = 0; __i < long_lag; ++__i)
  475. {
  476. _UIntType __sum = 0u;
  477. _UIntType __factor = 1u;
  478. for (size_t __j = 0; __j < __n; ++__j)
  479. {
  480. __sum += __detail::__mod<uint_least32_t,
  481. __detail::_Shift<uint_least32_t, 32>::__value>
  482. (__lcg()) * __factor;
  483. __factor *= __detail::_Shift<_UIntType, 32>::__value;
  484. }
  485. _M_x[__i] = __detail::__mod<_UIntType,
  486. __detail::_Shift<_UIntType, __w>::__value>(__sum);
  487. }
  488. _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
  489. _M_p = 0;
  490. }
  491. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  492. template<typename _Sseq>
  493. auto
  494. subtract_with_carry_engine<_UIntType, __w, __s, __r>::
  495. seed(_Sseq& __q)
  496. -> _If_seed_seq<_Sseq>
  497. {
  498. const size_t __k = (__w + 31) / 32;
  499. uint_least32_t __arr[__r * __k];
  500. __q.generate(__arr + 0, __arr + __r * __k);
  501. for (size_t __i = 0; __i < long_lag; ++__i)
  502. {
  503. _UIntType __sum = 0u;
  504. _UIntType __factor = 1u;
  505. for (size_t __j = 0; __j < __k; ++__j)
  506. {
  507. __sum += __arr[__k * __i + __j] * __factor;
  508. __factor *= __detail::_Shift<_UIntType, 32>::__value;
  509. }
  510. _M_x[__i] = __detail::__mod<_UIntType,
  511. __detail::_Shift<_UIntType, __w>::__value>(__sum);
  512. }
  513. _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
  514. _M_p = 0;
  515. }
  516. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  517. typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
  518. result_type
  519. subtract_with_carry_engine<_UIntType, __w, __s, __r>::
  520. operator()()
  521. {
  522. // Derive short lag index from current index.
  523. long __ps = _M_p - short_lag;
  524. if (__ps < 0)
  525. __ps += long_lag;
  526. // Calculate new x(i) without overflow or division.
  527. // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
  528. // cannot overflow.
  529. _UIntType __xi;
  530. if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
  531. {
  532. __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
  533. _M_carry = 0;
  534. }
  535. else
  536. {
  537. __xi = (__detail::_Shift<_UIntType, __w>::__value
  538. - _M_x[_M_p] - _M_carry + _M_x[__ps]);
  539. _M_carry = 1;
  540. }
  541. _M_x[_M_p] = __xi;
  542. // Adjust current index to loop around in ring buffer.
  543. if (++_M_p >= long_lag)
  544. _M_p = 0;
  545. return __xi;
  546. }
  547. template<typename _UIntType, size_t __w, size_t __s, size_t __r,
  548. typename _CharT, typename _Traits>
  549. std::basic_ostream<_CharT, _Traits>&
  550. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  551. const subtract_with_carry_engine<_UIntType,
  552. __w, __s, __r>& __x)
  553. {
  554. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  555. const typename __ios_base::fmtflags __flags = __os.flags();
  556. const _CharT __fill = __os.fill();
  557. const _CharT __space = __os.widen(' ');
  558. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  559. __os.fill(__space);
  560. for (size_t __i = 0; __i < __r; ++__i)
  561. __os << __x._M_x[__i] << __space;
  562. __os << __x._M_carry << __space << __x._M_p;
  563. __os.flags(__flags);
  564. __os.fill(__fill);
  565. return __os;
  566. }
  567. template<typename _UIntType, size_t __w, size_t __s, size_t __r,
  568. typename _CharT, typename _Traits>
  569. std::basic_istream<_CharT, _Traits>&
  570. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  571. subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
  572. {
  573. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  574. const typename __ios_base::fmtflags __flags = __is.flags();
  575. __is.flags(__ios_base::dec | __ios_base::skipws);
  576. for (size_t __i = 0; __i < __r; ++__i)
  577. __is >> __x._M_x[__i];
  578. __is >> __x._M_carry;
  579. __is >> __x._M_p;
  580. __is.flags(__flags);
  581. return __is;
  582. }
  583. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  584. constexpr size_t
  585. discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
  586. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  587. constexpr size_t
  588. discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
  589. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  590. typename discard_block_engine<_RandomNumberEngine,
  591. __p, __r>::result_type
  592. discard_block_engine<_RandomNumberEngine, __p, __r>::
  593. operator()()
  594. {
  595. if (_M_n >= used_block)
  596. {
  597. _M_b.discard(block_size - _M_n);
  598. _M_n = 0;
  599. }
  600. ++_M_n;
  601. return _M_b();
  602. }
  603. template<typename _RandomNumberEngine, size_t __p, size_t __r,
  604. typename _CharT, typename _Traits>
  605. std::basic_ostream<_CharT, _Traits>&
  606. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  607. const discard_block_engine<_RandomNumberEngine,
  608. __p, __r>& __x)
  609. {
  610. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  611. const typename __ios_base::fmtflags __flags = __os.flags();
  612. const _CharT __fill = __os.fill();
  613. const _CharT __space = __os.widen(' ');
  614. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  615. __os.fill(__space);
  616. __os << __x.base() << __space << __x._M_n;
  617. __os.flags(__flags);
  618. __os.fill(__fill);
  619. return __os;
  620. }
  621. template<typename _RandomNumberEngine, size_t __p, size_t __r,
  622. typename _CharT, typename _Traits>
  623. std::basic_istream<_CharT, _Traits>&
  624. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  625. discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
  626. {
  627. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  628. const typename __ios_base::fmtflags __flags = __is.flags();
  629. __is.flags(__ios_base::dec | __ios_base::skipws);
  630. __is >> __x._M_b >> __x._M_n;
  631. __is.flags(__flags);
  632. return __is;
  633. }
  634. template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
  635. typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
  636. result_type
  637. independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
  638. operator()()
  639. {
  640. typedef typename _RandomNumberEngine::result_type _Eresult_type;
  641. const _Eresult_type __r
  642. = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
  643. ? _M_b.max() - _M_b.min() + 1 : 0);
  644. const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
  645. const unsigned __m = __r ? std::__lg(__r) : __edig;
  646. typedef typename std::common_type<_Eresult_type, result_type>::type
  647. __ctype;
  648. const unsigned __cdig = std::numeric_limits<__ctype>::digits;
  649. unsigned __n, __n0;
  650. __ctype __s0, __s1, __y0, __y1;
  651. for (size_t __i = 0; __i < 2; ++__i)
  652. {
  653. __n = (__w + __m - 1) / __m + __i;
  654. __n0 = __n - __w % __n;
  655. const unsigned __w0 = __w / __n; // __w0 <= __m
  656. __s0 = 0;
  657. __s1 = 0;
  658. if (__w0 < __cdig)
  659. {
  660. __s0 = __ctype(1) << __w0;
  661. __s1 = __s0 << 1;
  662. }
  663. __y0 = 0;
  664. __y1 = 0;
  665. if (__r)
  666. {
  667. __y0 = __s0 * (__r / __s0);
  668. if (__s1)
  669. __y1 = __s1 * (__r / __s1);
  670. if (__r - __y0 <= __y0 / __n)
  671. break;
  672. }
  673. else
  674. break;
  675. }
  676. result_type __sum = 0;
  677. for (size_t __k = 0; __k < __n0; ++__k)
  678. {
  679. __ctype __u;
  680. do
  681. __u = _M_b() - _M_b.min();
  682. while (__y0 && __u >= __y0);
  683. __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
  684. }
  685. for (size_t __k = __n0; __k < __n; ++__k)
  686. {
  687. __ctype __u;
  688. do
  689. __u = _M_b() - _M_b.min();
  690. while (__y1 && __u >= __y1);
  691. __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
  692. }
  693. return __sum;
  694. }
  695. template<typename _RandomNumberEngine, size_t __k>
  696. constexpr size_t
  697. shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
  698. namespace __detail
  699. {
  700. // Determine whether an integer is representable as double.
  701. template<typename _Tp>
  702. constexpr bool
  703. __representable_as_double(_Tp __x) noexcept
  704. {
  705. static_assert(numeric_limits<_Tp>::is_integer);
  706. static_assert(!numeric_limits<_Tp>::is_signed);
  707. // All integers <= 2^53 are representable.
  708. return (__x <= (1ull << __DBL_MANT_DIG__))
  709. // Between 2^53 and 2^54 only even numbers are representable.
  710. || (!(__x & 1) && __detail::__representable_as_double(__x >> 1));
  711. }
  712. // Determine whether x+1 is representable as double.
  713. template<typename _Tp>
  714. constexpr bool
  715. __p1_representable_as_double(_Tp __x) noexcept
  716. {
  717. static_assert(numeric_limits<_Tp>::is_integer);
  718. static_assert(!numeric_limits<_Tp>::is_signed);
  719. return numeric_limits<_Tp>::digits < __DBL_MANT_DIG__
  720. || (bool(__x + 1u) // return false if x+1 wraps around to zero
  721. && __detail::__representable_as_double(__x + 1u));
  722. }
  723. }
  724. template<typename _RandomNumberEngine, size_t __k>
  725. typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
  726. shuffle_order_engine<_RandomNumberEngine, __k>::
  727. operator()()
  728. {
  729. constexpr result_type __range = max() - min();
  730. size_t __j = __k;
  731. const result_type __y = _M_y - min();
  732. // Avoid using slower long double arithmetic if possible.
  733. if _GLIBCXX17_CONSTEXPR (__detail::__p1_representable_as_double(__range))
  734. __j *= __y / (__range + 1.0);
  735. else
  736. __j *= __y / (__range + 1.0L);
  737. _M_y = _M_v[__j];
  738. _M_v[__j] = _M_b();
  739. return _M_y;
  740. }
  741. template<typename _RandomNumberEngine, size_t __k,
  742. typename _CharT, typename _Traits>
  743. std::basic_ostream<_CharT, _Traits>&
  744. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  745. const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
  746. {
  747. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  748. const typename __ios_base::fmtflags __flags = __os.flags();
  749. const _CharT __fill = __os.fill();
  750. const _CharT __space = __os.widen(' ');
  751. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  752. __os.fill(__space);
  753. __os << __x.base();
  754. for (size_t __i = 0; __i < __k; ++__i)
  755. __os << __space << __x._M_v[__i];
  756. __os << __space << __x._M_y;
  757. __os.flags(__flags);
  758. __os.fill(__fill);
  759. return __os;
  760. }
  761. template<typename _RandomNumberEngine, size_t __k,
  762. typename _CharT, typename _Traits>
  763. std::basic_istream<_CharT, _Traits>&
  764. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  765. shuffle_order_engine<_RandomNumberEngine, __k>& __x)
  766. {
  767. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  768. const typename __ios_base::fmtflags __flags = __is.flags();
  769. __is.flags(__ios_base::dec | __ios_base::skipws);
  770. __is >> __x._M_b;
  771. for (size_t __i = 0; __i < __k; ++__i)
  772. __is >> __x._M_v[__i];
  773. __is >> __x._M_y;
  774. __is.flags(__flags);
  775. return __is;
  776. }
  777. template<typename _IntType, typename _CharT, typename _Traits>
  778. std::basic_ostream<_CharT, _Traits>&
  779. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  780. const uniform_int_distribution<_IntType>& __x)
  781. {
  782. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  783. const typename __ios_base::fmtflags __flags = __os.flags();
  784. const _CharT __fill = __os.fill();
  785. const _CharT __space = __os.widen(' ');
  786. __os.flags(__ios_base::scientific | __ios_base::left);
  787. __os.fill(__space);
  788. __os << __x.a() << __space << __x.b();
  789. __os.flags(__flags);
  790. __os.fill(__fill);
  791. return __os;
  792. }
  793. template<typename _IntType, typename _CharT, typename _Traits>
  794. std::basic_istream<_CharT, _Traits>&
  795. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  796. uniform_int_distribution<_IntType>& __x)
  797. {
  798. using param_type
  799. = typename uniform_int_distribution<_IntType>::param_type;
  800. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  801. const typename __ios_base::fmtflags __flags = __is.flags();
  802. __is.flags(__ios_base::dec | __ios_base::skipws);
  803. _IntType __a, __b;
  804. if (__is >> __a >> __b)
  805. __x.param(param_type(__a, __b));
  806. __is.flags(__flags);
  807. return __is;
  808. }
  809. template<typename _RealType>
  810. template<typename _ForwardIterator,
  811. typename _UniformRandomNumberGenerator>
  812. void
  813. uniform_real_distribution<_RealType>::
  814. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  815. _UniformRandomNumberGenerator& __urng,
  816. const param_type& __p)
  817. {
  818. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  819. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  820. __aurng(__urng);
  821. auto __range = __p.b() - __p.a();
  822. while (__f != __t)
  823. *__f++ = __aurng() * __range + __p.a();
  824. }
  825. template<typename _RealType, typename _CharT, typename _Traits>
  826. std::basic_ostream<_CharT, _Traits>&
  827. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  828. const uniform_real_distribution<_RealType>& __x)
  829. {
  830. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  831. const typename __ios_base::fmtflags __flags = __os.flags();
  832. const _CharT __fill = __os.fill();
  833. const std::streamsize __precision = __os.precision();
  834. const _CharT __space = __os.widen(' ');
  835. __os.flags(__ios_base::scientific | __ios_base::left);
  836. __os.fill(__space);
  837. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  838. __os << __x.a() << __space << __x.b();
  839. __os.flags(__flags);
  840. __os.fill(__fill);
  841. __os.precision(__precision);
  842. return __os;
  843. }
  844. template<typename _RealType, typename _CharT, typename _Traits>
  845. std::basic_istream<_CharT, _Traits>&
  846. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  847. uniform_real_distribution<_RealType>& __x)
  848. {
  849. using param_type
  850. = typename uniform_real_distribution<_RealType>::param_type;
  851. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  852. const typename __ios_base::fmtflags __flags = __is.flags();
  853. __is.flags(__ios_base::skipws);
  854. _RealType __a, __b;
  855. if (__is >> __a >> __b)
  856. __x.param(param_type(__a, __b));
  857. __is.flags(__flags);
  858. return __is;
  859. }
  860. template<typename _ForwardIterator,
  861. typename _UniformRandomNumberGenerator>
  862. void
  863. std::bernoulli_distribution::
  864. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  865. _UniformRandomNumberGenerator& __urng,
  866. const param_type& __p)
  867. {
  868. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  869. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  870. __aurng(__urng);
  871. auto __limit = __p.p() * (__aurng.max() - __aurng.min());
  872. while (__f != __t)
  873. *__f++ = (__aurng() - __aurng.min()) < __limit;
  874. }
  875. template<typename _CharT, typename _Traits>
  876. std::basic_ostream<_CharT, _Traits>&
  877. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  878. const bernoulli_distribution& __x)
  879. {
  880. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  881. const typename __ios_base::fmtflags __flags = __os.flags();
  882. const _CharT __fill = __os.fill();
  883. const std::streamsize __precision = __os.precision();
  884. __os.flags(__ios_base::scientific | __ios_base::left);
  885. __os.fill(__os.widen(' '));
  886. __os.precision(std::numeric_limits<double>::max_digits10);
  887. __os << __x.p();
  888. __os.flags(__flags);
  889. __os.fill(__fill);
  890. __os.precision(__precision);
  891. return __os;
  892. }
  893. template<typename _IntType>
  894. template<typename _UniformRandomNumberGenerator>
  895. typename geometric_distribution<_IntType>::result_type
  896. geometric_distribution<_IntType>::
  897. operator()(_UniformRandomNumberGenerator& __urng,
  898. const param_type& __param)
  899. {
  900. // About the epsilon thing see this thread:
  901. // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
  902. const double __naf =
  903. (1 - std::numeric_limits<double>::epsilon()) / 2;
  904. // The largest _RealType convertible to _IntType.
  905. const double __thr =
  906. std::numeric_limits<_IntType>::max() + __naf;
  907. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  908. __aurng(__urng);
  909. double __cand;
  910. do
  911. __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
  912. while (__cand >= __thr);
  913. return result_type(__cand + __naf);
  914. }
  915. template<typename _IntType>
  916. template<typename _ForwardIterator,
  917. typename _UniformRandomNumberGenerator>
  918. void
  919. geometric_distribution<_IntType>::
  920. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  921. _UniformRandomNumberGenerator& __urng,
  922. const param_type& __param)
  923. {
  924. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  925. // About the epsilon thing see this thread:
  926. // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
  927. const double __naf =
  928. (1 - std::numeric_limits<double>::epsilon()) / 2;
  929. // The largest _RealType convertible to _IntType.
  930. const double __thr =
  931. std::numeric_limits<_IntType>::max() + __naf;
  932. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  933. __aurng(__urng);
  934. while (__f != __t)
  935. {
  936. double __cand;
  937. do
  938. __cand = std::floor(std::log(1.0 - __aurng())
  939. / __param._M_log_1_p);
  940. while (__cand >= __thr);
  941. *__f++ = __cand + __naf;
  942. }
  943. }
  944. template<typename _IntType,
  945. typename _CharT, typename _Traits>
  946. std::basic_ostream<_CharT, _Traits>&
  947. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  948. const geometric_distribution<_IntType>& __x)
  949. {
  950. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  951. const typename __ios_base::fmtflags __flags = __os.flags();
  952. const _CharT __fill = __os.fill();
  953. const std::streamsize __precision = __os.precision();
  954. __os.flags(__ios_base::scientific | __ios_base::left);
  955. __os.fill(__os.widen(' '));
  956. __os.precision(std::numeric_limits<double>::max_digits10);
  957. __os << __x.p();
  958. __os.flags(__flags);
  959. __os.fill(__fill);
  960. __os.precision(__precision);
  961. return __os;
  962. }
  963. template<typename _IntType,
  964. typename _CharT, typename _Traits>
  965. std::basic_istream<_CharT, _Traits>&
  966. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  967. geometric_distribution<_IntType>& __x)
  968. {
  969. using param_type = typename geometric_distribution<_IntType>::param_type;
  970. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  971. const typename __ios_base::fmtflags __flags = __is.flags();
  972. __is.flags(__ios_base::skipws);
  973. double __p;
  974. if (__is >> __p)
  975. __x.param(param_type(__p));
  976. __is.flags(__flags);
  977. return __is;
  978. }
  979. // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
  980. template<typename _IntType>
  981. template<typename _UniformRandomNumberGenerator>
  982. typename negative_binomial_distribution<_IntType>::result_type
  983. negative_binomial_distribution<_IntType>::
  984. operator()(_UniformRandomNumberGenerator& __urng)
  985. {
  986. const double __y = _M_gd(__urng);
  987. // XXX Is the constructor too slow?
  988. std::poisson_distribution<result_type> __poisson(__y);
  989. return __poisson(__urng);
  990. }
  991. template<typename _IntType>
  992. template<typename _UniformRandomNumberGenerator>
  993. typename negative_binomial_distribution<_IntType>::result_type
  994. negative_binomial_distribution<_IntType>::
  995. operator()(_UniformRandomNumberGenerator& __urng,
  996. const param_type& __p)
  997. {
  998. typedef typename std::gamma_distribution<double>::param_type
  999. param_type;
  1000. const double __y =
  1001. _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
  1002. std::poisson_distribution<result_type> __poisson(__y);
  1003. return __poisson(__urng);
  1004. }
  1005. template<typename _IntType>
  1006. template<typename _ForwardIterator,
  1007. typename _UniformRandomNumberGenerator>
  1008. void
  1009. negative_binomial_distribution<_IntType>::
  1010. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1011. _UniformRandomNumberGenerator& __urng)
  1012. {
  1013. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1014. while (__f != __t)
  1015. {
  1016. const double __y = _M_gd(__urng);
  1017. // XXX Is the constructor too slow?
  1018. std::poisson_distribution<result_type> __poisson(__y);
  1019. *__f++ = __poisson(__urng);
  1020. }
  1021. }
  1022. template<typename _IntType>
  1023. template<typename _ForwardIterator,
  1024. typename _UniformRandomNumberGenerator>
  1025. void
  1026. negative_binomial_distribution<_IntType>::
  1027. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1028. _UniformRandomNumberGenerator& __urng,
  1029. const param_type& __p)
  1030. {
  1031. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1032. typename std::gamma_distribution<result_type>::param_type
  1033. __p2(__p.k(), (1.0 - __p.p()) / __p.p());
  1034. while (__f != __t)
  1035. {
  1036. const double __y = _M_gd(__urng, __p2);
  1037. std::poisson_distribution<result_type> __poisson(__y);
  1038. *__f++ = __poisson(__urng);
  1039. }
  1040. }
  1041. template<typename _IntType, typename _CharT, typename _Traits>
  1042. std::basic_ostream<_CharT, _Traits>&
  1043. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1044. const negative_binomial_distribution<_IntType>& __x)
  1045. {
  1046. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1047. const typename __ios_base::fmtflags __flags = __os.flags();
  1048. const _CharT __fill = __os.fill();
  1049. const std::streamsize __precision = __os.precision();
  1050. const _CharT __space = __os.widen(' ');
  1051. __os.flags(__ios_base::scientific | __ios_base::left);
  1052. __os.fill(__os.widen(' '));
  1053. __os.precision(std::numeric_limits<double>::max_digits10);
  1054. __os << __x.k() << __space << __x.p()
  1055. << __space << __x._M_gd;
  1056. __os.flags(__flags);
  1057. __os.fill(__fill);
  1058. __os.precision(__precision);
  1059. return __os;
  1060. }
  1061. template<typename _IntType, typename _CharT, typename _Traits>
  1062. std::basic_istream<_CharT, _Traits>&
  1063. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1064. negative_binomial_distribution<_IntType>& __x)
  1065. {
  1066. using param_type
  1067. = typename negative_binomial_distribution<_IntType>::param_type;
  1068. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1069. const typename __ios_base::fmtflags __flags = __is.flags();
  1070. __is.flags(__ios_base::skipws);
  1071. _IntType __k;
  1072. double __p;
  1073. if (__is >> __k >> __p >> __x._M_gd)
  1074. __x.param(param_type(__k, __p));
  1075. __is.flags(__flags);
  1076. return __is;
  1077. }
  1078. template<typename _IntType>
  1079. void
  1080. poisson_distribution<_IntType>::param_type::
  1081. _M_initialize()
  1082. {
  1083. #if _GLIBCXX_USE_C99_MATH_TR1
  1084. if (_M_mean >= 12)
  1085. {
  1086. const double __m = std::floor(_M_mean);
  1087. _M_lm_thr = std::log(_M_mean);
  1088. _M_lfm = std::lgamma(__m + 1);
  1089. _M_sm = std::sqrt(__m);
  1090. const double __pi_4 = 0.7853981633974483096156608458198757L;
  1091. const double __dx = std::sqrt(2 * __m * std::log(32 * __m
  1092. / __pi_4));
  1093. _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx)));
  1094. const double __cx = 2 * __m + _M_d;
  1095. _M_scx = std::sqrt(__cx / 2);
  1096. _M_1cx = 1 / __cx;
  1097. _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
  1098. _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
  1099. / _M_d;
  1100. }
  1101. else
  1102. #endif
  1103. _M_lm_thr = std::exp(-_M_mean);
  1104. }
  1105. /**
  1106. * A rejection algorithm when mean >= 12 and a simple method based
  1107. * upon the multiplication of uniform random variates otherwise.
  1108. * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
  1109. * is defined.
  1110. *
  1111. * Reference:
  1112. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1113. * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
  1114. */
  1115. template<typename _IntType>
  1116. template<typename _UniformRandomNumberGenerator>
  1117. typename poisson_distribution<_IntType>::result_type
  1118. poisson_distribution<_IntType>::
  1119. operator()(_UniformRandomNumberGenerator& __urng,
  1120. const param_type& __param)
  1121. {
  1122. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  1123. __aurng(__urng);
  1124. #if _GLIBCXX_USE_C99_MATH_TR1
  1125. if (__param.mean() >= 12)
  1126. {
  1127. double __x;
  1128. // See comments above...
  1129. const double __naf =
  1130. (1 - std::numeric_limits<double>::epsilon()) / 2;
  1131. const double __thr =
  1132. std::numeric_limits<_IntType>::max() + __naf;
  1133. const double __m = std::floor(__param.mean());
  1134. // sqrt(pi / 2)
  1135. const double __spi_2 = 1.2533141373155002512078826424055226L;
  1136. const double __c1 = __param._M_sm * __spi_2;
  1137. const double __c2 = __param._M_c2b + __c1;
  1138. const double __c3 = __c2 + 1;
  1139. const double __c4 = __c3 + 1;
  1140. // 1 / 78
  1141. const double __178 = 0.0128205128205128205128205128205128L;
  1142. // e^(1 / 78)
  1143. const double __e178 = 1.0129030479320018583185514777512983L;
  1144. const double __c5 = __c4 + __e178;
  1145. const double __c = __param._M_cb + __c5;
  1146. const double __2cx = 2 * (2 * __m + __param._M_d);
  1147. bool __reject = true;
  1148. do
  1149. {
  1150. const double __u = __c * __aurng();
  1151. const double __e = -std::log(1.0 - __aurng());
  1152. double __w = 0.0;
  1153. if (__u <= __c1)
  1154. {
  1155. const double __n = _M_nd(__urng);
  1156. const double __y = -std::abs(__n) * __param._M_sm - 1;
  1157. __x = std::floor(__y);
  1158. __w = -__n * __n / 2;
  1159. if (__x < -__m)
  1160. continue;
  1161. }
  1162. else if (__u <= __c2)
  1163. {
  1164. const double __n = _M_nd(__urng);
  1165. const double __y = 1 + std::abs(__n) * __param._M_scx;
  1166. __x = std::ceil(__y);
  1167. __w = __y * (2 - __y) * __param._M_1cx;
  1168. if (__x > __param._M_d)
  1169. continue;
  1170. }
  1171. else if (__u <= __c3)
  1172. // NB: This case not in the book, nor in the Errata,
  1173. // but should be ok...
  1174. __x = -1;
  1175. else if (__u <= __c4)
  1176. __x = 0;
  1177. else if (__u <= __c5)
  1178. {
  1179. __x = 1;
  1180. // Only in the Errata, see libstdc++/83237.
  1181. __w = __178;
  1182. }
  1183. else
  1184. {
  1185. const double __v = -std::log(1.0 - __aurng());
  1186. const double __y = __param._M_d
  1187. + __v * __2cx / __param._M_d;
  1188. __x = std::ceil(__y);
  1189. __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
  1190. }
  1191. __reject = (__w - __e - __x * __param._M_lm_thr
  1192. > __param._M_lfm - std::lgamma(__x + __m + 1));
  1193. __reject |= __x + __m >= __thr;
  1194. } while (__reject);
  1195. return result_type(__x + __m + __naf);
  1196. }
  1197. else
  1198. #endif
  1199. {
  1200. _IntType __x = 0;
  1201. double __prod = 1.0;
  1202. do
  1203. {
  1204. __prod *= __aurng();
  1205. __x += 1;
  1206. }
  1207. while (__prod > __param._M_lm_thr);
  1208. return __x - 1;
  1209. }
  1210. }
  1211. template<typename _IntType>
  1212. template<typename _ForwardIterator,
  1213. typename _UniformRandomNumberGenerator>
  1214. void
  1215. poisson_distribution<_IntType>::
  1216. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1217. _UniformRandomNumberGenerator& __urng,
  1218. const param_type& __param)
  1219. {
  1220. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1221. // We could duplicate everything from operator()...
  1222. while (__f != __t)
  1223. *__f++ = this->operator()(__urng, __param);
  1224. }
  1225. template<typename _IntType,
  1226. typename _CharT, typename _Traits>
  1227. std::basic_ostream<_CharT, _Traits>&
  1228. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1229. const poisson_distribution<_IntType>& __x)
  1230. {
  1231. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1232. const typename __ios_base::fmtflags __flags = __os.flags();
  1233. const _CharT __fill = __os.fill();
  1234. const std::streamsize __precision = __os.precision();
  1235. const _CharT __space = __os.widen(' ');
  1236. __os.flags(__ios_base::scientific | __ios_base::left);
  1237. __os.fill(__space);
  1238. __os.precision(std::numeric_limits<double>::max_digits10);
  1239. __os << __x.mean() << __space << __x._M_nd;
  1240. __os.flags(__flags);
  1241. __os.fill(__fill);
  1242. __os.precision(__precision);
  1243. return __os;
  1244. }
  1245. template<typename _IntType,
  1246. typename _CharT, typename _Traits>
  1247. std::basic_istream<_CharT, _Traits>&
  1248. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1249. poisson_distribution<_IntType>& __x)
  1250. {
  1251. using param_type = typename poisson_distribution<_IntType>::param_type;
  1252. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1253. const typename __ios_base::fmtflags __flags = __is.flags();
  1254. __is.flags(__ios_base::skipws);
  1255. double __mean;
  1256. if (__is >> __mean >> __x._M_nd)
  1257. __x.param(param_type(__mean));
  1258. __is.flags(__flags);
  1259. return __is;
  1260. }
  1261. template<typename _IntType>
  1262. void
  1263. binomial_distribution<_IntType>::param_type::
  1264. _M_initialize()
  1265. {
  1266. const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
  1267. _M_easy = true;
  1268. #if _GLIBCXX_USE_C99_MATH_TR1
  1269. if (_M_t * __p12 >= 8)
  1270. {
  1271. _M_easy = false;
  1272. const double __np = std::floor(_M_t * __p12);
  1273. const double __pa = __np / _M_t;
  1274. const double __1p = 1 - __pa;
  1275. const double __pi_4 = 0.7853981633974483096156608458198757L;
  1276. const double __d1x =
  1277. std::sqrt(__np * __1p * std::log(32 * __np
  1278. / (81 * __pi_4 * __1p)));
  1279. _M_d1 = std::round(std::max<double>(1.0, __d1x));
  1280. const double __d2x =
  1281. std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
  1282. / (__pi_4 * __pa)));
  1283. _M_d2 = std::round(std::max<double>(1.0, __d2x));
  1284. // sqrt(pi / 2)
  1285. const double __spi_2 = 1.2533141373155002512078826424055226L;
  1286. _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
  1287. _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
  1288. _M_c = 2 * _M_d1 / __np;
  1289. _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
  1290. const double __a12 = _M_a1 + _M_s2 * __spi_2;
  1291. const double __s1s = _M_s1 * _M_s1;
  1292. _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
  1293. * 2 * __s1s / _M_d1
  1294. * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
  1295. const double __s2s = _M_s2 * _M_s2;
  1296. _M_s = (_M_a123 + 2 * __s2s / _M_d2
  1297. * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
  1298. _M_lf = (std::lgamma(__np + 1)
  1299. + std::lgamma(_M_t - __np + 1));
  1300. _M_lp1p = std::log(__pa / __1p);
  1301. _M_q = -std::log(1 - (__p12 - __pa) / __1p);
  1302. }
  1303. else
  1304. #endif
  1305. _M_q = -std::log(1 - __p12);
  1306. }
  1307. template<typename _IntType>
  1308. template<typename _UniformRandomNumberGenerator>
  1309. typename binomial_distribution<_IntType>::result_type
  1310. binomial_distribution<_IntType>::
  1311. _M_waiting(_UniformRandomNumberGenerator& __urng,
  1312. _IntType __t, double __q)
  1313. {
  1314. _IntType __x = 0;
  1315. double __sum = 0.0;
  1316. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  1317. __aurng(__urng);
  1318. do
  1319. {
  1320. if (__t == __x)
  1321. return __x;
  1322. const double __e = -std::log(1.0 - __aurng());
  1323. __sum += __e / (__t - __x);
  1324. __x += 1;
  1325. }
  1326. while (__sum <= __q);
  1327. return __x - 1;
  1328. }
  1329. /**
  1330. * A rejection algorithm when t * p >= 8 and a simple waiting time
  1331. * method - the second in the referenced book - otherwise.
  1332. * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
  1333. * is defined.
  1334. *
  1335. * Reference:
  1336. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1337. * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
  1338. */
  1339. template<typename _IntType>
  1340. template<typename _UniformRandomNumberGenerator>
  1341. typename binomial_distribution<_IntType>::result_type
  1342. binomial_distribution<_IntType>::
  1343. operator()(_UniformRandomNumberGenerator& __urng,
  1344. const param_type& __param)
  1345. {
  1346. result_type __ret;
  1347. const _IntType __t = __param.t();
  1348. const double __p = __param.p();
  1349. const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
  1350. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  1351. __aurng(__urng);
  1352. #if _GLIBCXX_USE_C99_MATH_TR1
  1353. if (!__param._M_easy)
  1354. {
  1355. double __x;
  1356. // See comments above...
  1357. const double __naf =
  1358. (1 - std::numeric_limits<double>::epsilon()) / 2;
  1359. const double __thr =
  1360. std::numeric_limits<_IntType>::max() + __naf;
  1361. const double __np = std::floor(__t * __p12);
  1362. // sqrt(pi / 2)
  1363. const double __spi_2 = 1.2533141373155002512078826424055226L;
  1364. const double __a1 = __param._M_a1;
  1365. const double __a12 = __a1 + __param._M_s2 * __spi_2;
  1366. const double __a123 = __param._M_a123;
  1367. const double __s1s = __param._M_s1 * __param._M_s1;
  1368. const double __s2s = __param._M_s2 * __param._M_s2;
  1369. bool __reject;
  1370. do
  1371. {
  1372. const double __u = __param._M_s * __aurng();
  1373. double __v;
  1374. if (__u <= __a1)
  1375. {
  1376. const double __n = _M_nd(__urng);
  1377. const double __y = __param._M_s1 * std::abs(__n);
  1378. __reject = __y >= __param._M_d1;
  1379. if (!__reject)
  1380. {
  1381. const double __e = -std::log(1.0 - __aurng());
  1382. __x = std::floor(__y);
  1383. __v = -__e - __n * __n / 2 + __param._M_c;
  1384. }
  1385. }
  1386. else if (__u <= __a12)
  1387. {
  1388. const double __n = _M_nd(__urng);
  1389. const double __y = __param._M_s2 * std::abs(__n);
  1390. __reject = __y >= __param._M_d2;
  1391. if (!__reject)
  1392. {
  1393. const double __e = -std::log(1.0 - __aurng());
  1394. __x = std::floor(-__y);
  1395. __v = -__e - __n * __n / 2;
  1396. }
  1397. }
  1398. else if (__u <= __a123)
  1399. {
  1400. const double __e1 = -std::log(1.0 - __aurng());
  1401. const double __e2 = -std::log(1.0 - __aurng());
  1402. const double __y = __param._M_d1
  1403. + 2 * __s1s * __e1 / __param._M_d1;
  1404. __x = std::floor(__y);
  1405. __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
  1406. -__y / (2 * __s1s)));
  1407. __reject = false;
  1408. }
  1409. else
  1410. {
  1411. const double __e1 = -std::log(1.0 - __aurng());
  1412. const double __e2 = -std::log(1.0 - __aurng());
  1413. const double __y = __param._M_d2
  1414. + 2 * __s2s * __e1 / __param._M_d2;
  1415. __x = std::floor(-__y);
  1416. __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
  1417. __reject = false;
  1418. }
  1419. __reject = __reject || __x < -__np || __x > __t - __np;
  1420. if (!__reject)
  1421. {
  1422. const double __lfx =
  1423. std::lgamma(__np + __x + 1)
  1424. + std::lgamma(__t - (__np + __x) + 1);
  1425. __reject = __v > __param._M_lf - __lfx
  1426. + __x * __param._M_lp1p;
  1427. }
  1428. __reject |= __x + __np >= __thr;
  1429. }
  1430. while (__reject);
  1431. __x += __np + __naf;
  1432. const _IntType __z = _M_waiting(__urng, __t - _IntType(__x),
  1433. __param._M_q);
  1434. __ret = _IntType(__x) + __z;
  1435. }
  1436. else
  1437. #endif
  1438. __ret = _M_waiting(__urng, __t, __param._M_q);
  1439. if (__p12 != __p)
  1440. __ret = __t - __ret;
  1441. return __ret;
  1442. }
  1443. template<typename _IntType>
  1444. template<typename _ForwardIterator,
  1445. typename _UniformRandomNumberGenerator>
  1446. void
  1447. binomial_distribution<_IntType>::
  1448. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1449. _UniformRandomNumberGenerator& __urng,
  1450. const param_type& __param)
  1451. {
  1452. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1453. // We could duplicate everything from operator()...
  1454. while (__f != __t)
  1455. *__f++ = this->operator()(__urng, __param);
  1456. }
  1457. template<typename _IntType,
  1458. typename _CharT, typename _Traits>
  1459. std::basic_ostream<_CharT, _Traits>&
  1460. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1461. const binomial_distribution<_IntType>& __x)
  1462. {
  1463. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1464. const typename __ios_base::fmtflags __flags = __os.flags();
  1465. const _CharT __fill = __os.fill();
  1466. const std::streamsize __precision = __os.precision();
  1467. const _CharT __space = __os.widen(' ');
  1468. __os.flags(__ios_base::scientific | __ios_base::left);
  1469. __os.fill(__space);
  1470. __os.precision(std::numeric_limits<double>::max_digits10);
  1471. __os << __x.t() << __space << __x.p()
  1472. << __space << __x._M_nd;
  1473. __os.flags(__flags);
  1474. __os.fill(__fill);
  1475. __os.precision(__precision);
  1476. return __os;
  1477. }
  1478. template<typename _IntType,
  1479. typename _CharT, typename _Traits>
  1480. std::basic_istream<_CharT, _Traits>&
  1481. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1482. binomial_distribution<_IntType>& __x)
  1483. {
  1484. using param_type = typename binomial_distribution<_IntType>::param_type;
  1485. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1486. const typename __ios_base::fmtflags __flags = __is.flags();
  1487. __is.flags(__ios_base::dec | __ios_base::skipws);
  1488. _IntType __t;
  1489. double __p;
  1490. if (__is >> __t >> __p >> __x._M_nd)
  1491. __x.param(param_type(__t, __p));
  1492. __is.flags(__flags);
  1493. return __is;
  1494. }
  1495. template<typename _RealType>
  1496. template<typename _ForwardIterator,
  1497. typename _UniformRandomNumberGenerator>
  1498. void
  1499. std::exponential_distribution<_RealType>::
  1500. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1501. _UniformRandomNumberGenerator& __urng,
  1502. const param_type& __p)
  1503. {
  1504. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1505. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1506. __aurng(__urng);
  1507. while (__f != __t)
  1508. *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda();
  1509. }
  1510. template<typename _RealType, typename _CharT, typename _Traits>
  1511. std::basic_ostream<_CharT, _Traits>&
  1512. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1513. const exponential_distribution<_RealType>& __x)
  1514. {
  1515. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1516. const typename __ios_base::fmtflags __flags = __os.flags();
  1517. const _CharT __fill = __os.fill();
  1518. const std::streamsize __precision = __os.precision();
  1519. __os.flags(__ios_base::scientific | __ios_base::left);
  1520. __os.fill(__os.widen(' '));
  1521. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1522. __os << __x.lambda();
  1523. __os.flags(__flags);
  1524. __os.fill(__fill);
  1525. __os.precision(__precision);
  1526. return __os;
  1527. }
  1528. template<typename _RealType, typename _CharT, typename _Traits>
  1529. std::basic_istream<_CharT, _Traits>&
  1530. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1531. exponential_distribution<_RealType>& __x)
  1532. {
  1533. using param_type
  1534. = typename exponential_distribution<_RealType>::param_type;
  1535. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1536. const typename __ios_base::fmtflags __flags = __is.flags();
  1537. __is.flags(__ios_base::dec | __ios_base::skipws);
  1538. _RealType __lambda;
  1539. if (__is >> __lambda)
  1540. __x.param(param_type(__lambda));
  1541. __is.flags(__flags);
  1542. return __is;
  1543. }
  1544. /**
  1545. * Polar method due to Marsaglia.
  1546. *
  1547. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1548. * New York, 1986, Ch. V, Sect. 4.4.
  1549. */
  1550. template<typename _RealType>
  1551. template<typename _UniformRandomNumberGenerator>
  1552. typename normal_distribution<_RealType>::result_type
  1553. normal_distribution<_RealType>::
  1554. operator()(_UniformRandomNumberGenerator& __urng,
  1555. const param_type& __param)
  1556. {
  1557. result_type __ret;
  1558. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1559. __aurng(__urng);
  1560. if (_M_saved_available)
  1561. {
  1562. _M_saved_available = false;
  1563. __ret = _M_saved;
  1564. }
  1565. else
  1566. {
  1567. result_type __x, __y, __r2;
  1568. do
  1569. {
  1570. __x = result_type(2.0) * __aurng() - 1.0;
  1571. __y = result_type(2.0) * __aurng() - 1.0;
  1572. __r2 = __x * __x + __y * __y;
  1573. }
  1574. while (__r2 > 1.0 || __r2 == 0.0);
  1575. const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
  1576. _M_saved = __x * __mult;
  1577. _M_saved_available = true;
  1578. __ret = __y * __mult;
  1579. }
  1580. __ret = __ret * __param.stddev() + __param.mean();
  1581. return __ret;
  1582. }
  1583. template<typename _RealType>
  1584. template<typename _ForwardIterator,
  1585. typename _UniformRandomNumberGenerator>
  1586. void
  1587. normal_distribution<_RealType>::
  1588. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1589. _UniformRandomNumberGenerator& __urng,
  1590. const param_type& __param)
  1591. {
  1592. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1593. if (__f == __t)
  1594. return;
  1595. if (_M_saved_available)
  1596. {
  1597. _M_saved_available = false;
  1598. *__f++ = _M_saved * __param.stddev() + __param.mean();
  1599. if (__f == __t)
  1600. return;
  1601. }
  1602. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1603. __aurng(__urng);
  1604. while (__f + 1 < __t)
  1605. {
  1606. result_type __x, __y, __r2;
  1607. do
  1608. {
  1609. __x = result_type(2.0) * __aurng() - 1.0;
  1610. __y = result_type(2.0) * __aurng() - 1.0;
  1611. __r2 = __x * __x + __y * __y;
  1612. }
  1613. while (__r2 > 1.0 || __r2 == 0.0);
  1614. const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
  1615. *__f++ = __y * __mult * __param.stddev() + __param.mean();
  1616. *__f++ = __x * __mult * __param.stddev() + __param.mean();
  1617. }
  1618. if (__f != __t)
  1619. {
  1620. result_type __x, __y, __r2;
  1621. do
  1622. {
  1623. __x = result_type(2.0) * __aurng() - 1.0;
  1624. __y = result_type(2.0) * __aurng() - 1.0;
  1625. __r2 = __x * __x + __y * __y;
  1626. }
  1627. while (__r2 > 1.0 || __r2 == 0.0);
  1628. const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
  1629. _M_saved = __x * __mult;
  1630. _M_saved_available = true;
  1631. *__f = __y * __mult * __param.stddev() + __param.mean();
  1632. }
  1633. }
  1634. template<typename _RealType>
  1635. bool
  1636. operator==(const std::normal_distribution<_RealType>& __d1,
  1637. const std::normal_distribution<_RealType>& __d2)
  1638. {
  1639. if (__d1._M_param == __d2._M_param
  1640. && __d1._M_saved_available == __d2._M_saved_available)
  1641. {
  1642. if (__d1._M_saved_available
  1643. && __d1._M_saved == __d2._M_saved)
  1644. return true;
  1645. else if(!__d1._M_saved_available)
  1646. return true;
  1647. else
  1648. return false;
  1649. }
  1650. else
  1651. return false;
  1652. }
  1653. template<typename _RealType, typename _CharT, typename _Traits>
  1654. std::basic_ostream<_CharT, _Traits>&
  1655. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1656. const normal_distribution<_RealType>& __x)
  1657. {
  1658. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1659. const typename __ios_base::fmtflags __flags = __os.flags();
  1660. const _CharT __fill = __os.fill();
  1661. const std::streamsize __precision = __os.precision();
  1662. const _CharT __space = __os.widen(' ');
  1663. __os.flags(__ios_base::scientific | __ios_base::left);
  1664. __os.fill(__space);
  1665. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1666. __os << __x.mean() << __space << __x.stddev()
  1667. << __space << __x._M_saved_available;
  1668. if (__x._M_saved_available)
  1669. __os << __space << __x._M_saved;
  1670. __os.flags(__flags);
  1671. __os.fill(__fill);
  1672. __os.precision(__precision);
  1673. return __os;
  1674. }
  1675. template<typename _RealType, typename _CharT, typename _Traits>
  1676. std::basic_istream<_CharT, _Traits>&
  1677. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1678. normal_distribution<_RealType>& __x)
  1679. {
  1680. using param_type = typename normal_distribution<_RealType>::param_type;
  1681. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1682. const typename __ios_base::fmtflags __flags = __is.flags();
  1683. __is.flags(__ios_base::dec | __ios_base::skipws);
  1684. double __mean, __stddev;
  1685. bool __saved_avail;
  1686. if (__is >> __mean >> __stddev >> __saved_avail)
  1687. {
  1688. if (__saved_avail && (__is >> __x._M_saved))
  1689. {
  1690. __x._M_saved_available = __saved_avail;
  1691. __x.param(param_type(__mean, __stddev));
  1692. }
  1693. }
  1694. __is.flags(__flags);
  1695. return __is;
  1696. }
  1697. template<typename _RealType>
  1698. template<typename _ForwardIterator,
  1699. typename _UniformRandomNumberGenerator>
  1700. void
  1701. lognormal_distribution<_RealType>::
  1702. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1703. _UniformRandomNumberGenerator& __urng,
  1704. const param_type& __p)
  1705. {
  1706. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1707. while (__f != __t)
  1708. *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
  1709. }
  1710. template<typename _RealType, typename _CharT, typename _Traits>
  1711. std::basic_ostream<_CharT, _Traits>&
  1712. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1713. const lognormal_distribution<_RealType>& __x)
  1714. {
  1715. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1716. const typename __ios_base::fmtflags __flags = __os.flags();
  1717. const _CharT __fill = __os.fill();
  1718. const std::streamsize __precision = __os.precision();
  1719. const _CharT __space = __os.widen(' ');
  1720. __os.flags(__ios_base::scientific | __ios_base::left);
  1721. __os.fill(__space);
  1722. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1723. __os << __x.m() << __space << __x.s()
  1724. << __space << __x._M_nd;
  1725. __os.flags(__flags);
  1726. __os.fill(__fill);
  1727. __os.precision(__precision);
  1728. return __os;
  1729. }
  1730. template<typename _RealType, typename _CharT, typename _Traits>
  1731. std::basic_istream<_CharT, _Traits>&
  1732. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1733. lognormal_distribution<_RealType>& __x)
  1734. {
  1735. using param_type
  1736. = typename lognormal_distribution<_RealType>::param_type;
  1737. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1738. const typename __ios_base::fmtflags __flags = __is.flags();
  1739. __is.flags(__ios_base::dec | __ios_base::skipws);
  1740. _RealType __m, __s;
  1741. if (__is >> __m >> __s >> __x._M_nd)
  1742. __x.param(param_type(__m, __s));
  1743. __is.flags(__flags);
  1744. return __is;
  1745. }
  1746. template<typename _RealType>
  1747. template<typename _ForwardIterator,
  1748. typename _UniformRandomNumberGenerator>
  1749. void
  1750. std::chi_squared_distribution<_RealType>::
  1751. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1752. _UniformRandomNumberGenerator& __urng)
  1753. {
  1754. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1755. while (__f != __t)
  1756. *__f++ = 2 * _M_gd(__urng);
  1757. }
  1758. template<typename _RealType>
  1759. template<typename _ForwardIterator,
  1760. typename _UniformRandomNumberGenerator>
  1761. void
  1762. std::chi_squared_distribution<_RealType>::
  1763. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1764. _UniformRandomNumberGenerator& __urng,
  1765. const typename
  1766. std::gamma_distribution<result_type>::param_type& __p)
  1767. {
  1768. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1769. while (__f != __t)
  1770. *__f++ = 2 * _M_gd(__urng, __p);
  1771. }
  1772. template<typename _RealType, typename _CharT, typename _Traits>
  1773. std::basic_ostream<_CharT, _Traits>&
  1774. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1775. const chi_squared_distribution<_RealType>& __x)
  1776. {
  1777. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1778. const typename __ios_base::fmtflags __flags = __os.flags();
  1779. const _CharT __fill = __os.fill();
  1780. const std::streamsize __precision = __os.precision();
  1781. const _CharT __space = __os.widen(' ');
  1782. __os.flags(__ios_base::scientific | __ios_base::left);
  1783. __os.fill(__space);
  1784. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1785. __os << __x.n() << __space << __x._M_gd;
  1786. __os.flags(__flags);
  1787. __os.fill(__fill);
  1788. __os.precision(__precision);
  1789. return __os;
  1790. }
  1791. template<typename _RealType, typename _CharT, typename _Traits>
  1792. std::basic_istream<_CharT, _Traits>&
  1793. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1794. chi_squared_distribution<_RealType>& __x)
  1795. {
  1796. using param_type
  1797. = typename chi_squared_distribution<_RealType>::param_type;
  1798. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1799. const typename __ios_base::fmtflags __flags = __is.flags();
  1800. __is.flags(__ios_base::dec | __ios_base::skipws);
  1801. _RealType __n;
  1802. if (__is >> __n >> __x._M_gd)
  1803. __x.param(param_type(__n));
  1804. __is.flags(__flags);
  1805. return __is;
  1806. }
  1807. template<typename _RealType>
  1808. template<typename _UniformRandomNumberGenerator>
  1809. typename cauchy_distribution<_RealType>::result_type
  1810. cauchy_distribution<_RealType>::
  1811. operator()(_UniformRandomNumberGenerator& __urng,
  1812. const param_type& __p)
  1813. {
  1814. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1815. __aurng(__urng);
  1816. _RealType __u;
  1817. do
  1818. __u = __aurng();
  1819. while (__u == 0.5);
  1820. const _RealType __pi = 3.1415926535897932384626433832795029L;
  1821. return __p.a() + __p.b() * std::tan(__pi * __u);
  1822. }
  1823. template<typename _RealType>
  1824. template<typename _ForwardIterator,
  1825. typename _UniformRandomNumberGenerator>
  1826. void
  1827. cauchy_distribution<_RealType>::
  1828. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1829. _UniformRandomNumberGenerator& __urng,
  1830. const param_type& __p)
  1831. {
  1832. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1833. const _RealType __pi = 3.1415926535897932384626433832795029L;
  1834. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1835. __aurng(__urng);
  1836. while (__f != __t)
  1837. {
  1838. _RealType __u;
  1839. do
  1840. __u = __aurng();
  1841. while (__u == 0.5);
  1842. *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
  1843. }
  1844. }
  1845. template<typename _RealType, typename _CharT, typename _Traits>
  1846. std::basic_ostream<_CharT, _Traits>&
  1847. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1848. const cauchy_distribution<_RealType>& __x)
  1849. {
  1850. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1851. const typename __ios_base::fmtflags __flags = __os.flags();
  1852. const _CharT __fill = __os.fill();
  1853. const std::streamsize __precision = __os.precision();
  1854. const _CharT __space = __os.widen(' ');
  1855. __os.flags(__ios_base::scientific | __ios_base::left);
  1856. __os.fill(__space);
  1857. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1858. __os << __x.a() << __space << __x.b();
  1859. __os.flags(__flags);
  1860. __os.fill(__fill);
  1861. __os.precision(__precision);
  1862. return __os;
  1863. }
  1864. template<typename _RealType, typename _CharT, typename _Traits>
  1865. std::basic_istream<_CharT, _Traits>&
  1866. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1867. cauchy_distribution<_RealType>& __x)
  1868. {
  1869. using param_type = typename cauchy_distribution<_RealType>::param_type;
  1870. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1871. const typename __ios_base::fmtflags __flags = __is.flags();
  1872. __is.flags(__ios_base::dec | __ios_base::skipws);
  1873. _RealType __a, __b;
  1874. if (__is >> __a >> __b)
  1875. __x.param(param_type(__a, __b));
  1876. __is.flags(__flags);
  1877. return __is;
  1878. }
  1879. template<typename _RealType>
  1880. template<typename _ForwardIterator,
  1881. typename _UniformRandomNumberGenerator>
  1882. void
  1883. std::fisher_f_distribution<_RealType>::
  1884. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1885. _UniformRandomNumberGenerator& __urng)
  1886. {
  1887. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1888. while (__f != __t)
  1889. *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
  1890. }
  1891. template<typename _RealType>
  1892. template<typename _ForwardIterator,
  1893. typename _UniformRandomNumberGenerator>
  1894. void
  1895. std::fisher_f_distribution<_RealType>::
  1896. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1897. _UniformRandomNumberGenerator& __urng,
  1898. const param_type& __p)
  1899. {
  1900. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1901. typedef typename std::gamma_distribution<result_type>::param_type
  1902. param_type;
  1903. param_type __p1(__p.m() / 2);
  1904. param_type __p2(__p.n() / 2);
  1905. while (__f != __t)
  1906. *__f++ = ((_M_gd_x(__urng, __p1) * n())
  1907. / (_M_gd_y(__urng, __p2) * m()));
  1908. }
  1909. template<typename _RealType, typename _CharT, typename _Traits>
  1910. std::basic_ostream<_CharT, _Traits>&
  1911. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1912. const fisher_f_distribution<_RealType>& __x)
  1913. {
  1914. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1915. const typename __ios_base::fmtflags __flags = __os.flags();
  1916. const _CharT __fill = __os.fill();
  1917. const std::streamsize __precision = __os.precision();
  1918. const _CharT __space = __os.widen(' ');
  1919. __os.flags(__ios_base::scientific | __ios_base::left);
  1920. __os.fill(__space);
  1921. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1922. __os << __x.m() << __space << __x.n()
  1923. << __space << __x._M_gd_x << __space << __x._M_gd_y;
  1924. __os.flags(__flags);
  1925. __os.fill(__fill);
  1926. __os.precision(__precision);
  1927. return __os;
  1928. }
  1929. template<typename _RealType, typename _CharT, typename _Traits>
  1930. std::basic_istream<_CharT, _Traits>&
  1931. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1932. fisher_f_distribution<_RealType>& __x)
  1933. {
  1934. using param_type
  1935. = typename fisher_f_distribution<_RealType>::param_type;
  1936. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1937. const typename __ios_base::fmtflags __flags = __is.flags();
  1938. __is.flags(__ios_base::dec | __ios_base::skipws);
  1939. _RealType __m, __n;
  1940. if (__is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y)
  1941. __x.param(param_type(__m, __n));
  1942. __is.flags(__flags);
  1943. return __is;
  1944. }
  1945. template<typename _RealType>
  1946. template<typename _ForwardIterator,
  1947. typename _UniformRandomNumberGenerator>
  1948. void
  1949. std::student_t_distribution<_RealType>::
  1950. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1951. _UniformRandomNumberGenerator& __urng)
  1952. {
  1953. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1954. while (__f != __t)
  1955. *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
  1956. }
  1957. template<typename _RealType>
  1958. template<typename _ForwardIterator,
  1959. typename _UniformRandomNumberGenerator>
  1960. void
  1961. std::student_t_distribution<_RealType>::
  1962. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1963. _UniformRandomNumberGenerator& __urng,
  1964. const param_type& __p)
  1965. {
  1966. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1967. typename std::gamma_distribution<result_type>::param_type
  1968. __p2(__p.n() / 2, 2);
  1969. while (__f != __t)
  1970. *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
  1971. }
  1972. template<typename _RealType, typename _CharT, typename _Traits>
  1973. std::basic_ostream<_CharT, _Traits>&
  1974. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1975. const student_t_distribution<_RealType>& __x)
  1976. {
  1977. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  1978. const typename __ios_base::fmtflags __flags = __os.flags();
  1979. const _CharT __fill = __os.fill();
  1980. const std::streamsize __precision = __os.precision();
  1981. const _CharT __space = __os.widen(' ');
  1982. __os.flags(__ios_base::scientific | __ios_base::left);
  1983. __os.fill(__space);
  1984. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1985. __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
  1986. __os.flags(__flags);
  1987. __os.fill(__fill);
  1988. __os.precision(__precision);
  1989. return __os;
  1990. }
  1991. template<typename _RealType, typename _CharT, typename _Traits>
  1992. std::basic_istream<_CharT, _Traits>&
  1993. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1994. student_t_distribution<_RealType>& __x)
  1995. {
  1996. using param_type
  1997. = typename student_t_distribution<_RealType>::param_type;
  1998. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  1999. const typename __ios_base::fmtflags __flags = __is.flags();
  2000. __is.flags(__ios_base::dec | __ios_base::skipws);
  2001. _RealType __n;
  2002. if (__is >> __n >> __x._M_nd >> __x._M_gd)
  2003. __x.param(param_type(__n));
  2004. __is.flags(__flags);
  2005. return __is;
  2006. }
  2007. template<typename _RealType>
  2008. void
  2009. gamma_distribution<_RealType>::param_type::
  2010. _M_initialize()
  2011. {
  2012. _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
  2013. const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
  2014. _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
  2015. }
  2016. /**
  2017. * Marsaglia, G. and Tsang, W. W.
  2018. * "A Simple Method for Generating Gamma Variables"
  2019. * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
  2020. */
  2021. template<typename _RealType>
  2022. template<typename _UniformRandomNumberGenerator>
  2023. typename gamma_distribution<_RealType>::result_type
  2024. gamma_distribution<_RealType>::
  2025. operator()(_UniformRandomNumberGenerator& __urng,
  2026. const param_type& __param)
  2027. {
  2028. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2029. __aurng(__urng);
  2030. result_type __u, __v, __n;
  2031. const result_type __a1 = (__param._M_malpha
  2032. - _RealType(1.0) / _RealType(3.0));
  2033. do
  2034. {
  2035. do
  2036. {
  2037. __n = _M_nd(__urng);
  2038. __v = result_type(1.0) + __param._M_a2 * __n;
  2039. }
  2040. while (__v <= 0.0);
  2041. __v = __v * __v * __v;
  2042. __u = __aurng();
  2043. }
  2044. while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
  2045. && (std::log(__u) > (0.5 * __n * __n + __a1
  2046. * (1.0 - __v + std::log(__v)))));
  2047. if (__param.alpha() == __param._M_malpha)
  2048. return __a1 * __v * __param.beta();
  2049. else
  2050. {
  2051. do
  2052. __u = __aurng();
  2053. while (__u == 0.0);
  2054. return (std::pow(__u, result_type(1.0) / __param.alpha())
  2055. * __a1 * __v * __param.beta());
  2056. }
  2057. }
  2058. template<typename _RealType>
  2059. template<typename _ForwardIterator,
  2060. typename _UniformRandomNumberGenerator>
  2061. void
  2062. gamma_distribution<_RealType>::
  2063. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2064. _UniformRandomNumberGenerator& __urng,
  2065. const param_type& __param)
  2066. {
  2067. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2068. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2069. __aurng(__urng);
  2070. result_type __u, __v, __n;
  2071. const result_type __a1 = (__param._M_malpha
  2072. - _RealType(1.0) / _RealType(3.0));
  2073. if (__param.alpha() == __param._M_malpha)
  2074. while (__f != __t)
  2075. {
  2076. do
  2077. {
  2078. do
  2079. {
  2080. __n = _M_nd(__urng);
  2081. __v = result_type(1.0) + __param._M_a2 * __n;
  2082. }
  2083. while (__v <= 0.0);
  2084. __v = __v * __v * __v;
  2085. __u = __aurng();
  2086. }
  2087. while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
  2088. && (std::log(__u) > (0.5 * __n * __n + __a1
  2089. * (1.0 - __v + std::log(__v)))));
  2090. *__f++ = __a1 * __v * __param.beta();
  2091. }
  2092. else
  2093. while (__f != __t)
  2094. {
  2095. do
  2096. {
  2097. do
  2098. {
  2099. __n = _M_nd(__urng);
  2100. __v = result_type(1.0) + __param._M_a2 * __n;
  2101. }
  2102. while (__v <= 0.0);
  2103. __v = __v * __v * __v;
  2104. __u = __aurng();
  2105. }
  2106. while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
  2107. && (std::log(__u) > (0.5 * __n * __n + __a1
  2108. * (1.0 - __v + std::log(__v)))));
  2109. do
  2110. __u = __aurng();
  2111. while (__u == 0.0);
  2112. *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
  2113. * __a1 * __v * __param.beta());
  2114. }
  2115. }
  2116. template<typename _RealType, typename _CharT, typename _Traits>
  2117. std::basic_ostream<_CharT, _Traits>&
  2118. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2119. const gamma_distribution<_RealType>& __x)
  2120. {
  2121. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2122. const typename __ios_base::fmtflags __flags = __os.flags();
  2123. const _CharT __fill = __os.fill();
  2124. const std::streamsize __precision = __os.precision();
  2125. const _CharT __space = __os.widen(' ');
  2126. __os.flags(__ios_base::scientific | __ios_base::left);
  2127. __os.fill(__space);
  2128. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  2129. __os << __x.alpha() << __space << __x.beta()
  2130. << __space << __x._M_nd;
  2131. __os.flags(__flags);
  2132. __os.fill(__fill);
  2133. __os.precision(__precision);
  2134. return __os;
  2135. }
  2136. template<typename _RealType, typename _CharT, typename _Traits>
  2137. std::basic_istream<_CharT, _Traits>&
  2138. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2139. gamma_distribution<_RealType>& __x)
  2140. {
  2141. using param_type = typename gamma_distribution<_RealType>::param_type;
  2142. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2143. const typename __ios_base::fmtflags __flags = __is.flags();
  2144. __is.flags(__ios_base::dec | __ios_base::skipws);
  2145. _RealType __alpha_val, __beta_val;
  2146. if (__is >> __alpha_val >> __beta_val >> __x._M_nd)
  2147. __x.param(param_type(__alpha_val, __beta_val));
  2148. __is.flags(__flags);
  2149. return __is;
  2150. }
  2151. template<typename _RealType>
  2152. template<typename _UniformRandomNumberGenerator>
  2153. typename weibull_distribution<_RealType>::result_type
  2154. weibull_distribution<_RealType>::
  2155. operator()(_UniformRandomNumberGenerator& __urng,
  2156. const param_type& __p)
  2157. {
  2158. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2159. __aurng(__urng);
  2160. return __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
  2161. result_type(1) / __p.a());
  2162. }
  2163. template<typename _RealType>
  2164. template<typename _ForwardIterator,
  2165. typename _UniformRandomNumberGenerator>
  2166. void
  2167. weibull_distribution<_RealType>::
  2168. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2169. _UniformRandomNumberGenerator& __urng,
  2170. const param_type& __p)
  2171. {
  2172. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2173. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2174. __aurng(__urng);
  2175. auto __inv_a = result_type(1) / __p.a();
  2176. while (__f != __t)
  2177. *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
  2178. __inv_a);
  2179. }
  2180. template<typename _RealType, typename _CharT, typename _Traits>
  2181. std::basic_ostream<_CharT, _Traits>&
  2182. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2183. const weibull_distribution<_RealType>& __x)
  2184. {
  2185. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2186. const typename __ios_base::fmtflags __flags = __os.flags();
  2187. const _CharT __fill = __os.fill();
  2188. const std::streamsize __precision = __os.precision();
  2189. const _CharT __space = __os.widen(' ');
  2190. __os.flags(__ios_base::scientific | __ios_base::left);
  2191. __os.fill(__space);
  2192. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  2193. __os << __x.a() << __space << __x.b();
  2194. __os.flags(__flags);
  2195. __os.fill(__fill);
  2196. __os.precision(__precision);
  2197. return __os;
  2198. }
  2199. template<typename _RealType, typename _CharT, typename _Traits>
  2200. std::basic_istream<_CharT, _Traits>&
  2201. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2202. weibull_distribution<_RealType>& __x)
  2203. {
  2204. using param_type = typename weibull_distribution<_RealType>::param_type;
  2205. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2206. const typename __ios_base::fmtflags __flags = __is.flags();
  2207. __is.flags(__ios_base::dec | __ios_base::skipws);
  2208. _RealType __a, __b;
  2209. if (__is >> __a >> __b)
  2210. __x.param(param_type(__a, __b));
  2211. __is.flags(__flags);
  2212. return __is;
  2213. }
  2214. template<typename _RealType>
  2215. template<typename _UniformRandomNumberGenerator>
  2216. typename extreme_value_distribution<_RealType>::result_type
  2217. extreme_value_distribution<_RealType>::
  2218. operator()(_UniformRandomNumberGenerator& __urng,
  2219. const param_type& __p)
  2220. {
  2221. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2222. __aurng(__urng);
  2223. return __p.a() - __p.b() * std::log(-std::log(result_type(1)
  2224. - __aurng()));
  2225. }
  2226. template<typename _RealType>
  2227. template<typename _ForwardIterator,
  2228. typename _UniformRandomNumberGenerator>
  2229. void
  2230. extreme_value_distribution<_RealType>::
  2231. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2232. _UniformRandomNumberGenerator& __urng,
  2233. const param_type& __p)
  2234. {
  2235. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2236. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2237. __aurng(__urng);
  2238. while (__f != __t)
  2239. *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1)
  2240. - __aurng()));
  2241. }
  2242. template<typename _RealType, typename _CharT, typename _Traits>
  2243. std::basic_ostream<_CharT, _Traits>&
  2244. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2245. const extreme_value_distribution<_RealType>& __x)
  2246. {
  2247. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2248. const typename __ios_base::fmtflags __flags = __os.flags();
  2249. const _CharT __fill = __os.fill();
  2250. const std::streamsize __precision = __os.precision();
  2251. const _CharT __space = __os.widen(' ');
  2252. __os.flags(__ios_base::scientific | __ios_base::left);
  2253. __os.fill(__space);
  2254. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  2255. __os << __x.a() << __space << __x.b();
  2256. __os.flags(__flags);
  2257. __os.fill(__fill);
  2258. __os.precision(__precision);
  2259. return __os;
  2260. }
  2261. template<typename _RealType, typename _CharT, typename _Traits>
  2262. std::basic_istream<_CharT, _Traits>&
  2263. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2264. extreme_value_distribution<_RealType>& __x)
  2265. {
  2266. using param_type
  2267. = typename extreme_value_distribution<_RealType>::param_type;
  2268. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2269. const typename __ios_base::fmtflags __flags = __is.flags();
  2270. __is.flags(__ios_base::dec | __ios_base::skipws);
  2271. _RealType __a, __b;
  2272. if (__is >> __a >> __b)
  2273. __x.param(param_type(__a, __b));
  2274. __is.flags(__flags);
  2275. return __is;
  2276. }
  2277. template<typename _IntType>
  2278. void
  2279. discrete_distribution<_IntType>::param_type::
  2280. _M_initialize()
  2281. {
  2282. if (_M_prob.size() < 2)
  2283. {
  2284. _M_prob.clear();
  2285. return;
  2286. }
  2287. const double __sum = std::accumulate(_M_prob.begin(),
  2288. _M_prob.end(), 0.0);
  2289. __glibcxx_assert(__sum > 0);
  2290. // Now normalize the probabilites.
  2291. __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
  2292. __sum);
  2293. // Accumulate partial sums.
  2294. _M_cp.reserve(_M_prob.size());
  2295. std::partial_sum(_M_prob.begin(), _M_prob.end(),
  2296. std::back_inserter(_M_cp));
  2297. // Make sure the last cumulative probability is one.
  2298. _M_cp[_M_cp.size() - 1] = 1.0;
  2299. }
  2300. template<typename _IntType>
  2301. template<typename _Func>
  2302. discrete_distribution<_IntType>::param_type::
  2303. param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
  2304. : _M_prob(), _M_cp()
  2305. {
  2306. const size_t __n = __nw == 0 ? 1 : __nw;
  2307. const double __delta = (__xmax - __xmin) / __n;
  2308. _M_prob.reserve(__n);
  2309. for (size_t __k = 0; __k < __nw; ++__k)
  2310. _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
  2311. _M_initialize();
  2312. }
  2313. template<typename _IntType>
  2314. template<typename _UniformRandomNumberGenerator>
  2315. typename discrete_distribution<_IntType>::result_type
  2316. discrete_distribution<_IntType>::
  2317. operator()(_UniformRandomNumberGenerator& __urng,
  2318. const param_type& __param)
  2319. {
  2320. if (__param._M_cp.empty())
  2321. return result_type(0);
  2322. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  2323. __aurng(__urng);
  2324. const double __p = __aurng();
  2325. auto __pos = std::lower_bound(__param._M_cp.begin(),
  2326. __param._M_cp.end(), __p);
  2327. return __pos - __param._M_cp.begin();
  2328. }
  2329. template<typename _IntType>
  2330. template<typename _ForwardIterator,
  2331. typename _UniformRandomNumberGenerator>
  2332. void
  2333. discrete_distribution<_IntType>::
  2334. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2335. _UniformRandomNumberGenerator& __urng,
  2336. const param_type& __param)
  2337. {
  2338. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2339. if (__param._M_cp.empty())
  2340. {
  2341. while (__f != __t)
  2342. *__f++ = result_type(0);
  2343. return;
  2344. }
  2345. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  2346. __aurng(__urng);
  2347. while (__f != __t)
  2348. {
  2349. const double __p = __aurng();
  2350. auto __pos = std::lower_bound(__param._M_cp.begin(),
  2351. __param._M_cp.end(), __p);
  2352. *__f++ = __pos - __param._M_cp.begin();
  2353. }
  2354. }
  2355. template<typename _IntType, typename _CharT, typename _Traits>
  2356. std::basic_ostream<_CharT, _Traits>&
  2357. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2358. const discrete_distribution<_IntType>& __x)
  2359. {
  2360. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2361. const typename __ios_base::fmtflags __flags = __os.flags();
  2362. const _CharT __fill = __os.fill();
  2363. const std::streamsize __precision = __os.precision();
  2364. const _CharT __space = __os.widen(' ');
  2365. __os.flags(__ios_base::scientific | __ios_base::left);
  2366. __os.fill(__space);
  2367. __os.precision(std::numeric_limits<double>::max_digits10);
  2368. std::vector<double> __prob = __x.probabilities();
  2369. __os << __prob.size();
  2370. for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
  2371. __os << __space << *__dit;
  2372. __os.flags(__flags);
  2373. __os.fill(__fill);
  2374. __os.precision(__precision);
  2375. return __os;
  2376. }
  2377. namespace __detail
  2378. {
  2379. template<typename _ValT, typename _CharT, typename _Traits>
  2380. basic_istream<_CharT, _Traits>&
  2381. __extract_params(basic_istream<_CharT, _Traits>& __is,
  2382. vector<_ValT>& __vals, size_t __n)
  2383. {
  2384. __vals.reserve(__n);
  2385. while (__n--)
  2386. {
  2387. _ValT __val;
  2388. if (__is >> __val)
  2389. __vals.push_back(__val);
  2390. else
  2391. break;
  2392. }
  2393. return __is;
  2394. }
  2395. } // namespace __detail
  2396. template<typename _IntType, typename _CharT, typename _Traits>
  2397. std::basic_istream<_CharT, _Traits>&
  2398. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2399. discrete_distribution<_IntType>& __x)
  2400. {
  2401. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2402. const typename __ios_base::fmtflags __flags = __is.flags();
  2403. __is.flags(__ios_base::dec | __ios_base::skipws);
  2404. size_t __n;
  2405. if (__is >> __n)
  2406. {
  2407. std::vector<double> __prob_vec;
  2408. if (__detail::__extract_params(__is, __prob_vec, __n))
  2409. __x.param({__prob_vec.begin(), __prob_vec.end()});
  2410. }
  2411. __is.flags(__flags);
  2412. return __is;
  2413. }
  2414. template<typename _RealType>
  2415. void
  2416. piecewise_constant_distribution<_RealType>::param_type::
  2417. _M_initialize()
  2418. {
  2419. if (_M_int.size() < 2
  2420. || (_M_int.size() == 2
  2421. && _M_int[0] == _RealType(0)
  2422. && _M_int[1] == _RealType(1)))
  2423. {
  2424. _M_int.clear();
  2425. _M_den.clear();
  2426. return;
  2427. }
  2428. const double __sum = std::accumulate(_M_den.begin(),
  2429. _M_den.end(), 0.0);
  2430. __glibcxx_assert(__sum > 0);
  2431. __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
  2432. __sum);
  2433. _M_cp.reserve(_M_den.size());
  2434. std::partial_sum(_M_den.begin(), _M_den.end(),
  2435. std::back_inserter(_M_cp));
  2436. // Make sure the last cumulative probability is one.
  2437. _M_cp[_M_cp.size() - 1] = 1.0;
  2438. for (size_t __k = 0; __k < _M_den.size(); ++__k)
  2439. _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
  2440. }
  2441. template<typename _RealType>
  2442. template<typename _InputIteratorB, typename _InputIteratorW>
  2443. piecewise_constant_distribution<_RealType>::param_type::
  2444. param_type(_InputIteratorB __bbegin,
  2445. _InputIteratorB __bend,
  2446. _InputIteratorW __wbegin)
  2447. : _M_int(), _M_den(), _M_cp()
  2448. {
  2449. if (__bbegin != __bend)
  2450. {
  2451. for (;;)
  2452. {
  2453. _M_int.push_back(*__bbegin);
  2454. ++__bbegin;
  2455. if (__bbegin == __bend)
  2456. break;
  2457. _M_den.push_back(*__wbegin);
  2458. ++__wbegin;
  2459. }
  2460. }
  2461. _M_initialize();
  2462. }
  2463. template<typename _RealType>
  2464. template<typename _Func>
  2465. piecewise_constant_distribution<_RealType>::param_type::
  2466. param_type(initializer_list<_RealType> __bl, _Func __fw)
  2467. : _M_int(), _M_den(), _M_cp()
  2468. {
  2469. _M_int.reserve(__bl.size());
  2470. for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
  2471. _M_int.push_back(*__biter);
  2472. _M_den.reserve(_M_int.size() - 1);
  2473. for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
  2474. _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
  2475. _M_initialize();
  2476. }
  2477. template<typename _RealType>
  2478. template<typename _Func>
  2479. piecewise_constant_distribution<_RealType>::param_type::
  2480. param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
  2481. : _M_int(), _M_den(), _M_cp()
  2482. {
  2483. const size_t __n = __nw == 0 ? 1 : __nw;
  2484. const _RealType __delta = (__xmax - __xmin) / __n;
  2485. _M_int.reserve(__n + 1);
  2486. for (size_t __k = 0; __k <= __nw; ++__k)
  2487. _M_int.push_back(__xmin + __k * __delta);
  2488. _M_den.reserve(__n);
  2489. for (size_t __k = 0; __k < __nw; ++__k)
  2490. _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
  2491. _M_initialize();
  2492. }
  2493. template<typename _RealType>
  2494. template<typename _UniformRandomNumberGenerator>
  2495. typename piecewise_constant_distribution<_RealType>::result_type
  2496. piecewise_constant_distribution<_RealType>::
  2497. operator()(_UniformRandomNumberGenerator& __urng,
  2498. const param_type& __param)
  2499. {
  2500. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  2501. __aurng(__urng);
  2502. const double __p = __aurng();
  2503. if (__param._M_cp.empty())
  2504. return __p;
  2505. auto __pos = std::lower_bound(__param._M_cp.begin(),
  2506. __param._M_cp.end(), __p);
  2507. const size_t __i = __pos - __param._M_cp.begin();
  2508. const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
  2509. return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
  2510. }
  2511. template<typename _RealType>
  2512. template<typename _ForwardIterator,
  2513. typename _UniformRandomNumberGenerator>
  2514. void
  2515. piecewise_constant_distribution<_RealType>::
  2516. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2517. _UniformRandomNumberGenerator& __urng,
  2518. const param_type& __param)
  2519. {
  2520. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2521. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  2522. __aurng(__urng);
  2523. if (__param._M_cp.empty())
  2524. {
  2525. while (__f != __t)
  2526. *__f++ = __aurng();
  2527. return;
  2528. }
  2529. while (__f != __t)
  2530. {
  2531. const double __p = __aurng();
  2532. auto __pos = std::lower_bound(__param._M_cp.begin(),
  2533. __param._M_cp.end(), __p);
  2534. const size_t __i = __pos - __param._M_cp.begin();
  2535. const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
  2536. *__f++ = (__param._M_int[__i]
  2537. + (__p - __pref) / __param._M_den[__i]);
  2538. }
  2539. }
  2540. template<typename _RealType, typename _CharT, typename _Traits>
  2541. std::basic_ostream<_CharT, _Traits>&
  2542. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2543. const piecewise_constant_distribution<_RealType>& __x)
  2544. {
  2545. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2546. const typename __ios_base::fmtflags __flags = __os.flags();
  2547. const _CharT __fill = __os.fill();
  2548. const std::streamsize __precision = __os.precision();
  2549. const _CharT __space = __os.widen(' ');
  2550. __os.flags(__ios_base::scientific | __ios_base::left);
  2551. __os.fill(__space);
  2552. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  2553. std::vector<_RealType> __int = __x.intervals();
  2554. __os << __int.size() - 1;
  2555. for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
  2556. __os << __space << *__xit;
  2557. std::vector<double> __den = __x.densities();
  2558. for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
  2559. __os << __space << *__dit;
  2560. __os.flags(__flags);
  2561. __os.fill(__fill);
  2562. __os.precision(__precision);
  2563. return __os;
  2564. }
  2565. template<typename _RealType, typename _CharT, typename _Traits>
  2566. std::basic_istream<_CharT, _Traits>&
  2567. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2568. piecewise_constant_distribution<_RealType>& __x)
  2569. {
  2570. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2571. const typename __ios_base::fmtflags __flags = __is.flags();
  2572. __is.flags(__ios_base::dec | __ios_base::skipws);
  2573. size_t __n;
  2574. if (__is >> __n)
  2575. {
  2576. std::vector<_RealType> __int_vec;
  2577. if (__detail::__extract_params(__is, __int_vec, __n + 1))
  2578. {
  2579. std::vector<double> __den_vec;
  2580. if (__detail::__extract_params(__is, __den_vec, __n))
  2581. {
  2582. __x.param({ __int_vec.begin(), __int_vec.end(),
  2583. __den_vec.begin() });
  2584. }
  2585. }
  2586. }
  2587. __is.flags(__flags);
  2588. return __is;
  2589. }
  2590. template<typename _RealType>
  2591. void
  2592. piecewise_linear_distribution<_RealType>::param_type::
  2593. _M_initialize()
  2594. {
  2595. if (_M_int.size() < 2
  2596. || (_M_int.size() == 2
  2597. && _M_int[0] == _RealType(0)
  2598. && _M_int[1] == _RealType(1)
  2599. && _M_den[0] == _M_den[1]))
  2600. {
  2601. _M_int.clear();
  2602. _M_den.clear();
  2603. return;
  2604. }
  2605. double __sum = 0.0;
  2606. _M_cp.reserve(_M_int.size() - 1);
  2607. _M_m.reserve(_M_int.size() - 1);
  2608. for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
  2609. {
  2610. const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
  2611. __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
  2612. _M_cp.push_back(__sum);
  2613. _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
  2614. }
  2615. __glibcxx_assert(__sum > 0);
  2616. // Now normalize the densities...
  2617. __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
  2618. __sum);
  2619. // ... and partial sums...
  2620. __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum);
  2621. // ... and slopes.
  2622. __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum);
  2623. // Make sure the last cumulative probablility is one.
  2624. _M_cp[_M_cp.size() - 1] = 1.0;
  2625. }
  2626. template<typename _RealType>
  2627. template<typename _InputIteratorB, typename _InputIteratorW>
  2628. piecewise_linear_distribution<_RealType>::param_type::
  2629. param_type(_InputIteratorB __bbegin,
  2630. _InputIteratorB __bend,
  2631. _InputIteratorW __wbegin)
  2632. : _M_int(), _M_den(), _M_cp(), _M_m()
  2633. {
  2634. for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
  2635. {
  2636. _M_int.push_back(*__bbegin);
  2637. _M_den.push_back(*__wbegin);
  2638. }
  2639. _M_initialize();
  2640. }
  2641. template<typename _RealType>
  2642. template<typename _Func>
  2643. piecewise_linear_distribution<_RealType>::param_type::
  2644. param_type(initializer_list<_RealType> __bl, _Func __fw)
  2645. : _M_int(), _M_den(), _M_cp(), _M_m()
  2646. {
  2647. _M_int.reserve(__bl.size());
  2648. _M_den.reserve(__bl.size());
  2649. for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
  2650. {
  2651. _M_int.push_back(*__biter);
  2652. _M_den.push_back(__fw(*__biter));
  2653. }
  2654. _M_initialize();
  2655. }
  2656. template<typename _RealType>
  2657. template<typename _Func>
  2658. piecewise_linear_distribution<_RealType>::param_type::
  2659. param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
  2660. : _M_int(), _M_den(), _M_cp(), _M_m()
  2661. {
  2662. const size_t __n = __nw == 0 ? 1 : __nw;
  2663. const _RealType __delta = (__xmax - __xmin) / __n;
  2664. _M_int.reserve(__n + 1);
  2665. _M_den.reserve(__n + 1);
  2666. for (size_t __k = 0; __k <= __nw; ++__k)
  2667. {
  2668. _M_int.push_back(__xmin + __k * __delta);
  2669. _M_den.push_back(__fw(_M_int[__k] + __delta));
  2670. }
  2671. _M_initialize();
  2672. }
  2673. template<typename _RealType>
  2674. template<typename _UniformRandomNumberGenerator>
  2675. typename piecewise_linear_distribution<_RealType>::result_type
  2676. piecewise_linear_distribution<_RealType>::
  2677. operator()(_UniformRandomNumberGenerator& __urng,
  2678. const param_type& __param)
  2679. {
  2680. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  2681. __aurng(__urng);
  2682. const double __p = __aurng();
  2683. if (__param._M_cp.empty())
  2684. return __p;
  2685. auto __pos = std::lower_bound(__param._M_cp.begin(),
  2686. __param._M_cp.end(), __p);
  2687. const size_t __i = __pos - __param._M_cp.begin();
  2688. const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
  2689. const double __a = 0.5 * __param._M_m[__i];
  2690. const double __b = __param._M_den[__i];
  2691. const double __cm = __p - __pref;
  2692. _RealType __x = __param._M_int[__i];
  2693. if (__a == 0)
  2694. __x += __cm / __b;
  2695. else
  2696. {
  2697. const double __d = __b * __b + 4.0 * __a * __cm;
  2698. __x += 0.5 * (std::sqrt(__d) - __b) / __a;
  2699. }
  2700. return __x;
  2701. }
  2702. template<typename _RealType>
  2703. template<typename _ForwardIterator,
  2704. typename _UniformRandomNumberGenerator>
  2705. void
  2706. piecewise_linear_distribution<_RealType>::
  2707. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2708. _UniformRandomNumberGenerator& __urng,
  2709. const param_type& __param)
  2710. {
  2711. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  2712. // We could duplicate everything from operator()...
  2713. while (__f != __t)
  2714. *__f++ = this->operator()(__urng, __param);
  2715. }
  2716. template<typename _RealType, typename _CharT, typename _Traits>
  2717. std::basic_ostream<_CharT, _Traits>&
  2718. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2719. const piecewise_linear_distribution<_RealType>& __x)
  2720. {
  2721. using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
  2722. const typename __ios_base::fmtflags __flags = __os.flags();
  2723. const _CharT __fill = __os.fill();
  2724. const std::streamsize __precision = __os.precision();
  2725. const _CharT __space = __os.widen(' ');
  2726. __os.flags(__ios_base::scientific | __ios_base::left);
  2727. __os.fill(__space);
  2728. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  2729. std::vector<_RealType> __int = __x.intervals();
  2730. __os << __int.size() - 1;
  2731. for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
  2732. __os << __space << *__xit;
  2733. std::vector<double> __den = __x.densities();
  2734. for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
  2735. __os << __space << *__dit;
  2736. __os.flags(__flags);
  2737. __os.fill(__fill);
  2738. __os.precision(__precision);
  2739. return __os;
  2740. }
  2741. template<typename _RealType, typename _CharT, typename _Traits>
  2742. std::basic_istream<_CharT, _Traits>&
  2743. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2744. piecewise_linear_distribution<_RealType>& __x)
  2745. {
  2746. using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
  2747. const typename __ios_base::fmtflags __flags = __is.flags();
  2748. __is.flags(__ios_base::dec | __ios_base::skipws);
  2749. size_t __n;
  2750. if (__is >> __n)
  2751. {
  2752. vector<_RealType> __int_vec;
  2753. if (__detail::__extract_params(__is, __int_vec, __n + 1))
  2754. {
  2755. vector<double> __den_vec;
  2756. if (__detail::__extract_params(__is, __den_vec, __n + 1))
  2757. {
  2758. __x.param({ __int_vec.begin(), __int_vec.end(),
  2759. __den_vec.begin() });
  2760. }
  2761. }
  2762. }
  2763. __is.flags(__flags);
  2764. return __is;
  2765. }
  2766. template<typename _IntType>
  2767. seed_seq::seed_seq(std::initializer_list<_IntType> __il)
  2768. {
  2769. for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
  2770. _M_v.push_back(__detail::__mod<result_type,
  2771. __detail::_Shift<result_type, 32>::__value>(*__iter));
  2772. }
  2773. template<typename _InputIterator>
  2774. seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
  2775. {
  2776. for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
  2777. _M_v.push_back(__detail::__mod<result_type,
  2778. __detail::_Shift<result_type, 32>::__value>(*__iter));
  2779. }
  2780. template<typename _RandomAccessIterator>
  2781. void
  2782. seed_seq::generate(_RandomAccessIterator __begin,
  2783. _RandomAccessIterator __end)
  2784. {
  2785. typedef typename iterator_traits<_RandomAccessIterator>::value_type
  2786. _Type;
  2787. if (__begin == __end)
  2788. return;
  2789. std::fill(__begin, __end, _Type(0x8b8b8b8bu));
  2790. const size_t __n = __end - __begin;
  2791. const size_t __s = _M_v.size();
  2792. const size_t __t = (__n >= 623) ? 11
  2793. : (__n >= 68) ? 7
  2794. : (__n >= 39) ? 5
  2795. : (__n >= 7) ? 3
  2796. : (__n - 1) / 2;
  2797. const size_t __p = (__n - __t) / 2;
  2798. const size_t __q = __p + __t;
  2799. const size_t __m = std::max(size_t(__s + 1), __n);
  2800. #ifndef __UINT32_TYPE__
  2801. struct _Up
  2802. {
  2803. _Up(uint_least32_t v) : _M_v(v & 0xffffffffu) { }
  2804. operator uint_least32_t() const { return _M_v; }
  2805. uint_least32_t _M_v;
  2806. };
  2807. using uint32_t = _Up;
  2808. #endif
  2809. // k == 0, every element in [begin,end) equals 0x8b8b8b8bu
  2810. {
  2811. uint32_t __r1 = 1371501266u;
  2812. uint32_t __r2 = __r1 + __s;
  2813. __begin[__p] += __r1;
  2814. __begin[__q] = (uint32_t)__begin[__q] + __r2;
  2815. __begin[0] = __r2;
  2816. }
  2817. for (size_t __k = 1; __k <= __s; ++__k)
  2818. {
  2819. const size_t __kn = __k % __n;
  2820. const size_t __kpn = (__k + __p) % __n;
  2821. const size_t __kqn = (__k + __q) % __n;
  2822. uint32_t __arg = (__begin[__kn]
  2823. ^ __begin[__kpn]
  2824. ^ __begin[(__k - 1) % __n]);
  2825. uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
  2826. uint32_t __r2 = __r1 + (uint32_t)__kn + _M_v[__k - 1];
  2827. __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
  2828. __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
  2829. __begin[__kn] = __r2;
  2830. }
  2831. for (size_t __k = __s + 1; __k < __m; ++__k)
  2832. {
  2833. const size_t __kn = __k % __n;
  2834. const size_t __kpn = (__k + __p) % __n;
  2835. const size_t __kqn = (__k + __q) % __n;
  2836. uint32_t __arg = (__begin[__kn]
  2837. ^ __begin[__kpn]
  2838. ^ __begin[(__k - 1) % __n]);
  2839. uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
  2840. uint32_t __r2 = __r1 + (uint32_t)__kn;
  2841. __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
  2842. __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
  2843. __begin[__kn] = __r2;
  2844. }
  2845. for (size_t __k = __m; __k < __m + __n; ++__k)
  2846. {
  2847. const size_t __kn = __k % __n;
  2848. const size_t __kpn = (__k + __p) % __n;
  2849. const size_t __kqn = (__k + __q) % __n;
  2850. uint32_t __arg = (__begin[__kn]
  2851. + __begin[__kpn]
  2852. + __begin[(__k - 1) % __n]);
  2853. uint32_t __r3 = 1566083941u * (__arg ^ (__arg >> 27));
  2854. uint32_t __r4 = __r3 - __kn;
  2855. __begin[__kpn] ^= __r3;
  2856. __begin[__kqn] ^= __r4;
  2857. __begin[__kn] = __r4;
  2858. }
  2859. }
  2860. template<typename _RealType, size_t __bits,
  2861. typename _UniformRandomNumberGenerator>
  2862. _RealType
  2863. generate_canonical(_UniformRandomNumberGenerator& __urng)
  2864. {
  2865. static_assert(std::is_floating_point<_RealType>::value,
  2866. "template argument must be a floating point type");
  2867. const size_t __b
  2868. = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
  2869. __bits);
  2870. const long double __r = static_cast<long double>(__urng.max())
  2871. - static_cast<long double>(__urng.min()) + 1.0L;
  2872. const size_t __log2r = std::log(__r) / std::log(2.0L);
  2873. const size_t __m = std::max<size_t>(1UL,
  2874. (__b + __log2r - 1UL) / __log2r);
  2875. _RealType __ret;
  2876. _RealType __sum = _RealType(0);
  2877. _RealType __tmp = _RealType(1);
  2878. for (size_t __k = __m; __k != 0; --__k)
  2879. {
  2880. __sum += _RealType(__urng() - __urng.min()) * __tmp;
  2881. __tmp *= __r;
  2882. }
  2883. __ret = __sum / __tmp;
  2884. if (__builtin_expect(__ret >= _RealType(1), 0))
  2885. {
  2886. #if _GLIBCXX_USE_C99_MATH_TR1
  2887. __ret = std::nextafter(_RealType(1), _RealType(0));
  2888. #else
  2889. __ret = _RealType(1)
  2890. - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
  2891. #endif
  2892. }
  2893. return __ret;
  2894. }
  2895. _GLIBCXX_END_NAMESPACE_VERSION
  2896. } // namespace
  2897. #endif