random.tcc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. // random number generation (out of line) -*- C++ -*-
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file tr1/random.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{tr1/random}
  23. */
  24. #ifndef _GLIBCXX_TR1_RANDOM_TCC
  25. #define _GLIBCXX_TR1_RANDOM_TCC 1
  26. namespace std _GLIBCXX_VISIBILITY(default)
  27. {
  28. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  29. namespace tr1
  30. {
  31. /*
  32. * (Further) implementation-space details.
  33. */
  34. namespace __detail
  35. {
  36. // General case for x = (ax + c) mod m -- use Schrage's algorithm to avoid
  37. // integer overflow.
  38. //
  39. // Because a and c are compile-time integral constants the compiler kindly
  40. // elides any unreachable paths.
  41. //
  42. // Preconditions: a > 0, m > 0.
  43. //
  44. template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
  45. struct _Mod
  46. {
  47. static _Tp
  48. __calc(_Tp __x)
  49. {
  50. if (__a == 1)
  51. __x %= __m;
  52. else
  53. {
  54. static const _Tp __q = __m / __a;
  55. static const _Tp __r = __m % __a;
  56. _Tp __t1 = __a * (__x % __q);
  57. _Tp __t2 = __r * (__x / __q);
  58. if (__t1 >= __t2)
  59. __x = __t1 - __t2;
  60. else
  61. __x = __m - __t2 + __t1;
  62. }
  63. if (__c != 0)
  64. {
  65. const _Tp __d = __m - __x;
  66. if (__d > __c)
  67. __x += __c;
  68. else
  69. __x = __c - __d;
  70. }
  71. return __x;
  72. }
  73. };
  74. // Special case for m == 0 -- use unsigned integer overflow as modulo
  75. // operator.
  76. template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
  77. struct _Mod<_Tp, __a, __c, __m, true>
  78. {
  79. static _Tp
  80. __calc(_Tp __x)
  81. { return __a * __x + __c; }
  82. };
  83. } // namespace __detail
  84. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  85. const _UIntType
  86. linear_congruential<_UIntType, __a, __c, __m>::multiplier;
  87. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  88. const _UIntType
  89. linear_congruential<_UIntType, __a, __c, __m>::increment;
  90. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  91. const _UIntType
  92. linear_congruential<_UIntType, __a, __c, __m>::modulus;
  93. /**
  94. * Seeds the LCR with integral value @p __x0, adjusted so that the
  95. * ring identity is never a member of the convergence set.
  96. */
  97. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  98. void
  99. linear_congruential<_UIntType, __a, __c, __m>::
  100. seed(unsigned long __x0)
  101. {
  102. if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
  103. && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
  104. _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
  105. else
  106. _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
  107. }
  108. /**
  109. * Seeds the LCR engine with a value generated by @p __g.
  110. */
  111. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  112. template<class _Gen>
  113. void
  114. linear_congruential<_UIntType, __a, __c, __m>::
  115. seed(_Gen& __g, false_type)
  116. {
  117. _UIntType __x0 = __g();
  118. if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
  119. && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
  120. _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
  121. else
  122. _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
  123. }
  124. /**
  125. * Gets the next generated value in sequence.
  126. */
  127. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  128. typename linear_congruential<_UIntType, __a, __c, __m>::result_type
  129. linear_congruential<_UIntType, __a, __c, __m>::
  130. operator()()
  131. {
  132. _M_x = __detail::__mod<_UIntType, __a, __c, __m>(_M_x);
  133. return _M_x;
  134. }
  135. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
  136. typename _CharT, typename _Traits>
  137. std::basic_ostream<_CharT, _Traits>&
  138. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  139. const linear_congruential<_UIntType, __a, __c, __m>& __lcr)
  140. {
  141. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  142. typedef typename __ostream_type::ios_base __ios_base;
  143. const typename __ios_base::fmtflags __flags = __os.flags();
  144. const _CharT __fill = __os.fill();
  145. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  146. __os.fill(__os.widen(' '));
  147. __os << __lcr._M_x;
  148. __os.flags(__flags);
  149. __os.fill(__fill);
  150. return __os;
  151. }
  152. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
  153. typename _CharT, typename _Traits>
  154. std::basic_istream<_CharT, _Traits>&
  155. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  156. linear_congruential<_UIntType, __a, __c, __m>& __lcr)
  157. {
  158. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  159. typedef typename __istream_type::ios_base __ios_base;
  160. const typename __ios_base::fmtflags __flags = __is.flags();
  161. __is.flags(__ios_base::dec);
  162. __is >> __lcr._M_x;
  163. __is.flags(__flags);
  164. return __is;
  165. }
  166. template<class _UIntType, int __w, int __n, int __m, int __r,
  167. _UIntType __a, int __u, int __s,
  168. _UIntType __b, int __t, _UIntType __c, int __l>
  169. const int
  170. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  171. __b, __t, __c, __l>::word_size;
  172. template<class _UIntType, int __w, int __n, int __m, int __r,
  173. _UIntType __a, int __u, int __s,
  174. _UIntType __b, int __t, _UIntType __c, int __l>
  175. const int
  176. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  177. __b, __t, __c, __l>::state_size;
  178. template<class _UIntType, int __w, int __n, int __m, int __r,
  179. _UIntType __a, int __u, int __s,
  180. _UIntType __b, int __t, _UIntType __c, int __l>
  181. const int
  182. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  183. __b, __t, __c, __l>::shift_size;
  184. template<class _UIntType, int __w, int __n, int __m, int __r,
  185. _UIntType __a, int __u, int __s,
  186. _UIntType __b, int __t, _UIntType __c, int __l>
  187. const int
  188. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  189. __b, __t, __c, __l>::mask_bits;
  190. template<class _UIntType, int __w, int __n, int __m, int __r,
  191. _UIntType __a, int __u, int __s,
  192. _UIntType __b, int __t, _UIntType __c, int __l>
  193. const _UIntType
  194. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  195. __b, __t, __c, __l>::parameter_a;
  196. template<class _UIntType, int __w, int __n, int __m, int __r,
  197. _UIntType __a, int __u, int __s,
  198. _UIntType __b, int __t, _UIntType __c, int __l>
  199. const int
  200. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  201. __b, __t, __c, __l>::output_u;
  202. template<class _UIntType, int __w, int __n, int __m, int __r,
  203. _UIntType __a, int __u, int __s,
  204. _UIntType __b, int __t, _UIntType __c, int __l>
  205. const int
  206. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  207. __b, __t, __c, __l>::output_s;
  208. template<class _UIntType, int __w, int __n, int __m, int __r,
  209. _UIntType __a, int __u, int __s,
  210. _UIntType __b, int __t, _UIntType __c, int __l>
  211. const _UIntType
  212. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  213. __b, __t, __c, __l>::output_b;
  214. template<class _UIntType, int __w, int __n, int __m, int __r,
  215. _UIntType __a, int __u, int __s,
  216. _UIntType __b, int __t, _UIntType __c, int __l>
  217. const int
  218. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  219. __b, __t, __c, __l>::output_t;
  220. template<class _UIntType, int __w, int __n, int __m, int __r,
  221. _UIntType __a, int __u, int __s,
  222. _UIntType __b, int __t, _UIntType __c, int __l>
  223. const _UIntType
  224. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  225. __b, __t, __c, __l>::output_c;
  226. template<class _UIntType, int __w, int __n, int __m, int __r,
  227. _UIntType __a, int __u, int __s,
  228. _UIntType __b, int __t, _UIntType __c, int __l>
  229. const int
  230. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  231. __b, __t, __c, __l>::output_l;
  232. template<class _UIntType, int __w, int __n, int __m, int __r,
  233. _UIntType __a, int __u, int __s,
  234. _UIntType __b, int __t, _UIntType __c, int __l>
  235. void
  236. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  237. __b, __t, __c, __l>::
  238. seed(unsigned long __value)
  239. {
  240. _M_x[0] = __detail::__mod<_UIntType, 1, 0,
  241. __detail::_Shift<_UIntType, __w>::__value>(__value);
  242. for (int __i = 1; __i < state_size; ++__i)
  243. {
  244. _UIntType __x = _M_x[__i - 1];
  245. __x ^= __x >> (__w - 2);
  246. __x *= 1812433253ul;
  247. __x += __i;
  248. _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
  249. __detail::_Shift<_UIntType, __w>::__value>(__x);
  250. }
  251. _M_p = state_size;
  252. }
  253. template<class _UIntType, int __w, int __n, int __m, int __r,
  254. _UIntType __a, int __u, int __s,
  255. _UIntType __b, int __t, _UIntType __c, int __l>
  256. template<class _Gen>
  257. void
  258. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  259. __b, __t, __c, __l>::
  260. seed(_Gen& __gen, false_type)
  261. {
  262. for (int __i = 0; __i < state_size; ++__i)
  263. _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
  264. __detail::_Shift<_UIntType, __w>::__value>(__gen());
  265. _M_p = state_size;
  266. }
  267. template<class _UIntType, int __w, int __n, int __m, int __r,
  268. _UIntType __a, int __u, int __s,
  269. _UIntType __b, int __t, _UIntType __c, int __l>
  270. typename
  271. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  272. __b, __t, __c, __l>::result_type
  273. mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
  274. __b, __t, __c, __l>::
  275. operator()()
  276. {
  277. // Reload the vector - cost is O(n) amortized over n calls.
  278. if (_M_p >= state_size)
  279. {
  280. const _UIntType __upper_mask = (~_UIntType()) << __r;
  281. const _UIntType __lower_mask = ~__upper_mask;
  282. for (int __k = 0; __k < (__n - __m); ++__k)
  283. {
  284. _UIntType __y = ((_M_x[__k] & __upper_mask)
  285. | (_M_x[__k + 1] & __lower_mask));
  286. _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
  287. ^ ((__y & 0x01) ? __a : 0));
  288. }
  289. for (int __k = (__n - __m); __k < (__n - 1); ++__k)
  290. {
  291. _UIntType __y = ((_M_x[__k] & __upper_mask)
  292. | (_M_x[__k + 1] & __lower_mask));
  293. _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
  294. ^ ((__y & 0x01) ? __a : 0));
  295. }
  296. _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
  297. | (_M_x[0] & __lower_mask));
  298. _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
  299. ^ ((__y & 0x01) ? __a : 0));
  300. _M_p = 0;
  301. }
  302. // Calculate o(x(i)).
  303. result_type __z = _M_x[_M_p++];
  304. __z ^= (__z >> __u);
  305. __z ^= (__z << __s) & __b;
  306. __z ^= (__z << __t) & __c;
  307. __z ^= (__z >> __l);
  308. return __z;
  309. }
  310. template<class _UIntType, int __w, int __n, int __m, int __r,
  311. _UIntType __a, int __u, int __s, _UIntType __b, int __t,
  312. _UIntType __c, int __l,
  313. typename _CharT, typename _Traits>
  314. std::basic_ostream<_CharT, _Traits>&
  315. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  316. const mersenne_twister<_UIntType, __w, __n, __m,
  317. __r, __a, __u, __s, __b, __t, __c, __l>& __x)
  318. {
  319. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  320. typedef typename __ostream_type::ios_base __ios_base;
  321. const typename __ios_base::fmtflags __flags = __os.flags();
  322. const _CharT __fill = __os.fill();
  323. const _CharT __space = __os.widen(' ');
  324. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  325. __os.fill(__space);
  326. for (int __i = 0; __i < __n - 1; ++__i)
  327. __os << __x._M_x[__i] << __space;
  328. __os << __x._M_x[__n - 1];
  329. __os.flags(__flags);
  330. __os.fill(__fill);
  331. return __os;
  332. }
  333. template<class _UIntType, int __w, int __n, int __m, int __r,
  334. _UIntType __a, int __u, int __s, _UIntType __b, int __t,
  335. _UIntType __c, int __l,
  336. typename _CharT, typename _Traits>
  337. std::basic_istream<_CharT, _Traits>&
  338. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  339. mersenne_twister<_UIntType, __w, __n, __m,
  340. __r, __a, __u, __s, __b, __t, __c, __l>& __x)
  341. {
  342. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  343. typedef typename __istream_type::ios_base __ios_base;
  344. const typename __ios_base::fmtflags __flags = __is.flags();
  345. __is.flags(__ios_base::dec | __ios_base::skipws);
  346. for (int __i = 0; __i < __n; ++__i)
  347. __is >> __x._M_x[__i];
  348. __is.flags(__flags);
  349. return __is;
  350. }
  351. template<typename _IntType, _IntType __m, int __s, int __r>
  352. const _IntType
  353. subtract_with_carry<_IntType, __m, __s, __r>::modulus;
  354. template<typename _IntType, _IntType __m, int __s, int __r>
  355. const int
  356. subtract_with_carry<_IntType, __m, __s, __r>::long_lag;
  357. template<typename _IntType, _IntType __m, int __s, int __r>
  358. const int
  359. subtract_with_carry<_IntType, __m, __s, __r>::short_lag;
  360. template<typename _IntType, _IntType __m, int __s, int __r>
  361. void
  362. subtract_with_carry<_IntType, __m, __s, __r>::
  363. seed(unsigned long __value)
  364. {
  365. if (__value == 0)
  366. __value = 19780503;
  367. std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
  368. __lcg(__value);
  369. for (int __i = 0; __i < long_lag; ++__i)
  370. _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__lcg());
  371. _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
  372. _M_p = 0;
  373. }
  374. template<typename _IntType, _IntType __m, int __s, int __r>
  375. template<class _Gen>
  376. void
  377. subtract_with_carry<_IntType, __m, __s, __r>::
  378. seed(_Gen& __gen, false_type)
  379. {
  380. const int __n = (std::numeric_limits<_UIntType>::digits + 31) / 32;
  381. for (int __i = 0; __i < long_lag; ++__i)
  382. {
  383. _UIntType __tmp = 0;
  384. _UIntType __factor = 1;
  385. for (int __j = 0; __j < __n; ++__j)
  386. {
  387. __tmp += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
  388. (__gen()) * __factor;
  389. __factor *= __detail::_Shift<_UIntType, 32>::__value;
  390. }
  391. _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__tmp);
  392. }
  393. _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
  394. _M_p = 0;
  395. }
  396. template<typename _IntType, _IntType __m, int __s, int __r>
  397. typename subtract_with_carry<_IntType, __m, __s, __r>::result_type
  398. subtract_with_carry<_IntType, __m, __s, __r>::
  399. operator()()
  400. {
  401. // Derive short lag index from current index.
  402. int __ps = _M_p - short_lag;
  403. if (__ps < 0)
  404. __ps += long_lag;
  405. // Calculate new x(i) without overflow or division.
  406. // NB: Thanks to the requirements for _IntType, _M_x[_M_p] + _M_carry
  407. // cannot overflow.
  408. _UIntType __xi;
  409. if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
  410. {
  411. __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
  412. _M_carry = 0;
  413. }
  414. else
  415. {
  416. __xi = modulus - _M_x[_M_p] - _M_carry + _M_x[__ps];
  417. _M_carry = 1;
  418. }
  419. _M_x[_M_p] = __xi;
  420. // Adjust current index to loop around in ring buffer.
  421. if (++_M_p >= long_lag)
  422. _M_p = 0;
  423. return __xi;
  424. }
  425. template<typename _IntType, _IntType __m, int __s, int __r,
  426. typename _CharT, typename _Traits>
  427. std::basic_ostream<_CharT, _Traits>&
  428. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  429. const subtract_with_carry<_IntType, __m, __s, __r>& __x)
  430. {
  431. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  432. typedef typename __ostream_type::ios_base __ios_base;
  433. const typename __ios_base::fmtflags __flags = __os.flags();
  434. const _CharT __fill = __os.fill();
  435. const _CharT __space = __os.widen(' ');
  436. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  437. __os.fill(__space);
  438. for (int __i = 0; __i < __r; ++__i)
  439. __os << __x._M_x[__i] << __space;
  440. __os << __x._M_carry;
  441. __os.flags(__flags);
  442. __os.fill(__fill);
  443. return __os;
  444. }
  445. template<typename _IntType, _IntType __m, int __s, int __r,
  446. typename _CharT, typename _Traits>
  447. std::basic_istream<_CharT, _Traits>&
  448. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  449. subtract_with_carry<_IntType, __m, __s, __r>& __x)
  450. {
  451. typedef std::basic_ostream<_CharT, _Traits> __istream_type;
  452. typedef typename __istream_type::ios_base __ios_base;
  453. const typename __ios_base::fmtflags __flags = __is.flags();
  454. __is.flags(__ios_base::dec | __ios_base::skipws);
  455. for (int __i = 0; __i < __r; ++__i)
  456. __is >> __x._M_x[__i];
  457. __is >> __x._M_carry;
  458. __is.flags(__flags);
  459. return __is;
  460. }
  461. template<typename _RealType, int __w, int __s, int __r>
  462. const int
  463. subtract_with_carry_01<_RealType, __w, __s, __r>::word_size;
  464. template<typename _RealType, int __w, int __s, int __r>
  465. const int
  466. subtract_with_carry_01<_RealType, __w, __s, __r>::long_lag;
  467. template<typename _RealType, int __w, int __s, int __r>
  468. const int
  469. subtract_with_carry_01<_RealType, __w, __s, __r>::short_lag;
  470. template<typename _RealType, int __w, int __s, int __r>
  471. void
  472. subtract_with_carry_01<_RealType, __w, __s, __r>::
  473. _M_initialize_npows()
  474. {
  475. for (int __j = 0; __j < __n; ++__j)
  476. #if _GLIBCXX_USE_C99_MATH_TR1
  477. _M_npows[__j] = std::tr1::ldexp(_RealType(1), -__w + __j * 32);
  478. #else
  479. _M_npows[__j] = std::pow(_RealType(2), -__w + __j * 32);
  480. #endif
  481. }
  482. template<typename _RealType, int __w, int __s, int __r>
  483. void
  484. subtract_with_carry_01<_RealType, __w, __s, __r>::
  485. seed(unsigned long __value)
  486. {
  487. if (__value == 0)
  488. __value = 19780503;
  489. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  490. // 512. Seeding subtract_with_carry_01 from a single unsigned long.
  491. std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
  492. __lcg(__value);
  493. this->seed(__lcg);
  494. }
  495. template<typename _RealType, int __w, int __s, int __r>
  496. template<class _Gen>
  497. void
  498. subtract_with_carry_01<_RealType, __w, __s, __r>::
  499. seed(_Gen& __gen, false_type)
  500. {
  501. for (int __i = 0; __i < long_lag; ++__i)
  502. {
  503. for (int __j = 0; __j < __n - 1; ++__j)
  504. _M_x[__i][__j] = __detail::__mod<_UInt32Type, 1, 0, 0>(__gen());
  505. _M_x[__i][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
  506. __detail::_Shift<_UInt32Type, __w % 32>::__value>(__gen());
  507. }
  508. _M_carry = 1;
  509. for (int __j = 0; __j < __n; ++__j)
  510. if (_M_x[long_lag - 1][__j] != 0)
  511. {
  512. _M_carry = 0;
  513. break;
  514. }
  515. _M_p = 0;
  516. }
  517. template<typename _RealType, int __w, int __s, int __r>
  518. typename subtract_with_carry_01<_RealType, __w, __s, __r>::result_type
  519. subtract_with_carry_01<_RealType, __w, __s, __r>::
  520. operator()()
  521. {
  522. // Derive short lag index from current index.
  523. int __ps = _M_p - short_lag;
  524. if (__ps < 0)
  525. __ps += long_lag;
  526. _UInt32Type __new_carry;
  527. for (int __j = 0; __j < __n - 1; ++__j)
  528. {
  529. if (_M_x[__ps][__j] > _M_x[_M_p][__j]
  530. || (_M_x[__ps][__j] == _M_x[_M_p][__j] && _M_carry == 0))
  531. __new_carry = 0;
  532. else
  533. __new_carry = 1;
  534. _M_x[_M_p][__j] = _M_x[__ps][__j] - _M_x[_M_p][__j] - _M_carry;
  535. _M_carry = __new_carry;
  536. }
  537. if (_M_x[__ps][__n - 1] > _M_x[_M_p][__n - 1]
  538. || (_M_x[__ps][__n - 1] == _M_x[_M_p][__n - 1] && _M_carry == 0))
  539. __new_carry = 0;
  540. else
  541. __new_carry = 1;
  542. _M_x[_M_p][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
  543. __detail::_Shift<_UInt32Type, __w % 32>::__value>
  544. (_M_x[__ps][__n - 1] - _M_x[_M_p][__n - 1] - _M_carry);
  545. _M_carry = __new_carry;
  546. result_type __ret = 0.0;
  547. for (int __j = 0; __j < __n; ++__j)
  548. __ret += _M_x[_M_p][__j] * _M_npows[__j];
  549. // Adjust current index to loop around in ring buffer.
  550. if (++_M_p >= long_lag)
  551. _M_p = 0;
  552. return __ret;
  553. }
  554. template<typename _RealType, int __w, int __s, int __r,
  555. typename _CharT, typename _Traits>
  556. std::basic_ostream<_CharT, _Traits>&
  557. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  558. const subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
  559. {
  560. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  561. typedef typename __ostream_type::ios_base __ios_base;
  562. const typename __ios_base::fmtflags __flags = __os.flags();
  563. const _CharT __fill = __os.fill();
  564. const _CharT __space = __os.widen(' ');
  565. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  566. __os.fill(__space);
  567. for (int __i = 0; __i < __r; ++__i)
  568. for (int __j = 0; __j < __x.__n; ++__j)
  569. __os << __x._M_x[__i][__j] << __space;
  570. __os << __x._M_carry;
  571. __os.flags(__flags);
  572. __os.fill(__fill);
  573. return __os;
  574. }
  575. template<typename _RealType, int __w, int __s, int __r,
  576. typename _CharT, typename _Traits>
  577. std::basic_istream<_CharT, _Traits>&
  578. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  579. subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
  580. {
  581. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  582. typedef typename __istream_type::ios_base __ios_base;
  583. const typename __ios_base::fmtflags __flags = __is.flags();
  584. __is.flags(__ios_base::dec | __ios_base::skipws);
  585. for (int __i = 0; __i < __r; ++__i)
  586. for (int __j = 0; __j < __x.__n; ++__j)
  587. __is >> __x._M_x[__i][__j];
  588. __is >> __x._M_carry;
  589. __is.flags(__flags);
  590. return __is;
  591. }
  592. template<class _UniformRandomNumberGenerator, int __p, int __r>
  593. const int
  594. discard_block<_UniformRandomNumberGenerator, __p, __r>::block_size;
  595. template<class _UniformRandomNumberGenerator, int __p, int __r>
  596. const int
  597. discard_block<_UniformRandomNumberGenerator, __p, __r>::used_block;
  598. template<class _UniformRandomNumberGenerator, int __p, int __r>
  599. typename discard_block<_UniformRandomNumberGenerator,
  600. __p, __r>::result_type
  601. discard_block<_UniformRandomNumberGenerator, __p, __r>::
  602. operator()()
  603. {
  604. if (_M_n >= used_block)
  605. {
  606. while (_M_n < block_size)
  607. {
  608. _M_b();
  609. ++_M_n;
  610. }
  611. _M_n = 0;
  612. }
  613. ++_M_n;
  614. return _M_b();
  615. }
  616. template<class _UniformRandomNumberGenerator, int __p, int __r,
  617. typename _CharT, typename _Traits>
  618. std::basic_ostream<_CharT, _Traits>&
  619. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  620. const discard_block<_UniformRandomNumberGenerator,
  621. __p, __r>& __x)
  622. {
  623. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  624. typedef typename __ostream_type::ios_base __ios_base;
  625. const typename __ios_base::fmtflags __flags = __os.flags();
  626. const _CharT __fill = __os.fill();
  627. const _CharT __space = __os.widen(' ');
  628. __os.flags(__ios_base::dec | __ios_base::fixed
  629. | __ios_base::left);
  630. __os.fill(__space);
  631. __os << __x._M_b << __space << __x._M_n;
  632. __os.flags(__flags);
  633. __os.fill(__fill);
  634. return __os;
  635. }
  636. template<class _UniformRandomNumberGenerator, int __p, int __r,
  637. typename _CharT, typename _Traits>
  638. std::basic_istream<_CharT, _Traits>&
  639. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  640. discard_block<_UniformRandomNumberGenerator, __p, __r>& __x)
  641. {
  642. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  643. typedef typename __istream_type::ios_base __ios_base;
  644. const typename __ios_base::fmtflags __flags = __is.flags();
  645. __is.flags(__ios_base::dec | __ios_base::skipws);
  646. __is >> __x._M_b >> __x._M_n;
  647. __is.flags(__flags);
  648. return __is;
  649. }
  650. template<class _UniformRandomNumberGenerator1, int __s1,
  651. class _UniformRandomNumberGenerator2, int __s2>
  652. const int
  653. xor_combine<_UniformRandomNumberGenerator1, __s1,
  654. _UniformRandomNumberGenerator2, __s2>::shift1;
  655. template<class _UniformRandomNumberGenerator1, int __s1,
  656. class _UniformRandomNumberGenerator2, int __s2>
  657. const int
  658. xor_combine<_UniformRandomNumberGenerator1, __s1,
  659. _UniformRandomNumberGenerator2, __s2>::shift2;
  660. template<class _UniformRandomNumberGenerator1, int __s1,
  661. class _UniformRandomNumberGenerator2, int __s2>
  662. void
  663. xor_combine<_UniformRandomNumberGenerator1, __s1,
  664. _UniformRandomNumberGenerator2, __s2>::
  665. _M_initialize_max()
  666. {
  667. const int __w = std::numeric_limits<result_type>::digits;
  668. const result_type __m1 =
  669. std::min(result_type(_M_b1.max() - _M_b1.min()),
  670. __detail::_Shift<result_type, __w - __s1>::__value - 1);
  671. const result_type __m2 =
  672. std::min(result_type(_M_b2.max() - _M_b2.min()),
  673. __detail::_Shift<result_type, __w - __s2>::__value - 1);
  674. // NB: In TR1 s1 is not required to be >= s2.
  675. if (__s1 < __s2)
  676. _M_max = _M_initialize_max_aux(__m2, __m1, __s2 - __s1) << __s1;
  677. else
  678. _M_max = _M_initialize_max_aux(__m1, __m2, __s1 - __s2) << __s2;
  679. }
  680. template<class _UniformRandomNumberGenerator1, int __s1,
  681. class _UniformRandomNumberGenerator2, int __s2>
  682. typename xor_combine<_UniformRandomNumberGenerator1, __s1,
  683. _UniformRandomNumberGenerator2, __s2>::result_type
  684. xor_combine<_UniformRandomNumberGenerator1, __s1,
  685. _UniformRandomNumberGenerator2, __s2>::
  686. _M_initialize_max_aux(result_type __a, result_type __b, int __d)
  687. {
  688. const result_type __two2d = result_type(1) << __d;
  689. const result_type __c = __a * __two2d;
  690. if (__a == 0 || __b < __two2d)
  691. return __c + __b;
  692. const result_type __t = std::max(__c, __b);
  693. const result_type __u = std::min(__c, __b);
  694. result_type __ub = __u;
  695. result_type __p;
  696. for (__p = 0; __ub != 1; __ub >>= 1)
  697. ++__p;
  698. const result_type __two2p = result_type(1) << __p;
  699. const result_type __k = __t / __two2p;
  700. if (__k & 1)
  701. return (__k + 1) * __two2p - 1;
  702. if (__c >= __b)
  703. return (__k + 1) * __two2p + _M_initialize_max_aux((__t % __two2p)
  704. / __two2d,
  705. __u % __two2p, __d);
  706. else
  707. return (__k + 1) * __two2p + _M_initialize_max_aux((__u % __two2p)
  708. / __two2d,
  709. __t % __two2p, __d);
  710. }
  711. template<class _UniformRandomNumberGenerator1, int __s1,
  712. class _UniformRandomNumberGenerator2, int __s2,
  713. typename _CharT, typename _Traits>
  714. std::basic_ostream<_CharT, _Traits>&
  715. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  716. const xor_combine<_UniformRandomNumberGenerator1, __s1,
  717. _UniformRandomNumberGenerator2, __s2>& __x)
  718. {
  719. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  720. typedef typename __ostream_type::ios_base __ios_base;
  721. const typename __ios_base::fmtflags __flags = __os.flags();
  722. const _CharT __fill = __os.fill();
  723. const _CharT __space = __os.widen(' ');
  724. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  725. __os.fill(__space);
  726. __os << __x.base1() << __space << __x.base2();
  727. __os.flags(__flags);
  728. __os.fill(__fill);
  729. return __os;
  730. }
  731. template<class _UniformRandomNumberGenerator1, int __s1,
  732. class _UniformRandomNumberGenerator2, int __s2,
  733. typename _CharT, typename _Traits>
  734. std::basic_istream<_CharT, _Traits>&
  735. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  736. xor_combine<_UniformRandomNumberGenerator1, __s1,
  737. _UniformRandomNumberGenerator2, __s2>& __x)
  738. {
  739. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  740. typedef typename __istream_type::ios_base __ios_base;
  741. const typename __ios_base::fmtflags __flags = __is.flags();
  742. __is.flags(__ios_base::skipws);
  743. __is >> __x._M_b1 >> __x._M_b2;
  744. __is.flags(__flags);
  745. return __is;
  746. }
  747. template<typename _IntType>
  748. template<typename _UniformRandomNumberGenerator>
  749. typename uniform_int<_IntType>::result_type
  750. uniform_int<_IntType>::
  751. _M_call(_UniformRandomNumberGenerator& __urng,
  752. result_type __min, result_type __max, true_type)
  753. {
  754. // XXX Must be fixed to work well for *arbitrary* __urng.max(),
  755. // __urng.min(), __max, __min. Currently works fine only in the
  756. // most common case __urng.max() - __urng.min() >= __max - __min,
  757. // with __urng.max() > __urng.min() >= 0.
  758. typedef typename __gnu_cxx::__add_unsigned<typename
  759. _UniformRandomNumberGenerator::result_type>::__type __urntype;
  760. typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
  761. __utype;
  762. typedef typename __gnu_cxx::__conditional_type<(sizeof(__urntype)
  763. > sizeof(__utype)),
  764. __urntype, __utype>::__type __uctype;
  765. result_type __ret;
  766. const __urntype __urnmin = __urng.min();
  767. const __urntype __urnmax = __urng.max();
  768. const __urntype __urnrange = __urnmax - __urnmin;
  769. const __uctype __urange = __max - __min;
  770. const __uctype __udenom = (__urnrange <= __urange
  771. ? 1 : __urnrange / (__urange + 1));
  772. do
  773. __ret = (__urntype(__urng()) - __urnmin) / __udenom;
  774. while (__ret > __max - __min);
  775. return __ret + __min;
  776. }
  777. template<typename _IntType, typename _CharT, typename _Traits>
  778. std::basic_ostream<_CharT, _Traits>&
  779. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  780. const uniform_int<_IntType>& __x)
  781. {
  782. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  783. typedef typename __ostream_type::ios_base __ios_base;
  784. const typename __ios_base::fmtflags __flags = __os.flags();
  785. const _CharT __fill = __os.fill();
  786. const _CharT __space = __os.widen(' ');
  787. __os.flags(__ios_base::scientific | __ios_base::left);
  788. __os.fill(__space);
  789. __os << __x.min() << __space << __x.max();
  790. __os.flags(__flags);
  791. __os.fill(__fill);
  792. return __os;
  793. }
  794. template<typename _IntType, typename _CharT, typename _Traits>
  795. std::basic_istream<_CharT, _Traits>&
  796. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  797. uniform_int<_IntType>& __x)
  798. {
  799. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  800. typedef typename __istream_type::ios_base __ios_base;
  801. const typename __ios_base::fmtflags __flags = __is.flags();
  802. __is.flags(__ios_base::dec | __ios_base::skipws);
  803. __is >> __x._M_min >> __x._M_max;
  804. __is.flags(__flags);
  805. return __is;
  806. }
  807. template<typename _CharT, typename _Traits>
  808. std::basic_ostream<_CharT, _Traits>&
  809. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  810. const bernoulli_distribution& __x)
  811. {
  812. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  813. typedef typename __ostream_type::ios_base __ios_base;
  814. const typename __ios_base::fmtflags __flags = __os.flags();
  815. const _CharT __fill = __os.fill();
  816. const std::streamsize __precision = __os.precision();
  817. __os.flags(__ios_base::scientific | __ios_base::left);
  818. __os.fill(__os.widen(' '));
  819. __os.precision(__gnu_cxx::__numeric_traits<double>::__max_digits10);
  820. __os << __x.p();
  821. __os.flags(__flags);
  822. __os.fill(__fill);
  823. __os.precision(__precision);
  824. return __os;
  825. }
  826. template<typename _IntType, typename _RealType>
  827. template<class _UniformRandomNumberGenerator>
  828. typename geometric_distribution<_IntType, _RealType>::result_type
  829. geometric_distribution<_IntType, _RealType>::
  830. operator()(_UniformRandomNumberGenerator& __urng)
  831. {
  832. // About the epsilon thing see this thread:
  833. // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
  834. const _RealType __naf =
  835. (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
  836. // The largest _RealType convertible to _IntType.
  837. const _RealType __thr =
  838. std::numeric_limits<_IntType>::max() + __naf;
  839. _RealType __cand;
  840. do
  841. __cand = std::ceil(std::log(__urng()) / _M_log_p);
  842. while (__cand >= __thr);
  843. return result_type(__cand + __naf);
  844. }
  845. template<typename _IntType, typename _RealType,
  846. typename _CharT, typename _Traits>
  847. std::basic_ostream<_CharT, _Traits>&
  848. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  849. const geometric_distribution<_IntType, _RealType>& __x)
  850. {
  851. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  852. typedef typename __ostream_type::ios_base __ios_base;
  853. const typename __ios_base::fmtflags __flags = __os.flags();
  854. const _CharT __fill = __os.fill();
  855. const std::streamsize __precision = __os.precision();
  856. __os.flags(__ios_base::scientific | __ios_base::left);
  857. __os.fill(__os.widen(' '));
  858. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  859. __os << __x.p();
  860. __os.flags(__flags);
  861. __os.fill(__fill);
  862. __os.precision(__precision);
  863. return __os;
  864. }
  865. template<typename _IntType, typename _RealType>
  866. void
  867. poisson_distribution<_IntType, _RealType>::
  868. _M_initialize()
  869. {
  870. #if _GLIBCXX_USE_C99_MATH_TR1
  871. if (_M_mean >= 12)
  872. {
  873. const _RealType __m = std::floor(_M_mean);
  874. _M_lm_thr = std::log(_M_mean);
  875. _M_lfm = std::tr1::lgamma(__m + 1);
  876. _M_sm = std::sqrt(__m);
  877. const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
  878. const _RealType __dx = std::sqrt(2 * __m * std::log(32 * __m
  879. / __pi_4));
  880. _M_d = std::tr1::round(std::max(_RealType(6),
  881. std::min(__m, __dx)));
  882. const _RealType __cx = 2 * __m + _M_d;
  883. _M_scx = std::sqrt(__cx / 2);
  884. _M_1cx = 1 / __cx;
  885. _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
  886. _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2)) / _M_d;
  887. }
  888. else
  889. #endif
  890. _M_lm_thr = std::exp(-_M_mean);
  891. }
  892. /**
  893. * A rejection algorithm when mean >= 12 and a simple method based
  894. * upon the multiplication of uniform random variates otherwise.
  895. * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
  896. * is defined.
  897. *
  898. * Reference:
  899. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  900. * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
  901. */
  902. template<typename _IntType, typename _RealType>
  903. template<class _UniformRandomNumberGenerator>
  904. typename poisson_distribution<_IntType, _RealType>::result_type
  905. poisson_distribution<_IntType, _RealType>::
  906. operator()(_UniformRandomNumberGenerator& __urng)
  907. {
  908. #if _GLIBCXX_USE_C99_MATH_TR1
  909. if (_M_mean >= 12)
  910. {
  911. _RealType __x;
  912. // See comments above...
  913. const _RealType __naf =
  914. (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
  915. const _RealType __thr =
  916. std::numeric_limits<_IntType>::max() + __naf;
  917. const _RealType __m = std::floor(_M_mean);
  918. // sqrt(pi / 2)
  919. const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
  920. const _RealType __c1 = _M_sm * __spi_2;
  921. const _RealType __c2 = _M_c2b + __c1;
  922. const _RealType __c3 = __c2 + 1;
  923. const _RealType __c4 = __c3 + 1;
  924. // e^(1 / 78)
  925. const _RealType __e178 = 1.0129030479320018583185514777512983L;
  926. const _RealType __c5 = __c4 + __e178;
  927. const _RealType __c = _M_cb + __c5;
  928. const _RealType __2cx = 2 * (2 * __m + _M_d);
  929. bool __reject = true;
  930. do
  931. {
  932. const _RealType __u = __c * __urng();
  933. const _RealType __e = -std::log(__urng());
  934. _RealType __w = 0.0;
  935. if (__u <= __c1)
  936. {
  937. const _RealType __n = _M_nd(__urng);
  938. const _RealType __y = -std::abs(__n) * _M_sm - 1;
  939. __x = std::floor(__y);
  940. __w = -__n * __n / 2;
  941. if (__x < -__m)
  942. continue;
  943. }
  944. else if (__u <= __c2)
  945. {
  946. const _RealType __n = _M_nd(__urng);
  947. const _RealType __y = 1 + std::abs(__n) * _M_scx;
  948. __x = std::ceil(__y);
  949. __w = __y * (2 - __y) * _M_1cx;
  950. if (__x > _M_d)
  951. continue;
  952. }
  953. else if (__u <= __c3)
  954. // NB: This case not in the book, nor in the Errata,
  955. // but should be ok...
  956. __x = -1;
  957. else if (__u <= __c4)
  958. __x = 0;
  959. else if (__u <= __c5)
  960. __x = 1;
  961. else
  962. {
  963. const _RealType __v = -std::log(__urng());
  964. const _RealType __y = _M_d + __v * __2cx / _M_d;
  965. __x = std::ceil(__y);
  966. __w = -_M_d * _M_1cx * (1 + __y / 2);
  967. }
  968. __reject = (__w - __e - __x * _M_lm_thr
  969. > _M_lfm - std::tr1::lgamma(__x + __m + 1));
  970. __reject |= __x + __m >= __thr;
  971. } while (__reject);
  972. return result_type(__x + __m + __naf);
  973. }
  974. else
  975. #endif
  976. {
  977. _IntType __x = 0;
  978. _RealType __prod = 1.0;
  979. do
  980. {
  981. __prod *= __urng();
  982. __x += 1;
  983. }
  984. while (__prod > _M_lm_thr);
  985. return __x - 1;
  986. }
  987. }
  988. template<typename _IntType, typename _RealType,
  989. typename _CharT, typename _Traits>
  990. std::basic_ostream<_CharT, _Traits>&
  991. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  992. const poisson_distribution<_IntType, _RealType>& __x)
  993. {
  994. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  995. typedef typename __ostream_type::ios_base __ios_base;
  996. const typename __ios_base::fmtflags __flags = __os.flags();
  997. const _CharT __fill = __os.fill();
  998. const std::streamsize __precision = __os.precision();
  999. const _CharT __space = __os.widen(' ');
  1000. __os.flags(__ios_base::scientific | __ios_base::left);
  1001. __os.fill(__space);
  1002. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1003. __os << __x.mean() << __space << __x._M_nd;
  1004. __os.flags(__flags);
  1005. __os.fill(__fill);
  1006. __os.precision(__precision);
  1007. return __os;
  1008. }
  1009. template<typename _IntType, typename _RealType,
  1010. typename _CharT, typename _Traits>
  1011. std::basic_istream<_CharT, _Traits>&
  1012. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1013. poisson_distribution<_IntType, _RealType>& __x)
  1014. {
  1015. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1016. typedef typename __istream_type::ios_base __ios_base;
  1017. const typename __ios_base::fmtflags __flags = __is.flags();
  1018. __is.flags(__ios_base::skipws);
  1019. __is >> __x._M_mean >> __x._M_nd;
  1020. __x._M_initialize();
  1021. __is.flags(__flags);
  1022. return __is;
  1023. }
  1024. template<typename _IntType, typename _RealType>
  1025. void
  1026. binomial_distribution<_IntType, _RealType>::
  1027. _M_initialize()
  1028. {
  1029. const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
  1030. _M_easy = true;
  1031. #if _GLIBCXX_USE_C99_MATH_TR1
  1032. if (_M_t * __p12 >= 8)
  1033. {
  1034. _M_easy = false;
  1035. const _RealType __np = std::floor(_M_t * __p12);
  1036. const _RealType __pa = __np / _M_t;
  1037. const _RealType __1p = 1 - __pa;
  1038. const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
  1039. const _RealType __d1x =
  1040. std::sqrt(__np * __1p * std::log(32 * __np
  1041. / (81 * __pi_4 * __1p)));
  1042. _M_d1 = std::tr1::round(std::max(_RealType(1), __d1x));
  1043. const _RealType __d2x =
  1044. std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
  1045. / (__pi_4 * __pa)));
  1046. _M_d2 = std::tr1::round(std::max(_RealType(1), __d2x));
  1047. // sqrt(pi / 2)
  1048. const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
  1049. _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
  1050. _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
  1051. _M_c = 2 * _M_d1 / __np;
  1052. _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
  1053. const _RealType __a12 = _M_a1 + _M_s2 * __spi_2;
  1054. const _RealType __s1s = _M_s1 * _M_s1;
  1055. _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
  1056. * 2 * __s1s / _M_d1
  1057. * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
  1058. const _RealType __s2s = _M_s2 * _M_s2;
  1059. _M_s = (_M_a123 + 2 * __s2s / _M_d2
  1060. * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
  1061. _M_lf = (std::tr1::lgamma(__np + 1)
  1062. + std::tr1::lgamma(_M_t - __np + 1));
  1063. _M_lp1p = std::log(__pa / __1p);
  1064. _M_q = -std::log(1 - (__p12 - __pa) / __1p);
  1065. }
  1066. else
  1067. #endif
  1068. _M_q = -std::log(1 - __p12);
  1069. }
  1070. template<typename _IntType, typename _RealType>
  1071. template<class _UniformRandomNumberGenerator>
  1072. typename binomial_distribution<_IntType, _RealType>::result_type
  1073. binomial_distribution<_IntType, _RealType>::
  1074. _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
  1075. {
  1076. _IntType __x = 0;
  1077. _RealType __sum = 0;
  1078. do
  1079. {
  1080. const _RealType __e = -std::log(__urng());
  1081. __sum += __e / (__t - __x);
  1082. __x += 1;
  1083. }
  1084. while (__sum <= _M_q);
  1085. return __x - 1;
  1086. }
  1087. /**
  1088. * A rejection algorithm when t * p >= 8 and a simple waiting time
  1089. * method - the second in the referenced book - otherwise.
  1090. * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
  1091. * is defined.
  1092. *
  1093. * Reference:
  1094. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1095. * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
  1096. */
  1097. template<typename _IntType, typename _RealType>
  1098. template<class _UniformRandomNumberGenerator>
  1099. typename binomial_distribution<_IntType, _RealType>::result_type
  1100. binomial_distribution<_IntType, _RealType>::
  1101. operator()(_UniformRandomNumberGenerator& __urng)
  1102. {
  1103. result_type __ret;
  1104. const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
  1105. #if _GLIBCXX_USE_C99_MATH_TR1
  1106. if (!_M_easy)
  1107. {
  1108. _RealType __x;
  1109. // See comments above...
  1110. const _RealType __naf =
  1111. (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
  1112. const _RealType __thr =
  1113. std::numeric_limits<_IntType>::max() + __naf;
  1114. const _RealType __np = std::floor(_M_t * __p12);
  1115. const _RealType __pa = __np / _M_t;
  1116. // sqrt(pi / 2)
  1117. const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
  1118. const _RealType __a1 = _M_a1;
  1119. const _RealType __a12 = __a1 + _M_s2 * __spi_2;
  1120. const _RealType __a123 = _M_a123;
  1121. const _RealType __s1s = _M_s1 * _M_s1;
  1122. const _RealType __s2s = _M_s2 * _M_s2;
  1123. bool __reject;
  1124. do
  1125. {
  1126. const _RealType __u = _M_s * __urng();
  1127. _RealType __v;
  1128. if (__u <= __a1)
  1129. {
  1130. const _RealType __n = _M_nd(__urng);
  1131. const _RealType __y = _M_s1 * std::abs(__n);
  1132. __reject = __y >= _M_d1;
  1133. if (!__reject)
  1134. {
  1135. const _RealType __e = -std::log(__urng());
  1136. __x = std::floor(__y);
  1137. __v = -__e - __n * __n / 2 + _M_c;
  1138. }
  1139. }
  1140. else if (__u <= __a12)
  1141. {
  1142. const _RealType __n = _M_nd(__urng);
  1143. const _RealType __y = _M_s2 * std::abs(__n);
  1144. __reject = __y >= _M_d2;
  1145. if (!__reject)
  1146. {
  1147. const _RealType __e = -std::log(__urng());
  1148. __x = std::floor(-__y);
  1149. __v = -__e - __n * __n / 2;
  1150. }
  1151. }
  1152. else if (__u <= __a123)
  1153. {
  1154. const _RealType __e1 = -std::log(__urng());
  1155. const _RealType __e2 = -std::log(__urng());
  1156. const _RealType __y = _M_d1 + 2 * __s1s * __e1 / _M_d1;
  1157. __x = std::floor(__y);
  1158. __v = (-__e2 + _M_d1 * (1 / (_M_t - __np)
  1159. -__y / (2 * __s1s)));
  1160. __reject = false;
  1161. }
  1162. else
  1163. {
  1164. const _RealType __e1 = -std::log(__urng());
  1165. const _RealType __e2 = -std::log(__urng());
  1166. const _RealType __y = _M_d2 + 2 * __s2s * __e1 / _M_d2;
  1167. __x = std::floor(-__y);
  1168. __v = -__e2 - _M_d2 * __y / (2 * __s2s);
  1169. __reject = false;
  1170. }
  1171. __reject = __reject || __x < -__np || __x > _M_t - __np;
  1172. if (!__reject)
  1173. {
  1174. const _RealType __lfx =
  1175. std::tr1::lgamma(__np + __x + 1)
  1176. + std::tr1::lgamma(_M_t - (__np + __x) + 1);
  1177. __reject = __v > _M_lf - __lfx + __x * _M_lp1p;
  1178. }
  1179. __reject |= __x + __np >= __thr;
  1180. }
  1181. while (__reject);
  1182. __x += __np + __naf;
  1183. const _IntType __z = _M_waiting(__urng, _M_t - _IntType(__x));
  1184. __ret = _IntType(__x) + __z;
  1185. }
  1186. else
  1187. #endif
  1188. __ret = _M_waiting(__urng, _M_t);
  1189. if (__p12 != _M_p)
  1190. __ret = _M_t - __ret;
  1191. return __ret;
  1192. }
  1193. template<typename _IntType, typename _RealType,
  1194. typename _CharT, typename _Traits>
  1195. std::basic_ostream<_CharT, _Traits>&
  1196. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1197. const binomial_distribution<_IntType, _RealType>& __x)
  1198. {
  1199. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1200. typedef typename __ostream_type::ios_base __ios_base;
  1201. const typename __ios_base::fmtflags __flags = __os.flags();
  1202. const _CharT __fill = __os.fill();
  1203. const std::streamsize __precision = __os.precision();
  1204. const _CharT __space = __os.widen(' ');
  1205. __os.flags(__ios_base::scientific | __ios_base::left);
  1206. __os.fill(__space);
  1207. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1208. __os << __x.t() << __space << __x.p()
  1209. << __space << __x._M_nd;
  1210. __os.flags(__flags);
  1211. __os.fill(__fill);
  1212. __os.precision(__precision);
  1213. return __os;
  1214. }
  1215. template<typename _IntType, typename _RealType,
  1216. typename _CharT, typename _Traits>
  1217. std::basic_istream<_CharT, _Traits>&
  1218. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1219. binomial_distribution<_IntType, _RealType>& __x)
  1220. {
  1221. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1222. typedef typename __istream_type::ios_base __ios_base;
  1223. const typename __ios_base::fmtflags __flags = __is.flags();
  1224. __is.flags(__ios_base::dec | __ios_base::skipws);
  1225. __is >> __x._M_t >> __x._M_p >> __x._M_nd;
  1226. __x._M_initialize();
  1227. __is.flags(__flags);
  1228. return __is;
  1229. }
  1230. template<typename _RealType, typename _CharT, typename _Traits>
  1231. std::basic_ostream<_CharT, _Traits>&
  1232. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1233. const uniform_real<_RealType>& __x)
  1234. {
  1235. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1236. typedef typename __ostream_type::ios_base __ios_base;
  1237. const typename __ios_base::fmtflags __flags = __os.flags();
  1238. const _CharT __fill = __os.fill();
  1239. const std::streamsize __precision = __os.precision();
  1240. const _CharT __space = __os.widen(' ');
  1241. __os.flags(__ios_base::scientific | __ios_base::left);
  1242. __os.fill(__space);
  1243. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1244. __os << __x.min() << __space << __x.max();
  1245. __os.flags(__flags);
  1246. __os.fill(__fill);
  1247. __os.precision(__precision);
  1248. return __os;
  1249. }
  1250. template<typename _RealType, typename _CharT, typename _Traits>
  1251. std::basic_istream<_CharT, _Traits>&
  1252. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1253. uniform_real<_RealType>& __x)
  1254. {
  1255. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1256. typedef typename __istream_type::ios_base __ios_base;
  1257. const typename __ios_base::fmtflags __flags = __is.flags();
  1258. __is.flags(__ios_base::skipws);
  1259. __is >> __x._M_min >> __x._M_max;
  1260. __is.flags(__flags);
  1261. return __is;
  1262. }
  1263. template<typename _RealType, typename _CharT, typename _Traits>
  1264. std::basic_ostream<_CharT, _Traits>&
  1265. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1266. const exponential_distribution<_RealType>& __x)
  1267. {
  1268. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1269. typedef typename __ostream_type::ios_base __ios_base;
  1270. const typename __ios_base::fmtflags __flags = __os.flags();
  1271. const _CharT __fill = __os.fill();
  1272. const std::streamsize __precision = __os.precision();
  1273. __os.flags(__ios_base::scientific | __ios_base::left);
  1274. __os.fill(__os.widen(' '));
  1275. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1276. __os << __x.lambda();
  1277. __os.flags(__flags);
  1278. __os.fill(__fill);
  1279. __os.precision(__precision);
  1280. return __os;
  1281. }
  1282. /**
  1283. * Polar method due to Marsaglia.
  1284. *
  1285. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1286. * New York, 1986, Ch. V, Sect. 4.4.
  1287. */
  1288. template<typename _RealType>
  1289. template<class _UniformRandomNumberGenerator>
  1290. typename normal_distribution<_RealType>::result_type
  1291. normal_distribution<_RealType>::
  1292. operator()(_UniformRandomNumberGenerator& __urng)
  1293. {
  1294. result_type __ret;
  1295. if (_M_saved_available)
  1296. {
  1297. _M_saved_available = false;
  1298. __ret = _M_saved;
  1299. }
  1300. else
  1301. {
  1302. result_type __x, __y, __r2;
  1303. do
  1304. {
  1305. __x = result_type(2.0) * __urng() - 1.0;
  1306. __y = result_type(2.0) * __urng() - 1.0;
  1307. __r2 = __x * __x + __y * __y;
  1308. }
  1309. while (__r2 > 1.0 || __r2 == 0.0);
  1310. const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
  1311. _M_saved = __x * __mult;
  1312. _M_saved_available = true;
  1313. __ret = __y * __mult;
  1314. }
  1315. __ret = __ret * _M_sigma + _M_mean;
  1316. return __ret;
  1317. }
  1318. template<typename _RealType, typename _CharT, typename _Traits>
  1319. std::basic_ostream<_CharT, _Traits>&
  1320. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1321. const normal_distribution<_RealType>& __x)
  1322. {
  1323. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1324. typedef typename __ostream_type::ios_base __ios_base;
  1325. const typename __ios_base::fmtflags __flags = __os.flags();
  1326. const _CharT __fill = __os.fill();
  1327. const std::streamsize __precision = __os.precision();
  1328. const _CharT __space = __os.widen(' ');
  1329. __os.flags(__ios_base::scientific | __ios_base::left);
  1330. __os.fill(__space);
  1331. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1332. __os << __x._M_saved_available << __space
  1333. << __x.mean() << __space
  1334. << __x.sigma();
  1335. if (__x._M_saved_available)
  1336. __os << __space << __x._M_saved;
  1337. __os.flags(__flags);
  1338. __os.fill(__fill);
  1339. __os.precision(__precision);
  1340. return __os;
  1341. }
  1342. template<typename _RealType, typename _CharT, typename _Traits>
  1343. std::basic_istream<_CharT, _Traits>&
  1344. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1345. normal_distribution<_RealType>& __x)
  1346. {
  1347. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1348. typedef typename __istream_type::ios_base __ios_base;
  1349. const typename __ios_base::fmtflags __flags = __is.flags();
  1350. __is.flags(__ios_base::dec | __ios_base::skipws);
  1351. __is >> __x._M_saved_available >> __x._M_mean
  1352. >> __x._M_sigma;
  1353. if (__x._M_saved_available)
  1354. __is >> __x._M_saved;
  1355. __is.flags(__flags);
  1356. return __is;
  1357. }
  1358. template<typename _RealType>
  1359. void
  1360. gamma_distribution<_RealType>::
  1361. _M_initialize()
  1362. {
  1363. if (_M_alpha >= 1)
  1364. _M_l_d = std::sqrt(2 * _M_alpha - 1);
  1365. else
  1366. _M_l_d = (std::pow(_M_alpha, _M_alpha / (1 - _M_alpha))
  1367. * (1 - _M_alpha));
  1368. }
  1369. /**
  1370. * Cheng's rejection algorithm GB for alpha >= 1 and a modification
  1371. * of Vaduva's rejection from Weibull algorithm due to Devroye for
  1372. * alpha < 1.
  1373. *
  1374. * References:
  1375. * Cheng, R. C. The Generation of Gamma Random Variables with Non-integral
  1376. * Shape Parameter. Applied Statistics, 26, 71-75, 1977.
  1377. *
  1378. * Vaduva, I. Computer Generation of Gamma Gandom Variables by Rejection
  1379. * and Composition Procedures. Math. Operationsforschung and Statistik,
  1380. * Series in Statistics, 8, 545-576, 1977.
  1381. *
  1382. * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
  1383. * New York, 1986, Ch. IX, Sect. 3.4 (+ Errata!).
  1384. */
  1385. template<typename _RealType>
  1386. template<class _UniformRandomNumberGenerator>
  1387. typename gamma_distribution<_RealType>::result_type
  1388. gamma_distribution<_RealType>::
  1389. operator()(_UniformRandomNumberGenerator& __urng)
  1390. {
  1391. result_type __x;
  1392. bool __reject;
  1393. if (_M_alpha >= 1)
  1394. {
  1395. // alpha - log(4)
  1396. const result_type __b = _M_alpha
  1397. - result_type(1.3862943611198906188344642429163531L);
  1398. const result_type __c = _M_alpha + _M_l_d;
  1399. const result_type __1l = 1 / _M_l_d;
  1400. // 1 + log(9 / 2)
  1401. const result_type __k = 2.5040773967762740733732583523868748L;
  1402. do
  1403. {
  1404. const result_type __u = __urng();
  1405. const result_type __v = __urng();
  1406. const result_type __y = __1l * std::log(__v / (1 - __v));
  1407. __x = _M_alpha * std::exp(__y);
  1408. const result_type __z = __u * __v * __v;
  1409. const result_type __r = __b + __c * __y - __x;
  1410. __reject = __r < result_type(4.5) * __z - __k;
  1411. if (__reject)
  1412. __reject = __r < std::log(__z);
  1413. }
  1414. while (__reject);
  1415. }
  1416. else
  1417. {
  1418. const result_type __c = 1 / _M_alpha;
  1419. do
  1420. {
  1421. const result_type __z = -std::log(__urng());
  1422. const result_type __e = -std::log(__urng());
  1423. __x = std::pow(__z, __c);
  1424. __reject = __z + __e < _M_l_d + __x;
  1425. }
  1426. while (__reject);
  1427. }
  1428. return __x;
  1429. }
  1430. template<typename _RealType, typename _CharT, typename _Traits>
  1431. std::basic_ostream<_CharT, _Traits>&
  1432. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1433. const gamma_distribution<_RealType>& __x)
  1434. {
  1435. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1436. typedef typename __ostream_type::ios_base __ios_base;
  1437. const typename __ios_base::fmtflags __flags = __os.flags();
  1438. const _CharT __fill = __os.fill();
  1439. const std::streamsize __precision = __os.precision();
  1440. __os.flags(__ios_base::scientific | __ios_base::left);
  1441. __os.fill(__os.widen(' '));
  1442. __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
  1443. __os << __x.alpha();
  1444. __os.flags(__flags);
  1445. __os.fill(__fill);
  1446. __os.precision(__precision);
  1447. return __os;
  1448. }
  1449. }
  1450. _GLIBCXX_END_NAMESPACE_VERSION
  1451. }
  1452. #endif