random.tcc 107 KB

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