random.tcc 103 KB

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