random.tcc 103 KB

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