random.tcc 53 KB

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