random.tcc 103 KB

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