random.tcc 103 KB

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