random.h 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. // random number generation -*- 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. /**
  21. * @file tr1/random.h
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{tr1/random}
  24. */
  25. #ifndef _GLIBCXX_TR1_RANDOM_H
  26. #define _GLIBCXX_TR1_RANDOM_H 1
  27. #pragma GCC system_header
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. namespace tr1
  31. {
  32. // [5.1] Random number generation
  33. /**
  34. * @addtogroup tr1_random Random Number Generation
  35. * A facility for generating random numbers on selected distributions.
  36. * @{
  37. */
  38. /*
  39. * Implementation-space details.
  40. */
  41. namespace __detail
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. template<typename _UIntType, int __w,
  45. bool = __w < std::numeric_limits<_UIntType>::digits>
  46. struct _Shift
  47. { static const _UIntType __value = 0; };
  48. template<typename _UIntType, int __w>
  49. struct _Shift<_UIntType, __w, true>
  50. { static const _UIntType __value = _UIntType(1) << __w; };
  51. template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
  52. struct _Mod;
  53. // Dispatch based on modulus value to prevent divide-by-zero compile-time
  54. // errors when m == 0.
  55. template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
  56. inline _Tp
  57. __mod(_Tp __x)
  58. { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); }
  59. typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4),
  60. unsigned, unsigned long>::__type _UInt32Type;
  61. /*
  62. * An adaptor class for converting the output of any Generator into
  63. * the input for a specific Distribution.
  64. */
  65. template<typename _Engine, typename _Distribution>
  66. struct _Adaptor
  67. {
  68. typedef typename remove_reference<_Engine>::type _BEngine;
  69. typedef typename _BEngine::result_type _Engine_result_type;
  70. typedef typename _Distribution::input_type result_type;
  71. public:
  72. _Adaptor(const _Engine& __g)
  73. : _M_g(__g) { }
  74. result_type
  75. min() const
  76. {
  77. result_type __return_value;
  78. if (is_integral<_Engine_result_type>::value
  79. && is_integral<result_type>::value)
  80. __return_value = _M_g.min();
  81. else
  82. __return_value = result_type(0);
  83. return __return_value;
  84. }
  85. result_type
  86. max() const
  87. {
  88. result_type __return_value;
  89. if (is_integral<_Engine_result_type>::value
  90. && is_integral<result_type>::value)
  91. __return_value = _M_g.max();
  92. else if (!is_integral<result_type>::value)
  93. __return_value = result_type(1);
  94. else
  95. __return_value = std::numeric_limits<result_type>::max() - 1;
  96. return __return_value;
  97. }
  98. /*
  99. * Converts a value generated by the adapted random number generator
  100. * into a value in the input domain for the dependent random number
  101. * distribution.
  102. *
  103. * Because the type traits are compile time constants only the
  104. * appropriate clause of the if statements will actually be emitted
  105. * by the compiler.
  106. */
  107. result_type
  108. operator()()
  109. {
  110. result_type __return_value;
  111. if (is_integral<_Engine_result_type>::value
  112. && is_integral<result_type>::value)
  113. __return_value = _M_g();
  114. else if (!is_integral<_Engine_result_type>::value
  115. && !is_integral<result_type>::value)
  116. __return_value = result_type(_M_g() - _M_g.min())
  117. / result_type(_M_g.max() - _M_g.min());
  118. else if (is_integral<_Engine_result_type>::value
  119. && !is_integral<result_type>::value)
  120. __return_value = result_type(_M_g() - _M_g.min())
  121. / result_type(_M_g.max() - _M_g.min() + result_type(1));
  122. else
  123. __return_value = (((_M_g() - _M_g.min())
  124. / (_M_g.max() - _M_g.min()))
  125. * std::numeric_limits<result_type>::max());
  126. return __return_value;
  127. }
  128. private:
  129. _Engine _M_g;
  130. };
  131. // Specialization for _Engine*.
  132. template<typename _Engine, typename _Distribution>
  133. struct _Adaptor<_Engine*, _Distribution>
  134. {
  135. typedef typename _Engine::result_type _Engine_result_type;
  136. typedef typename _Distribution::input_type result_type;
  137. public:
  138. _Adaptor(_Engine* __g)
  139. : _M_g(__g) { }
  140. result_type
  141. min() const
  142. {
  143. result_type __return_value;
  144. if (is_integral<_Engine_result_type>::value
  145. && is_integral<result_type>::value)
  146. __return_value = _M_g->min();
  147. else
  148. __return_value = result_type(0);
  149. return __return_value;
  150. }
  151. result_type
  152. max() const
  153. {
  154. result_type __return_value;
  155. if (is_integral<_Engine_result_type>::value
  156. && is_integral<result_type>::value)
  157. __return_value = _M_g->max();
  158. else if (!is_integral<result_type>::value)
  159. __return_value = result_type(1);
  160. else
  161. __return_value = std::numeric_limits<result_type>::max() - 1;
  162. return __return_value;
  163. }
  164. result_type
  165. operator()()
  166. {
  167. result_type __return_value;
  168. if (is_integral<_Engine_result_type>::value
  169. && is_integral<result_type>::value)
  170. __return_value = (*_M_g)();
  171. else if (!is_integral<_Engine_result_type>::value
  172. && !is_integral<result_type>::value)
  173. __return_value = result_type((*_M_g)() - _M_g->min())
  174. / result_type(_M_g->max() - _M_g->min());
  175. else if (is_integral<_Engine_result_type>::value
  176. && !is_integral<result_type>::value)
  177. __return_value = result_type((*_M_g)() - _M_g->min())
  178. / result_type(_M_g->max() - _M_g->min() + result_type(1));
  179. else
  180. __return_value = ((((*_M_g)() - _M_g->min())
  181. / (_M_g->max() - _M_g->min()))
  182. * std::numeric_limits<result_type>::max());
  183. return __return_value;
  184. }
  185. private:
  186. _Engine* _M_g;
  187. };
  188. _GLIBCXX_END_NAMESPACE_VERSION
  189. } // namespace __detail
  190. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  191. /**
  192. * Produces random numbers on a given distribution function using a
  193. * non-uniform random number generation engine.
  194. *
  195. * @todo the engine_value_type needs to be studied more carefully.
  196. */
  197. template<typename _Engine, typename _Dist>
  198. class variate_generator
  199. {
  200. // Concept requirements.
  201. __glibcxx_class_requires(_Engine, _CopyConstructibleConcept)
  202. // __glibcxx_class_requires(_Engine, _EngineConcept)
  203. // __glibcxx_class_requires(_Dist, _EngineConcept)
  204. public:
  205. typedef _Engine engine_type;
  206. typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type;
  207. typedef _Dist distribution_type;
  208. typedef typename _Dist::result_type result_type;
  209. // tr1:5.1.1 table 5.1 requirement
  210. typedef typename __gnu_cxx::__enable_if<
  211. is_arithmetic<result_type>::value, result_type>::__type _IsValidType;
  212. /**
  213. * Constructs a variate generator with the uniform random number
  214. * generator @p __eng for the random distribution @p __dist.
  215. *
  216. * @throws Any exceptions which may thrown by the copy constructors of
  217. * the @p _Engine or @p _Dist objects.
  218. */
  219. variate_generator(engine_type __eng, distribution_type __dist)
  220. : _M_engine(__eng), _M_dist(__dist) { }
  221. /**
  222. * Gets the next generated value on the distribution.
  223. */
  224. result_type
  225. operator()()
  226. { return _M_dist(_M_engine); }
  227. /**
  228. * WTF?
  229. */
  230. template<typename _Tp>
  231. result_type
  232. operator()(_Tp __value)
  233. { return _M_dist(_M_engine, __value); }
  234. /**
  235. * Gets a reference to the underlying uniform random number generator
  236. * object.
  237. */
  238. engine_value_type&
  239. engine()
  240. { return _M_engine; }
  241. /**
  242. * Gets a const reference to the underlying uniform random number
  243. * generator object.
  244. */
  245. const engine_value_type&
  246. engine() const
  247. { return _M_engine; }
  248. /**
  249. * Gets a reference to the underlying random distribution.
  250. */
  251. distribution_type&
  252. distribution()
  253. { return _M_dist; }
  254. /**
  255. * Gets a const reference to the underlying random distribution.
  256. */
  257. const distribution_type&
  258. distribution() const
  259. { return _M_dist; }
  260. /**
  261. * Gets the closed lower bound of the distribution interval.
  262. */
  263. result_type
  264. min() const
  265. { return this->distribution().min(); }
  266. /**
  267. * Gets the closed upper bound of the distribution interval.
  268. */
  269. result_type
  270. max() const
  271. { return this->distribution().max(); }
  272. private:
  273. engine_value_type _M_engine;
  274. distribution_type _M_dist;
  275. };
  276. /**
  277. * @addtogroup tr1_random_generators Random Number Generators
  278. * @ingroup tr1_random
  279. *
  280. * These classes define objects which provide random or pseudorandom
  281. * numbers, either from a discrete or a continuous interval. The
  282. * random number generator supplied as a part of this library are
  283. * all uniform random number generators which provide a sequence of
  284. * random number uniformly distributed over their range.
  285. *
  286. * A number generator is a function object with an operator() that
  287. * takes zero arguments and returns a number.
  288. *
  289. * A compliant random number generator must satisfy the following
  290. * requirements. <table border=1 cellpadding=10 cellspacing=0>
  291. * <caption align=top>Random Number Generator Requirements</caption>
  292. * <tr><td>To be documented.</td></tr> </table>
  293. *
  294. * @{
  295. */
  296. /**
  297. * @brief A model of a linear congruential random number generator.
  298. *
  299. * A random number generator that produces pseudorandom numbers using the
  300. * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
  301. *
  302. * The template parameter @p _UIntType must be an unsigned integral type
  303. * large enough to store values up to (__m-1). If the template parameter
  304. * @p __m is 0, the modulus @p __m used is
  305. * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
  306. * parameters @p __a and @p __c must be less than @p __m.
  307. *
  308. * The size of the state is @f$ 1 @f$.
  309. */
  310. template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  311. class linear_congruential
  312. {
  313. __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
  314. // __glibcpp_class_requires(__a < __m && __c < __m)
  315. public:
  316. /** The type of the generated random value. */
  317. typedef _UIntType result_type;
  318. /** The multiplier. */
  319. static const _UIntType multiplier = __a;
  320. /** An increment. */
  321. static const _UIntType increment = __c;
  322. /** The modulus. */
  323. static const _UIntType modulus = __m;
  324. /**
  325. * Constructs a %linear_congruential random number generator engine with
  326. * seed @p __s. The default seed value is 1.
  327. *
  328. * @param __s The initial seed value.
  329. */
  330. explicit
  331. linear_congruential(unsigned long __x0 = 1)
  332. { this->seed(__x0); }
  333. /**
  334. * Constructs a %linear_congruential random number generator engine
  335. * seeded from the generator function @p __g.
  336. *
  337. * @param __g The seed generator function.
  338. */
  339. template<class _Gen>
  340. linear_congruential(_Gen& __g)
  341. { this->seed(__g); }
  342. /**
  343. * Reseeds the %linear_congruential random number generator engine
  344. * sequence to the seed @g __s.
  345. *
  346. * @param __s The new seed.
  347. */
  348. void
  349. seed(unsigned long __s = 1);
  350. /**
  351. * Reseeds the %linear_congruential random number generator engine
  352. * sequence using values from the generator function @p __g.
  353. *
  354. * @param __g the seed generator function.
  355. */
  356. template<class _Gen>
  357. void
  358. seed(_Gen& __g)
  359. { seed(__g, typename is_fundamental<_Gen>::type()); }
  360. /**
  361. * Gets the smallest possible value in the output range.
  362. *
  363. * The minimum depends on the @p __c parameter: if it is zero, the
  364. * minimum generated must be > 0, otherwise 0 is allowed.
  365. */
  366. result_type
  367. min() const
  368. { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; }
  369. /**
  370. * Gets the largest possible value in the output range.
  371. */
  372. result_type
  373. max() const
  374. { return __m - 1; }
  375. /**
  376. * Gets the next random number in the sequence.
  377. */
  378. result_type
  379. operator()();
  380. /**
  381. * Compares two linear congruential random number generator
  382. * objects of the same type for equality.
  383. *
  384. * @param __lhs A linear congruential random number generator object.
  385. * @param __rhs Another linear congruential random number generator obj.
  386. *
  387. * @returns true if the two objects are equal, false otherwise.
  388. */
  389. friend bool
  390. operator==(const linear_congruential& __lhs,
  391. const linear_congruential& __rhs)
  392. { return __lhs._M_x == __rhs._M_x; }
  393. /**
  394. * Compares two linear congruential random number generator
  395. * objects of the same type for inequality.
  396. *
  397. * @param __lhs A linear congruential random number generator object.
  398. * @param __rhs Another linear congruential random number generator obj.
  399. *
  400. * @returns true if the two objects are not equal, false otherwise.
  401. */
  402. friend bool
  403. operator!=(const linear_congruential& __lhs,
  404. const linear_congruential& __rhs)
  405. { return !(__lhs == __rhs); }
  406. /**
  407. * Writes the textual representation of the state x(i) of x to @p __os.
  408. *
  409. * @param __os The output stream.
  410. * @param __lcr A % linear_congruential random number generator.
  411. * @returns __os.
  412. */
  413. template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  414. _UIntType1 __m1,
  415. typename _CharT, typename _Traits>
  416. friend std::basic_ostream<_CharT, _Traits>&
  417. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  418. const linear_congruential<_UIntType1, __a1, __c1,
  419. __m1>& __lcr);
  420. /**
  421. * Sets the state of the engine by reading its textual
  422. * representation from @p __is.
  423. *
  424. * The textual representation must have been previously written using an
  425. * output stream whose imbued locale and whose type's template
  426. * specialization arguments _CharT and _Traits were the same as those of
  427. * @p __is.
  428. *
  429. * @param __is The input stream.
  430. * @param __lcr A % linear_congruential random number generator.
  431. * @returns __is.
  432. */
  433. template<class _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  434. _UIntType1 __m1,
  435. typename _CharT, typename _Traits>
  436. friend std::basic_istream<_CharT, _Traits>&
  437. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  438. linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr);
  439. private:
  440. template<class _Gen>
  441. void
  442. seed(_Gen& __g, true_type)
  443. { return seed(static_cast<unsigned long>(__g)); }
  444. template<class _Gen>
  445. void
  446. seed(_Gen& __g, false_type);
  447. _UIntType _M_x;
  448. };
  449. /**
  450. * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
  451. */
  452. typedef linear_congruential<unsigned long, 16807, 0, 2147483647> minstd_rand0;
  453. /**
  454. * An alternative LCR (Lehmer Generator function) .
  455. */
  456. typedef linear_congruential<unsigned long, 48271, 0, 2147483647> minstd_rand;
  457. /**
  458. * A generalized feedback shift register discrete random number generator.
  459. *
  460. * This algorithm avoids multiplication and division and is designed to be
  461. * friendly to a pipelined architecture. If the parameters are chosen
  462. * correctly, this generator will produce numbers with a very long period and
  463. * fairly good apparent entropy, although still not cryptographically strong.
  464. *
  465. * The best way to use this generator is with the predefined mt19937 class.
  466. *
  467. * This algorithm was originally invented by Makoto Matsumoto and
  468. * Takuji Nishimura.
  469. *
  470. * @var word_size The number of bits in each element of the state vector.
  471. * @var state_size The degree of recursion.
  472. * @var shift_size The period parameter.
  473. * @var mask_bits The separation point bit index.
  474. * @var parameter_a The last row of the twist matrix.
  475. * @var output_u The first right-shift tempering matrix parameter.
  476. * @var output_s The first left-shift tempering matrix parameter.
  477. * @var output_b The first left-shift tempering matrix mask.
  478. * @var output_t The second left-shift tempering matrix parameter.
  479. * @var output_c The second left-shift tempering matrix mask.
  480. * @var output_l The second right-shift tempering matrix parameter.
  481. */
  482. template<class _UIntType, int __w, int __n, int __m, int __r,
  483. _UIntType __a, int __u, int __s, _UIntType __b, int __t,
  484. _UIntType __c, int __l>
  485. class mersenne_twister
  486. {
  487. __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept)
  488. public:
  489. // types
  490. typedef _UIntType result_type;
  491. // parameter values
  492. static const int word_size = __w;
  493. static const int state_size = __n;
  494. static const int shift_size = __m;
  495. static const int mask_bits = __r;
  496. static const _UIntType parameter_a = __a;
  497. static const int output_u = __u;
  498. static const int output_s = __s;
  499. static const _UIntType output_b = __b;
  500. static const int output_t = __t;
  501. static const _UIntType output_c = __c;
  502. static const int output_l = __l;
  503. // constructors and member function
  504. mersenne_twister()
  505. { seed(); }
  506. explicit
  507. mersenne_twister(unsigned long __value)
  508. { seed(__value); }
  509. template<class _Gen>
  510. mersenne_twister(_Gen& __g)
  511. { seed(__g); }
  512. void
  513. seed()
  514. { seed(5489UL); }
  515. void
  516. seed(unsigned long __value);
  517. template<class _Gen>
  518. void
  519. seed(_Gen& __g)
  520. { seed(__g, typename is_fundamental<_Gen>::type()); }
  521. result_type
  522. min() const
  523. { return 0; };
  524. result_type
  525. max() const
  526. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  527. result_type
  528. operator()();
  529. /**
  530. * Compares two % mersenne_twister random number generator objects of
  531. * the same type for equality.
  532. *
  533. * @param __lhs A % mersenne_twister random number generator object.
  534. * @param __rhs Another % mersenne_twister random number generator
  535. * object.
  536. *
  537. * @returns true if the two objects are equal, false otherwise.
  538. */
  539. friend bool
  540. operator==(const mersenne_twister& __lhs,
  541. const mersenne_twister& __rhs)
  542. { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
  543. /**
  544. * Compares two % mersenne_twister random number generator objects of
  545. * the same type for inequality.
  546. *
  547. * @param __lhs A % mersenne_twister random number generator object.
  548. * @param __rhs Another % mersenne_twister random number generator
  549. * object.
  550. *
  551. * @returns true if the two objects are not equal, false otherwise.
  552. */
  553. friend bool
  554. operator!=(const mersenne_twister& __lhs,
  555. const mersenne_twister& __rhs)
  556. { return !(__lhs == __rhs); }
  557. /**
  558. * Inserts the current state of a % mersenne_twister random number
  559. * generator engine @p __x into the output stream @p __os.
  560. *
  561. * @param __os An output stream.
  562. * @param __x A % mersenne_twister random number generator engine.
  563. *
  564. * @returns The output stream with the state of @p __x inserted or in
  565. * an error state.
  566. */
  567. template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
  568. _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
  569. _UIntType1 __c1, int __l1,
  570. typename _CharT, typename _Traits>
  571. friend std::basic_ostream<_CharT, _Traits>&
  572. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  573. const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
  574. __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
  575. /**
  576. * Extracts the current state of a % mersenne_twister random number
  577. * generator engine @p __x from the input stream @p __is.
  578. *
  579. * @param __is An input stream.
  580. * @param __x A % mersenne_twister random number generator engine.
  581. *
  582. * @returns The input stream with the state of @p __x extracted or in
  583. * an error state.
  584. */
  585. template<class _UIntType1, int __w1, int __n1, int __m1, int __r1,
  586. _UIntType1 __a1, int __u1, int __s1, _UIntType1 __b1, int __t1,
  587. _UIntType1 __c1, int __l1,
  588. typename _CharT, typename _Traits>
  589. friend std::basic_istream<_CharT, _Traits>&
  590. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  591. mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1,
  592. __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x);
  593. private:
  594. template<class _Gen>
  595. void
  596. seed(_Gen& __g, true_type)
  597. { return seed(static_cast<unsigned long>(__g)); }
  598. template<class _Gen>
  599. void
  600. seed(_Gen& __g, false_type);
  601. _UIntType _M_x[state_size];
  602. int _M_p;
  603. };
  604. /**
  605. * The classic Mersenne Twister.
  606. *
  607. * Reference:
  608. * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
  609. * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
  610. * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
  611. */
  612. typedef mersenne_twister<
  613. unsigned long, 32, 624, 397, 31,
  614. 0x9908b0dful, 11, 7,
  615. 0x9d2c5680ul, 15,
  616. 0xefc60000ul, 18
  617. > mt19937;
  618. /**
  619. * @brief The Marsaglia-Zaman generator.
  620. *
  621. * This is a model of a Generalized Fibonacci discrete random number
  622. * generator, sometimes referred to as the SWC generator.
  623. *
  624. * A discrete random number generator that produces pseudorandom
  625. * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
  626. * carry_{i-1}) \bmod m @f$.
  627. *
  628. * The size of the state is @f$ r @f$
  629. * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
  630. *
  631. * N1688[4.13] says <em>the template parameter _IntType shall denote
  632. * an integral type large enough to store values up to m</em>.
  633. *
  634. * @var _M_x The state of the generator. This is a ring buffer.
  635. * @var _M_carry The carry.
  636. * @var _M_p Current index of x(i - r).
  637. */
  638. template<typename _IntType, _IntType __m, int __s, int __r>
  639. class subtract_with_carry
  640. {
  641. __glibcxx_class_requires(_IntType, _IntegerConcept)
  642. public:
  643. /** The type of the generated random value. */
  644. typedef _IntType result_type;
  645. // parameter values
  646. static const _IntType modulus = __m;
  647. static const int long_lag = __r;
  648. static const int short_lag = __s;
  649. /**
  650. * Constructs a default-initialized % subtract_with_carry random number
  651. * generator.
  652. */
  653. subtract_with_carry()
  654. { this->seed(); }
  655. /**
  656. * Constructs an explicitly seeded % subtract_with_carry random number
  657. * generator.
  658. */
  659. explicit
  660. subtract_with_carry(unsigned long __value)
  661. { this->seed(__value); }
  662. /**
  663. * Constructs a %subtract_with_carry random number generator engine
  664. * seeded from the generator function @p __g.
  665. *
  666. * @param __g The seed generator function.
  667. */
  668. template<class _Gen>
  669. subtract_with_carry(_Gen& __g)
  670. { this->seed(__g); }
  671. /**
  672. * Seeds the initial state @f$ x_0 @f$ of the random number generator.
  673. *
  674. * N1688[4.19] modifies this as follows. If @p __value == 0,
  675. * sets value to 19780503. In any case, with a linear
  676. * congruential generator lcg(i) having parameters @f$ m_{lcg} =
  677. * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
  678. * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
  679. * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
  680. * set carry to 1, otherwise sets carry to 0.
  681. */
  682. void
  683. seed(unsigned long __value = 19780503);
  684. /**
  685. * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
  686. * random number generator.
  687. */
  688. template<class _Gen>
  689. void
  690. seed(_Gen& __g)
  691. { seed(__g, typename is_fundamental<_Gen>::type()); }
  692. /**
  693. * Gets the inclusive minimum value of the range of random integers
  694. * returned by this generator.
  695. */
  696. result_type
  697. min() const
  698. { return 0; }
  699. /**
  700. * Gets the inclusive maximum value of the range of random integers
  701. * returned by this generator.
  702. */
  703. result_type
  704. max() const
  705. { return this->modulus - 1; }
  706. /**
  707. * Gets the next random number in the sequence.
  708. */
  709. result_type
  710. operator()();
  711. /**
  712. * Compares two % subtract_with_carry random number generator objects of
  713. * the same type for equality.
  714. *
  715. * @param __lhs A % subtract_with_carry random number generator object.
  716. * @param __rhs Another % subtract_with_carry random number generator
  717. * object.
  718. *
  719. * @returns true if the two objects are equal, false otherwise.
  720. */
  721. friend bool
  722. operator==(const subtract_with_carry& __lhs,
  723. const subtract_with_carry& __rhs)
  724. { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
  725. /**
  726. * Compares two % subtract_with_carry random number generator objects of
  727. * the same type for inequality.
  728. *
  729. * @param __lhs A % subtract_with_carry random number generator object.
  730. * @param __rhs Another % subtract_with_carry random number generator
  731. * object.
  732. *
  733. * @returns true if the two objects are not equal, false otherwise.
  734. */
  735. friend bool
  736. operator!=(const subtract_with_carry& __lhs,
  737. const subtract_with_carry& __rhs)
  738. { return !(__lhs == __rhs); }
  739. /**
  740. * Inserts the current state of a % subtract_with_carry random number
  741. * generator engine @p __x into the output stream @p __os.
  742. *
  743. * @param __os An output stream.
  744. * @param __x A % subtract_with_carry random number generator engine.
  745. *
  746. * @returns The output stream with the state of @p __x inserted or in
  747. * an error state.
  748. */
  749. template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
  750. typename _CharT, typename _Traits>
  751. friend std::basic_ostream<_CharT, _Traits>&
  752. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  753. const subtract_with_carry<_IntType1, __m1, __s1,
  754. __r1>& __x);
  755. /**
  756. * Extracts the current state of a % subtract_with_carry random number
  757. * generator engine @p __x from the input stream @p __is.
  758. *
  759. * @param __is An input stream.
  760. * @param __x A % subtract_with_carry random number generator engine.
  761. *
  762. * @returns The input stream with the state of @p __x extracted or in
  763. * an error state.
  764. */
  765. template<typename _IntType1, _IntType1 __m1, int __s1, int __r1,
  766. typename _CharT, typename _Traits>
  767. friend std::basic_istream<_CharT, _Traits>&
  768. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  769. subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x);
  770. private:
  771. template<class _Gen>
  772. void
  773. seed(_Gen& __g, true_type)
  774. { return seed(static_cast<unsigned long>(__g)); }
  775. template<class _Gen>
  776. void
  777. seed(_Gen& __g, false_type);
  778. typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType;
  779. _UIntType _M_x[long_lag];
  780. _UIntType _M_carry;
  781. int _M_p;
  782. };
  783. /**
  784. * @brief The Marsaglia-Zaman generator (floats version).
  785. *
  786. * @var _M_x The state of the generator. This is a ring buffer.
  787. * @var _M_carry The carry.
  788. * @var _M_p Current index of x(i - r).
  789. * @var _M_npows Precomputed negative powers of 2.
  790. */
  791. template<typename _RealType, int __w, int __s, int __r>
  792. class subtract_with_carry_01
  793. {
  794. public:
  795. /** The type of the generated random value. */
  796. typedef _RealType result_type;
  797. // parameter values
  798. static const int word_size = __w;
  799. static const int long_lag = __r;
  800. static const int short_lag = __s;
  801. /**
  802. * Constructs a default-initialized % subtract_with_carry_01 random
  803. * number generator.
  804. */
  805. subtract_with_carry_01()
  806. {
  807. this->seed();
  808. _M_initialize_npows();
  809. }
  810. /**
  811. * Constructs an explicitly seeded % subtract_with_carry_01 random number
  812. * generator.
  813. */
  814. explicit
  815. subtract_with_carry_01(unsigned long __value)
  816. {
  817. this->seed(__value);
  818. _M_initialize_npows();
  819. }
  820. /**
  821. * Constructs a % subtract_with_carry_01 random number generator engine
  822. * seeded from the generator function @p __g.
  823. *
  824. * @param __g The seed generator function.
  825. */
  826. template<class _Gen>
  827. subtract_with_carry_01(_Gen& __g)
  828. {
  829. this->seed(__g);
  830. _M_initialize_npows();
  831. }
  832. /**
  833. * Seeds the initial state @f$ x_0 @f$ of the random number generator.
  834. */
  835. void
  836. seed(unsigned long __value = 19780503);
  837. /**
  838. * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
  839. * random number generator.
  840. */
  841. template<class _Gen>
  842. void
  843. seed(_Gen& __g)
  844. { seed(__g, typename is_fundamental<_Gen>::type()); }
  845. /**
  846. * Gets the minimum value of the range of random floats
  847. * returned by this generator.
  848. */
  849. result_type
  850. min() const
  851. { return 0.0; }
  852. /**
  853. * Gets the maximum value of the range of random floats
  854. * returned by this generator.
  855. */
  856. result_type
  857. max() const
  858. { return 1.0; }
  859. /**
  860. * Gets the next random number in the sequence.
  861. */
  862. result_type
  863. operator()();
  864. /**
  865. * Compares two % subtract_with_carry_01 random number generator objects
  866. * of the same type for equality.
  867. *
  868. * @param __lhs A % subtract_with_carry_01 random number
  869. * generator object.
  870. * @param __rhs Another % subtract_with_carry_01 random number generator
  871. * object.
  872. *
  873. * @returns true if the two objects are equal, false otherwise.
  874. */
  875. friend bool
  876. operator==(const subtract_with_carry_01& __lhs,
  877. const subtract_with_carry_01& __rhs)
  878. {
  879. for (int __i = 0; __i < long_lag; ++__i)
  880. if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n,
  881. __rhs._M_x[__i]))
  882. return false;
  883. return true;
  884. }
  885. /**
  886. * Compares two % subtract_with_carry_01 random number generator objects
  887. * of the same type for inequality.
  888. *
  889. * @param __lhs A % subtract_with_carry_01 random number
  890. * generator object.
  891. *
  892. * @param __rhs Another % subtract_with_carry_01 random number generator
  893. * object.
  894. *
  895. * @returns true if the two objects are not equal, false otherwise.
  896. */
  897. friend bool
  898. operator!=(const subtract_with_carry_01& __lhs,
  899. const subtract_with_carry_01& __rhs)
  900. { return !(__lhs == __rhs); }
  901. /**
  902. * Inserts the current state of a % subtract_with_carry_01 random number
  903. * generator engine @p __x into the output stream @p __os.
  904. *
  905. * @param __os An output stream.
  906. * @param __x A % subtract_with_carry_01 random number generator engine.
  907. *
  908. * @returns The output stream with the state of @p __x inserted or in
  909. * an error state.
  910. */
  911. template<typename _RealType1, int __w1, int __s1, int __r1,
  912. typename _CharT, typename _Traits>
  913. friend std::basic_ostream<_CharT, _Traits>&
  914. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  915. const subtract_with_carry_01<_RealType1, __w1, __s1,
  916. __r1>& __x);
  917. /**
  918. * Extracts the current state of a % subtract_with_carry_01 random number
  919. * generator engine @p __x from the input stream @p __is.
  920. *
  921. * @param __is An input stream.
  922. * @param __x A % subtract_with_carry_01 random number generator engine.
  923. *
  924. * @returns The input stream with the state of @p __x extracted or in
  925. * an error state.
  926. */
  927. template<typename _RealType1, int __w1, int __s1, int __r1,
  928. typename _CharT, typename _Traits>
  929. friend std::basic_istream<_CharT, _Traits>&
  930. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  931. subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x);
  932. private:
  933. template<class _Gen>
  934. void
  935. seed(_Gen& __g, true_type)
  936. { return seed(static_cast<unsigned long>(__g)); }
  937. template<class _Gen>
  938. void
  939. seed(_Gen& __g, false_type);
  940. void
  941. _M_initialize_npows();
  942. static const int __n = (__w + 31) / 32;
  943. typedef __detail::_UInt32Type _UInt32Type;
  944. _UInt32Type _M_x[long_lag][__n];
  945. _RealType _M_npows[__n];
  946. _UInt32Type _M_carry;
  947. int _M_p;
  948. };
  949. typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
  950. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  951. // 508. Bad parameters for ranlux64_base_01.
  952. typedef subtract_with_carry_01<double, 48, 5, 12> ranlux64_base_01;
  953. /**
  954. * Produces random numbers from some base engine by discarding blocks of
  955. * data.
  956. *
  957. * 0 <= @p __r <= @p __p
  958. */
  959. template<class _UniformRandomNumberGenerator, int __p, int __r>
  960. class discard_block
  961. {
  962. // __glibcxx_class_requires(typename base_type::result_type,
  963. // ArithmeticTypeConcept)
  964. public:
  965. /** The type of the underlying generator engine. */
  966. typedef _UniformRandomNumberGenerator base_type;
  967. /** The type of the generated random value. */
  968. typedef typename base_type::result_type result_type;
  969. // parameter values
  970. static const int block_size = __p;
  971. static const int used_block = __r;
  972. /**
  973. * Constructs a default %discard_block engine.
  974. *
  975. * The underlying engine is default constructed as well.
  976. */
  977. discard_block()
  978. : _M_n(0) { }
  979. /**
  980. * Copy constructs a %discard_block engine.
  981. *
  982. * Copies an existing base class random number generator.
  983. * @param rng An existing (base class) engine object.
  984. */
  985. explicit
  986. discard_block(const base_type& __rng)
  987. : _M_b(__rng), _M_n(0) { }
  988. /**
  989. * Seed constructs a %discard_block engine.
  990. *
  991. * Constructs the underlying generator engine seeded with @p __s.
  992. * @param __s A seed value for the base class engine.
  993. */
  994. explicit
  995. discard_block(unsigned long __s)
  996. : _M_b(__s), _M_n(0) { }
  997. /**
  998. * Generator construct a %discard_block engine.
  999. *
  1000. * @param __g A seed generator function.
  1001. */
  1002. template<class _Gen>
  1003. discard_block(_Gen& __g)
  1004. : _M_b(__g), _M_n(0) { }
  1005. /**
  1006. * Reseeds the %discard_block object with the default seed for the
  1007. * underlying base class generator engine.
  1008. */
  1009. void seed()
  1010. {
  1011. _M_b.seed();
  1012. _M_n = 0;
  1013. }
  1014. /**
  1015. * Reseeds the %discard_block object with the given seed generator
  1016. * function.
  1017. * @param __g A seed generator function.
  1018. */
  1019. template<class _Gen>
  1020. void seed(_Gen& __g)
  1021. {
  1022. _M_b.seed(__g);
  1023. _M_n = 0;
  1024. }
  1025. /**
  1026. * Gets a const reference to the underlying generator engine object.
  1027. */
  1028. const base_type&
  1029. base() const
  1030. { return _M_b; }
  1031. /**
  1032. * Gets the minimum value in the generated random number range.
  1033. */
  1034. result_type
  1035. min() const
  1036. { return _M_b.min(); }
  1037. /**
  1038. * Gets the maximum value in the generated random number range.
  1039. */
  1040. result_type
  1041. max() const
  1042. { return _M_b.max(); }
  1043. /**
  1044. * Gets the next value in the generated random number sequence.
  1045. */
  1046. result_type
  1047. operator()();
  1048. /**
  1049. * Compares two %discard_block random number generator objects of
  1050. * the same type for equality.
  1051. *
  1052. * @param __lhs A %discard_block random number generator object.
  1053. * @param __rhs Another %discard_block random number generator
  1054. * object.
  1055. *
  1056. * @returns true if the two objects are equal, false otherwise.
  1057. */
  1058. friend bool
  1059. operator==(const discard_block& __lhs, const discard_block& __rhs)
  1060. { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
  1061. /**
  1062. * Compares two %discard_block random number generator objects of
  1063. * the same type for inequality.
  1064. *
  1065. * @param __lhs A %discard_block random number generator object.
  1066. * @param __rhs Another %discard_block random number generator
  1067. * object.
  1068. *
  1069. * @returns true if the two objects are not equal, false otherwise.
  1070. */
  1071. friend bool
  1072. operator!=(const discard_block& __lhs, const discard_block& __rhs)
  1073. { return !(__lhs == __rhs); }
  1074. /**
  1075. * Inserts the current state of a %discard_block random number
  1076. * generator engine @p __x into the output stream @p __os.
  1077. *
  1078. * @param __os An output stream.
  1079. * @param __x A %discard_block random number generator engine.
  1080. *
  1081. * @returns The output stream with the state of @p __x inserted or in
  1082. * an error state.
  1083. */
  1084. template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
  1085. typename _CharT, typename _Traits>
  1086. friend std::basic_ostream<_CharT, _Traits>&
  1087. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1088. const discard_block<_UniformRandomNumberGenerator1,
  1089. __p1, __r1>& __x);
  1090. /**
  1091. * Extracts the current state of a % subtract_with_carry random number
  1092. * generator engine @p __x from the input stream @p __is.
  1093. *
  1094. * @param __is An input stream.
  1095. * @param __x A %discard_block random number generator engine.
  1096. *
  1097. * @returns The input stream with the state of @p __x extracted or in
  1098. * an error state.
  1099. */
  1100. template<class _UniformRandomNumberGenerator1, int __p1, int __r1,
  1101. typename _CharT, typename _Traits>
  1102. friend std::basic_istream<_CharT, _Traits>&
  1103. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1104. discard_block<_UniformRandomNumberGenerator1,
  1105. __p1, __r1>& __x);
  1106. private:
  1107. base_type _M_b;
  1108. int _M_n;
  1109. };
  1110. /**
  1111. * James's luxury-level-3 integer adaptation of Luescher's generator.
  1112. */
  1113. typedef discard_block<
  1114. subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
  1115. 223,
  1116. 24
  1117. > ranlux3;
  1118. /**
  1119. * James's luxury-level-4 integer adaptation of Luescher's generator.
  1120. */
  1121. typedef discard_block<
  1122. subtract_with_carry<unsigned long, (1UL << 24), 10, 24>,
  1123. 389,
  1124. 24
  1125. > ranlux4;
  1126. typedef discard_block<
  1127. subtract_with_carry_01<float, 24, 10, 24>,
  1128. 223,
  1129. 24
  1130. > ranlux3_01;
  1131. typedef discard_block<
  1132. subtract_with_carry_01<float, 24, 10, 24>,
  1133. 389,
  1134. 24
  1135. > ranlux4_01;
  1136. /**
  1137. * A random number generator adaptor class that combines two random number
  1138. * generator engines into a single output sequence.
  1139. */
  1140. template<class _UniformRandomNumberGenerator1, int __s1,
  1141. class _UniformRandomNumberGenerator2, int __s2>
  1142. class xor_combine
  1143. {
  1144. // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
  1145. // result_type, ArithmeticTypeConcept)
  1146. // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
  1147. // result_type, ArithmeticTypeConcept)
  1148. public:
  1149. /** The type of the first underlying generator engine. */
  1150. typedef _UniformRandomNumberGenerator1 base1_type;
  1151. /** The type of the second underlying generator engine. */
  1152. typedef _UniformRandomNumberGenerator2 base2_type;
  1153. private:
  1154. typedef typename base1_type::result_type _Result_type1;
  1155. typedef typename base2_type::result_type _Result_type2;
  1156. public:
  1157. /** The type of the generated random value. */
  1158. typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1)
  1159. > sizeof(_Result_type2)),
  1160. _Result_type1, _Result_type2>::__type result_type;
  1161. // parameter values
  1162. static const int shift1 = __s1;
  1163. static const int shift2 = __s2;
  1164. // constructors and member function
  1165. xor_combine()
  1166. : _M_b1(), _M_b2()
  1167. { _M_initialize_max(); }
  1168. xor_combine(const base1_type& __rng1, const base2_type& __rng2)
  1169. : _M_b1(__rng1), _M_b2(__rng2)
  1170. { _M_initialize_max(); }
  1171. xor_combine(unsigned long __s)
  1172. : _M_b1(__s), _M_b2(__s + 1)
  1173. { _M_initialize_max(); }
  1174. template<class _Gen>
  1175. xor_combine(_Gen& __g)
  1176. : _M_b1(__g), _M_b2(__g)
  1177. { _M_initialize_max(); }
  1178. void
  1179. seed()
  1180. {
  1181. _M_b1.seed();
  1182. _M_b2.seed();
  1183. }
  1184. template<class _Gen>
  1185. void
  1186. seed(_Gen& __g)
  1187. {
  1188. _M_b1.seed(__g);
  1189. _M_b2.seed(__g);
  1190. }
  1191. const base1_type&
  1192. base1() const
  1193. { return _M_b1; }
  1194. const base2_type&
  1195. base2() const
  1196. { return _M_b2; }
  1197. result_type
  1198. min() const
  1199. { return 0; }
  1200. result_type
  1201. max() const
  1202. { return _M_max; }
  1203. /**
  1204. * Gets the next random number in the sequence.
  1205. */
  1206. // NB: Not exactly the TR1 formula, per N2079 instead.
  1207. result_type
  1208. operator()()
  1209. {
  1210. return ((result_type(_M_b1() - _M_b1.min()) << shift1)
  1211. ^ (result_type(_M_b2() - _M_b2.min()) << shift2));
  1212. }
  1213. /**
  1214. * Compares two %xor_combine random number generator objects of
  1215. * the same type for equality.
  1216. *
  1217. * @param __lhs A %xor_combine random number generator object.
  1218. * @param __rhs Another %xor_combine random number generator
  1219. * object.
  1220. *
  1221. * @returns true if the two objects are equal, false otherwise.
  1222. */
  1223. friend bool
  1224. operator==(const xor_combine& __lhs, const xor_combine& __rhs)
  1225. {
  1226. return (__lhs.base1() == __rhs.base1())
  1227. && (__lhs.base2() == __rhs.base2());
  1228. }
  1229. /**
  1230. * Compares two %xor_combine random number generator objects of
  1231. * the same type for inequality.
  1232. *
  1233. * @param __lhs A %xor_combine random number generator object.
  1234. * @param __rhs Another %xor_combine random number generator
  1235. * object.
  1236. *
  1237. * @returns true if the two objects are not equal, false otherwise.
  1238. */
  1239. friend bool
  1240. operator!=(const xor_combine& __lhs, const xor_combine& __rhs)
  1241. { return !(__lhs == __rhs); }
  1242. /**
  1243. * Inserts the current state of a %xor_combine random number
  1244. * generator engine @p __x into the output stream @p __os.
  1245. *
  1246. * @param __os An output stream.
  1247. * @param __x A %xor_combine random number generator engine.
  1248. *
  1249. * @returns The output stream with the state of @p __x inserted or in
  1250. * an error state.
  1251. */
  1252. template<class _UniformRandomNumberGenerator11, int __s11,
  1253. class _UniformRandomNumberGenerator21, int __s21,
  1254. typename _CharT, typename _Traits>
  1255. friend std::basic_ostream<_CharT, _Traits>&
  1256. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1257. const xor_combine<_UniformRandomNumberGenerator11, __s11,
  1258. _UniformRandomNumberGenerator21, __s21>& __x);
  1259. /**
  1260. * Extracts the current state of a %xor_combine random number
  1261. * generator engine @p __x from the input stream @p __is.
  1262. *
  1263. * @param __is An input stream.
  1264. * @param __x A %xor_combine random number generator engine.
  1265. *
  1266. * @returns The input stream with the state of @p __x extracted or in
  1267. * an error state.
  1268. */
  1269. template<class _UniformRandomNumberGenerator11, int __s11,
  1270. class _UniformRandomNumberGenerator21, int __s21,
  1271. typename _CharT, typename _Traits>
  1272. friend std::basic_istream<_CharT, _Traits>&
  1273. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1274. xor_combine<_UniformRandomNumberGenerator11, __s11,
  1275. _UniformRandomNumberGenerator21, __s21>& __x);
  1276. private:
  1277. void
  1278. _M_initialize_max();
  1279. result_type
  1280. _M_initialize_max_aux(result_type, result_type, int);
  1281. base1_type _M_b1;
  1282. base2_type _M_b2;
  1283. result_type _M_max;
  1284. };
  1285. /**
  1286. * A standard interface to a platform-specific non-deterministic
  1287. * random number generator (if any are available).
  1288. */
  1289. class random_device
  1290. {
  1291. public:
  1292. // types
  1293. typedef unsigned int result_type;
  1294. // constructors, destructors and member functions
  1295. #ifdef _GLIBCXX_USE_RANDOM_TR1
  1296. explicit
  1297. random_device(const std::string& __token = "/dev/urandom")
  1298. {
  1299. if ((__token != "/dev/urandom" && __token != "/dev/random")
  1300. || !(_M_file = std::fopen(__token.c_str(), "rb")))
  1301. std::__throw_runtime_error(__N("random_device::"
  1302. "random_device(const std::string&)"));
  1303. }
  1304. ~random_device()
  1305. { std::fclose(_M_file); }
  1306. #else
  1307. explicit
  1308. random_device(const std::string& __token = "mt19937")
  1309. : _M_mt(_M_strtoul(__token)) { }
  1310. private:
  1311. static unsigned long
  1312. _M_strtoul(const std::string& __str)
  1313. {
  1314. unsigned long __ret = 5489UL;
  1315. if (__str != "mt19937")
  1316. {
  1317. const char* __nptr = __str.c_str();
  1318. char* __endptr;
  1319. __ret = std::strtoul(__nptr, &__endptr, 0);
  1320. if (*__nptr == '\0' || *__endptr != '\0')
  1321. std::__throw_runtime_error(__N("random_device::_M_strtoul"
  1322. "(const std::string&)"));
  1323. }
  1324. return __ret;
  1325. }
  1326. public:
  1327. #endif
  1328. result_type
  1329. min() const
  1330. { return std::numeric_limits<result_type>::min(); }
  1331. result_type
  1332. max() const
  1333. { return std::numeric_limits<result_type>::max(); }
  1334. double
  1335. entropy() const
  1336. { return 0.0; }
  1337. result_type
  1338. operator()()
  1339. {
  1340. #ifdef _GLIBCXX_USE_RANDOM_TR1
  1341. result_type __ret;
  1342. std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
  1343. 1, _M_file);
  1344. return __ret;
  1345. #else
  1346. return _M_mt();
  1347. #endif
  1348. }
  1349. private:
  1350. random_device(const random_device&);
  1351. void operator=(const random_device&);
  1352. #ifdef _GLIBCXX_USE_RANDOM_TR1
  1353. FILE* _M_file;
  1354. #else
  1355. mt19937 _M_mt;
  1356. #endif
  1357. };
  1358. /* @} */ // group tr1_random_generators
  1359. /**
  1360. * @addtogroup tr1_random_distributions Random Number Distributions
  1361. * @ingroup tr1_random
  1362. * @{
  1363. */
  1364. /**
  1365. * @addtogroup tr1_random_distributions_discrete Discrete Distributions
  1366. * @ingroup tr1_random_distributions
  1367. * @{
  1368. */
  1369. /**
  1370. * @brief Uniform discrete distribution for random numbers.
  1371. * A discrete random distribution on the range @f$[min, max]@f$ with equal
  1372. * probability throughout the range.
  1373. */
  1374. template<typename _IntType = int>
  1375. class uniform_int
  1376. {
  1377. __glibcxx_class_requires(_IntType, _IntegerConcept)
  1378. public:
  1379. /** The type of the parameters of the distribution. */
  1380. typedef _IntType input_type;
  1381. /** The type of the range of the distribution. */
  1382. typedef _IntType result_type;
  1383. public:
  1384. /**
  1385. * Constructs a uniform distribution object.
  1386. */
  1387. explicit
  1388. uniform_int(_IntType __min = 0, _IntType __max = 9)
  1389. : _M_min(__min), _M_max(__max)
  1390. {
  1391. _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
  1392. }
  1393. /**
  1394. * Gets the inclusive lower bound of the distribution range.
  1395. */
  1396. result_type
  1397. min() const
  1398. { return _M_min; }
  1399. /**
  1400. * Gets the inclusive upper bound of the distribution range.
  1401. */
  1402. result_type
  1403. max() const
  1404. { return _M_max; }
  1405. /**
  1406. * Resets the distribution state.
  1407. *
  1408. * Does nothing for the uniform integer distribution.
  1409. */
  1410. void
  1411. reset() { }
  1412. /**
  1413. * Gets a uniformly distributed random number in the range
  1414. * @f$(min, max)@f$.
  1415. */
  1416. template<typename _UniformRandomNumberGenerator>
  1417. result_type
  1418. operator()(_UniformRandomNumberGenerator& __urng)
  1419. {
  1420. typedef typename _UniformRandomNumberGenerator::result_type
  1421. _UResult_type;
  1422. return _M_call(__urng, _M_min, _M_max,
  1423. typename is_integral<_UResult_type>::type());
  1424. }
  1425. /**
  1426. * Gets a uniform random number in the range @f$[0, n)@f$.
  1427. *
  1428. * This function is aimed at use with std::random_shuffle.
  1429. */
  1430. template<typename _UniformRandomNumberGenerator>
  1431. result_type
  1432. operator()(_UniformRandomNumberGenerator& __urng, result_type __n)
  1433. {
  1434. typedef typename _UniformRandomNumberGenerator::result_type
  1435. _UResult_type;
  1436. return _M_call(__urng, 0, __n - 1,
  1437. typename is_integral<_UResult_type>::type());
  1438. }
  1439. /**
  1440. * Inserts a %uniform_int random number distribution @p __x into the
  1441. * output stream @p os.
  1442. *
  1443. * @param __os An output stream.
  1444. * @param __x A %uniform_int random number distribution.
  1445. *
  1446. * @returns The output stream with the state of @p __x inserted or in
  1447. * an error state.
  1448. */
  1449. template<typename _IntType1, typename _CharT, typename _Traits>
  1450. friend std::basic_ostream<_CharT, _Traits>&
  1451. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1452. const uniform_int<_IntType1>& __x);
  1453. /**
  1454. * Extracts a %uniform_int random number distribution
  1455. * @p __x from the input stream @p __is.
  1456. *
  1457. * @param __is An input stream.
  1458. * @param __x A %uniform_int random number generator engine.
  1459. *
  1460. * @returns The input stream with @p __x extracted or in an error state.
  1461. */
  1462. template<typename _IntType1, typename _CharT, typename _Traits>
  1463. friend std::basic_istream<_CharT, _Traits>&
  1464. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1465. uniform_int<_IntType1>& __x);
  1466. private:
  1467. template<typename _UniformRandomNumberGenerator>
  1468. result_type
  1469. _M_call(_UniformRandomNumberGenerator& __urng,
  1470. result_type __min, result_type __max, true_type);
  1471. template<typename _UniformRandomNumberGenerator>
  1472. result_type
  1473. _M_call(_UniformRandomNumberGenerator& __urng,
  1474. result_type __min, result_type __max, false_type)
  1475. {
  1476. return result_type((__urng() - __urng.min())
  1477. / (__urng.max() - __urng.min())
  1478. * (__max - __min + 1)) + __min;
  1479. }
  1480. _IntType _M_min;
  1481. _IntType _M_max;
  1482. };
  1483. /**
  1484. * @brief A Bernoulli random number distribution.
  1485. *
  1486. * Generates a sequence of true and false values with likelihood @f$ p @f$
  1487. * that true will come up and @f$ (1 - p) @f$ that false will appear.
  1488. */
  1489. class bernoulli_distribution
  1490. {
  1491. public:
  1492. typedef int input_type;
  1493. typedef bool result_type;
  1494. public:
  1495. /**
  1496. * Constructs a Bernoulli distribution with likelihood @p p.
  1497. *
  1498. * @param __p [IN] The likelihood of a true result being returned. Must
  1499. * be in the interval @f$ [0, 1] @f$.
  1500. */
  1501. explicit
  1502. bernoulli_distribution(double __p = 0.5)
  1503. : _M_p(__p)
  1504. {
  1505. _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
  1506. }
  1507. /**
  1508. * Gets the @p p parameter of the distribution.
  1509. */
  1510. double
  1511. p() const
  1512. { return _M_p; }
  1513. /**
  1514. * Resets the distribution state.
  1515. *
  1516. * Does nothing for a Bernoulli distribution.
  1517. */
  1518. void
  1519. reset() { }
  1520. /**
  1521. * Gets the next value in the Bernoullian sequence.
  1522. */
  1523. template<class _UniformRandomNumberGenerator>
  1524. result_type
  1525. operator()(_UniformRandomNumberGenerator& __urng)
  1526. {
  1527. if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min()))
  1528. return true;
  1529. return false;
  1530. }
  1531. /**
  1532. * Inserts a %bernoulli_distribution random number distribution
  1533. * @p __x into the output stream @p __os.
  1534. *
  1535. * @param __os An output stream.
  1536. * @param __x A %bernoulli_distribution random number distribution.
  1537. *
  1538. * @returns The output stream with the state of @p __x inserted or in
  1539. * an error state.
  1540. */
  1541. template<typename _CharT, typename _Traits>
  1542. friend std::basic_ostream<_CharT, _Traits>&
  1543. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1544. const bernoulli_distribution& __x);
  1545. /**
  1546. * Extracts a %bernoulli_distribution random number distribution
  1547. * @p __x from the input stream @p __is.
  1548. *
  1549. * @param __is An input stream.
  1550. * @param __x A %bernoulli_distribution random number generator engine.
  1551. *
  1552. * @returns The input stream with @p __x extracted or in an error state.
  1553. */
  1554. template<typename _CharT, typename _Traits>
  1555. friend std::basic_istream<_CharT, _Traits>&
  1556. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1557. bernoulli_distribution& __x)
  1558. { return __is >> __x._M_p; }
  1559. private:
  1560. double _M_p;
  1561. };
  1562. /**
  1563. * @brief A discrete geometric random number distribution.
  1564. *
  1565. * The formula for the geometric probability mass function is
  1566. * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
  1567. * distribution.
  1568. */
  1569. template<typename _IntType = int, typename _RealType = double>
  1570. class geometric_distribution
  1571. {
  1572. public:
  1573. // types
  1574. typedef _RealType input_type;
  1575. typedef _IntType result_type;
  1576. // constructors and member function
  1577. explicit
  1578. geometric_distribution(const _RealType& __p = _RealType(0.5))
  1579. : _M_p(__p)
  1580. {
  1581. _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
  1582. _M_initialize();
  1583. }
  1584. /**
  1585. * Gets the distribution parameter @p p.
  1586. */
  1587. _RealType
  1588. p() const
  1589. { return _M_p; }
  1590. void
  1591. reset() { }
  1592. template<class _UniformRandomNumberGenerator>
  1593. result_type
  1594. operator()(_UniformRandomNumberGenerator& __urng);
  1595. /**
  1596. * Inserts a %geometric_distribution random number distribution
  1597. * @p __x into the output stream @p __os.
  1598. *
  1599. * @param __os An output stream.
  1600. * @param __x A %geometric_distribution random number distribution.
  1601. *
  1602. * @returns The output stream with the state of @p __x inserted or in
  1603. * an error state.
  1604. */
  1605. template<typename _IntType1, typename _RealType1,
  1606. typename _CharT, typename _Traits>
  1607. friend std::basic_ostream<_CharT, _Traits>&
  1608. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1609. const geometric_distribution<_IntType1, _RealType1>& __x);
  1610. /**
  1611. * Extracts a %geometric_distribution random number distribution
  1612. * @p __x from the input stream @p __is.
  1613. *
  1614. * @param __is An input stream.
  1615. * @param __x A %geometric_distribution random number generator engine.
  1616. *
  1617. * @returns The input stream with @p __x extracted or in an error state.
  1618. */
  1619. template<typename _CharT, typename _Traits>
  1620. friend std::basic_istream<_CharT, _Traits>&
  1621. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1622. geometric_distribution& __x)
  1623. {
  1624. __is >> __x._M_p;
  1625. __x._M_initialize();
  1626. return __is;
  1627. }
  1628. private:
  1629. void
  1630. _M_initialize()
  1631. { _M_log_p = std::log(_M_p); }
  1632. _RealType _M_p;
  1633. _RealType _M_log_p;
  1634. };
  1635. template<typename _RealType>
  1636. class normal_distribution;
  1637. /**
  1638. * @brief A discrete Poisson random number distribution.
  1639. *
  1640. * The formula for the Poisson probability mass function is
  1641. * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
  1642. * parameter of the distribution.
  1643. */
  1644. template<typename _IntType = int, typename _RealType = double>
  1645. class poisson_distribution
  1646. {
  1647. public:
  1648. // types
  1649. typedef _RealType input_type;
  1650. typedef _IntType result_type;
  1651. // constructors and member function
  1652. explicit
  1653. poisson_distribution(const _RealType& __mean = _RealType(1))
  1654. : _M_mean(__mean), _M_nd()
  1655. {
  1656. _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
  1657. _M_initialize();
  1658. }
  1659. /**
  1660. * Gets the distribution parameter @p mean.
  1661. */
  1662. _RealType
  1663. mean() const
  1664. { return _M_mean; }
  1665. void
  1666. reset()
  1667. { _M_nd.reset(); }
  1668. template<class _UniformRandomNumberGenerator>
  1669. result_type
  1670. operator()(_UniformRandomNumberGenerator& __urng);
  1671. /**
  1672. * Inserts a %poisson_distribution random number distribution
  1673. * @p __x into the output stream @p __os.
  1674. *
  1675. * @param __os An output stream.
  1676. * @param __x A %poisson_distribution random number distribution.
  1677. *
  1678. * @returns The output stream with the state of @p __x inserted or in
  1679. * an error state.
  1680. */
  1681. template<typename _IntType1, typename _RealType1,
  1682. typename _CharT, typename _Traits>
  1683. friend std::basic_ostream<_CharT, _Traits>&
  1684. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1685. const poisson_distribution<_IntType1, _RealType1>& __x);
  1686. /**
  1687. * Extracts a %poisson_distribution random number distribution
  1688. * @p __x from the input stream @p __is.
  1689. *
  1690. * @param __is An input stream.
  1691. * @param __x A %poisson_distribution random number generator engine.
  1692. *
  1693. * @returns The input stream with @p __x extracted or in an error state.
  1694. */
  1695. template<typename _IntType1, typename _RealType1,
  1696. typename _CharT, typename _Traits>
  1697. friend std::basic_istream<_CharT, _Traits>&
  1698. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1699. poisson_distribution<_IntType1, _RealType1>& __x);
  1700. private:
  1701. void
  1702. _M_initialize();
  1703. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  1704. normal_distribution<_RealType> _M_nd;
  1705. _RealType _M_mean;
  1706. // Hosts either log(mean) or the threshold of the simple method.
  1707. _RealType _M_lm_thr;
  1708. #if _GLIBCXX_USE_C99_MATH_TR1
  1709. _RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
  1710. #endif
  1711. };
  1712. /**
  1713. * @brief A discrete binomial random number distribution.
  1714. *
  1715. * The formula for the binomial probability mass function is
  1716. * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
  1717. * and @f$ p @f$ are the parameters of the distribution.
  1718. */
  1719. template<typename _IntType = int, typename _RealType = double>
  1720. class binomial_distribution
  1721. {
  1722. public:
  1723. // types
  1724. typedef _RealType input_type;
  1725. typedef _IntType result_type;
  1726. // constructors and member function
  1727. explicit
  1728. binomial_distribution(_IntType __t = 1,
  1729. const _RealType& __p = _RealType(0.5))
  1730. : _M_t(__t), _M_p(__p), _M_nd()
  1731. {
  1732. _GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
  1733. _M_initialize();
  1734. }
  1735. /**
  1736. * Gets the distribution @p t parameter.
  1737. */
  1738. _IntType
  1739. t() const
  1740. { return _M_t; }
  1741. /**
  1742. * Gets the distribution @p p parameter.
  1743. */
  1744. _RealType
  1745. p() const
  1746. { return _M_p; }
  1747. void
  1748. reset()
  1749. { _M_nd.reset(); }
  1750. template<class _UniformRandomNumberGenerator>
  1751. result_type
  1752. operator()(_UniformRandomNumberGenerator& __urng);
  1753. /**
  1754. * Inserts a %binomial_distribution random number distribution
  1755. * @p __x into the output stream @p __os.
  1756. *
  1757. * @param __os An output stream.
  1758. * @param __x A %binomial_distribution random number distribution.
  1759. *
  1760. * @returns The output stream with the state of @p __x inserted or in
  1761. * an error state.
  1762. */
  1763. template<typename _IntType1, typename _RealType1,
  1764. typename _CharT, typename _Traits>
  1765. friend std::basic_ostream<_CharT, _Traits>&
  1766. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1767. const binomial_distribution<_IntType1, _RealType1>& __x);
  1768. /**
  1769. * Extracts a %binomial_distribution random number distribution
  1770. * @p __x from the input stream @p __is.
  1771. *
  1772. * @param __is An input stream.
  1773. * @param __x A %binomial_distribution random number generator engine.
  1774. *
  1775. * @returns The input stream with @p __x extracted or in an error state.
  1776. */
  1777. template<typename _IntType1, typename _RealType1,
  1778. typename _CharT, typename _Traits>
  1779. friend std::basic_istream<_CharT, _Traits>&
  1780. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1781. binomial_distribution<_IntType1, _RealType1>& __x);
  1782. private:
  1783. void
  1784. _M_initialize();
  1785. template<class _UniformRandomNumberGenerator>
  1786. result_type
  1787. _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
  1788. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  1789. normal_distribution<_RealType> _M_nd;
  1790. _RealType _M_q;
  1791. #if _GLIBCXX_USE_C99_MATH_TR1
  1792. _RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
  1793. _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
  1794. #endif
  1795. _RealType _M_p;
  1796. _IntType _M_t;
  1797. bool _M_easy;
  1798. };
  1799. /* @} */ // group tr1_random_distributions_discrete
  1800. /**
  1801. * @addtogroup tr1_random_distributions_continuous Continuous Distributions
  1802. * @ingroup tr1_random_distributions
  1803. * @{
  1804. */
  1805. /**
  1806. * @brief Uniform continuous distribution for random numbers.
  1807. *
  1808. * A continuous random distribution on the range [min, max) with equal
  1809. * probability throughout the range. The URNG should be real-valued and
  1810. * deliver number in the range [0, 1).
  1811. */
  1812. template<typename _RealType = double>
  1813. class uniform_real
  1814. {
  1815. public:
  1816. // types
  1817. typedef _RealType input_type;
  1818. typedef _RealType result_type;
  1819. public:
  1820. /**
  1821. * Constructs a uniform_real object.
  1822. *
  1823. * @param __min [IN] The lower bound of the distribution.
  1824. * @param __max [IN] The upper bound of the distribution.
  1825. */
  1826. explicit
  1827. uniform_real(_RealType __min = _RealType(0),
  1828. _RealType __max = _RealType(1))
  1829. : _M_min(__min), _M_max(__max)
  1830. {
  1831. _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max);
  1832. }
  1833. result_type
  1834. min() const
  1835. { return _M_min; }
  1836. result_type
  1837. max() const
  1838. { return _M_max; }
  1839. void
  1840. reset() { }
  1841. template<class _UniformRandomNumberGenerator>
  1842. result_type
  1843. operator()(_UniformRandomNumberGenerator& __urng)
  1844. { return (__urng() * (_M_max - _M_min)) + _M_min; }
  1845. /**
  1846. * Inserts a %uniform_real random number distribution @p __x into the
  1847. * output stream @p __os.
  1848. *
  1849. * @param __os An output stream.
  1850. * @param __x A %uniform_real random number distribution.
  1851. *
  1852. * @returns The output stream with the state of @p __x inserted or in
  1853. * an error state.
  1854. */
  1855. template<typename _RealType1, typename _CharT, typename _Traits>
  1856. friend std::basic_ostream<_CharT, _Traits>&
  1857. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1858. const uniform_real<_RealType1>& __x);
  1859. /**
  1860. * Extracts a %uniform_real random number distribution
  1861. * @p __x from the input stream @p __is.
  1862. *
  1863. * @param __is An input stream.
  1864. * @param __x A %uniform_real random number generator engine.
  1865. *
  1866. * @returns The input stream with @p __x extracted or in an error state.
  1867. */
  1868. template<typename _RealType1, typename _CharT, typename _Traits>
  1869. friend std::basic_istream<_CharT, _Traits>&
  1870. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1871. uniform_real<_RealType1>& __x);
  1872. private:
  1873. _RealType _M_min;
  1874. _RealType _M_max;
  1875. };
  1876. /**
  1877. * @brief An exponential continuous distribution for random numbers.
  1878. *
  1879. * The formula for the exponential probability mass function is
  1880. * @f$ p(x) = \lambda e^{-\lambda x} @f$.
  1881. *
  1882. * <table border=1 cellpadding=10 cellspacing=0>
  1883. * <caption align=top>Distribution Statistics</caption>
  1884. * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
  1885. * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
  1886. * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
  1887. * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
  1888. * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
  1889. * </table>
  1890. */
  1891. template<typename _RealType = double>
  1892. class exponential_distribution
  1893. {
  1894. public:
  1895. // types
  1896. typedef _RealType input_type;
  1897. typedef _RealType result_type;
  1898. public:
  1899. /**
  1900. * Constructs an exponential distribution with inverse scale parameter
  1901. * @f$ \lambda @f$.
  1902. */
  1903. explicit
  1904. exponential_distribution(const result_type& __lambda = result_type(1))
  1905. : _M_lambda(__lambda)
  1906. {
  1907. _GLIBCXX_DEBUG_ASSERT(_M_lambda > 0);
  1908. }
  1909. /**
  1910. * Gets the inverse scale parameter of the distribution.
  1911. */
  1912. _RealType
  1913. lambda() const
  1914. { return _M_lambda; }
  1915. /**
  1916. * Resets the distribution.
  1917. *
  1918. * Has no effect on exponential distributions.
  1919. */
  1920. void
  1921. reset() { }
  1922. template<class _UniformRandomNumberGenerator>
  1923. result_type
  1924. operator()(_UniformRandomNumberGenerator& __urng)
  1925. { return -std::log(__urng()) / _M_lambda; }
  1926. /**
  1927. * Inserts a %exponential_distribution random number distribution
  1928. * @p __x into the output stream @p __os.
  1929. *
  1930. * @param __os An output stream.
  1931. * @param __x A %exponential_distribution random number distribution.
  1932. *
  1933. * @returns The output stream with the state of @p __x inserted or in
  1934. * an error state.
  1935. */
  1936. template<typename _RealType1, typename _CharT, typename _Traits>
  1937. friend std::basic_ostream<_CharT, _Traits>&
  1938. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1939. const exponential_distribution<_RealType1>& __x);
  1940. /**
  1941. * Extracts a %exponential_distribution random number distribution
  1942. * @p __x from the input stream @p __is.
  1943. *
  1944. * @param __is An input stream.
  1945. * @param __x A %exponential_distribution random number
  1946. * generator engine.
  1947. *
  1948. * @returns The input stream with @p __x extracted or in an error state.
  1949. */
  1950. template<typename _CharT, typename _Traits>
  1951. friend std::basic_istream<_CharT, _Traits>&
  1952. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1953. exponential_distribution& __x)
  1954. { return __is >> __x._M_lambda; }
  1955. private:
  1956. result_type _M_lambda;
  1957. };
  1958. /**
  1959. * @brief A normal continuous distribution for random numbers.
  1960. *
  1961. * The formula for the normal probability mass function is
  1962. * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
  1963. * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
  1964. */
  1965. template<typename _RealType = double>
  1966. class normal_distribution
  1967. {
  1968. public:
  1969. // types
  1970. typedef _RealType input_type;
  1971. typedef _RealType result_type;
  1972. public:
  1973. /**
  1974. * Constructs a normal distribution with parameters @f$ mean @f$ and
  1975. * @f$ \sigma @f$.
  1976. */
  1977. explicit
  1978. normal_distribution(const result_type& __mean = result_type(0),
  1979. const result_type& __sigma = result_type(1))
  1980. : _M_mean(__mean), _M_sigma(__sigma), _M_saved_available(false)
  1981. {
  1982. _GLIBCXX_DEBUG_ASSERT(_M_sigma > 0);
  1983. }
  1984. /**
  1985. * Gets the mean of the distribution.
  1986. */
  1987. _RealType
  1988. mean() const
  1989. { return _M_mean; }
  1990. /**
  1991. * Gets the @f$ \sigma @f$ of the distribution.
  1992. */
  1993. _RealType
  1994. sigma() const
  1995. { return _M_sigma; }
  1996. /**
  1997. * Resets the distribution.
  1998. */
  1999. void
  2000. reset()
  2001. { _M_saved_available = false; }
  2002. template<class _UniformRandomNumberGenerator>
  2003. result_type
  2004. operator()(_UniformRandomNumberGenerator& __urng);
  2005. /**
  2006. * Inserts a %normal_distribution random number distribution
  2007. * @p __x into the output stream @p __os.
  2008. *
  2009. * @param __os An output stream.
  2010. * @param __x A %normal_distribution random number distribution.
  2011. *
  2012. * @returns The output stream with the state of @p __x inserted or in
  2013. * an error state.
  2014. */
  2015. template<typename _RealType1, typename _CharT, typename _Traits>
  2016. friend std::basic_ostream<_CharT, _Traits>&
  2017. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2018. const normal_distribution<_RealType1>& __x);
  2019. /**
  2020. * Extracts a %normal_distribution random number distribution
  2021. * @p __x from the input stream @p __is.
  2022. *
  2023. * @param __is An input stream.
  2024. * @param __x A %normal_distribution random number generator engine.
  2025. *
  2026. * @returns The input stream with @p __x extracted or in an error state.
  2027. */
  2028. template<typename _RealType1, typename _CharT, typename _Traits>
  2029. friend std::basic_istream<_CharT, _Traits>&
  2030. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2031. normal_distribution<_RealType1>& __x);
  2032. private:
  2033. result_type _M_mean;
  2034. result_type _M_sigma;
  2035. result_type _M_saved;
  2036. bool _M_saved_available;
  2037. };
  2038. /**
  2039. * @brief A gamma continuous distribution for random numbers.
  2040. *
  2041. * The formula for the gamma probability mass function is
  2042. * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
  2043. */
  2044. template<typename _RealType = double>
  2045. class gamma_distribution
  2046. {
  2047. public:
  2048. // types
  2049. typedef _RealType input_type;
  2050. typedef _RealType result_type;
  2051. public:
  2052. /**
  2053. * Constructs a gamma distribution with parameters @f$ \alpha @f$.
  2054. */
  2055. explicit
  2056. gamma_distribution(const result_type& __alpha_val = result_type(1))
  2057. : _M_alpha(__alpha_val)
  2058. {
  2059. _GLIBCXX_DEBUG_ASSERT(_M_alpha > 0);
  2060. _M_initialize();
  2061. }
  2062. /**
  2063. * Gets the @f$ \alpha @f$ of the distribution.
  2064. */
  2065. _RealType
  2066. alpha() const
  2067. { return _M_alpha; }
  2068. /**
  2069. * Resets the distribution.
  2070. */
  2071. void
  2072. reset() { }
  2073. template<class _UniformRandomNumberGenerator>
  2074. result_type
  2075. operator()(_UniformRandomNumberGenerator& __urng);
  2076. /**
  2077. * Inserts a %gamma_distribution random number distribution
  2078. * @p __x into the output stream @p __os.
  2079. *
  2080. * @param __os An output stream.
  2081. * @param __x A %gamma_distribution random number distribution.
  2082. *
  2083. * @returns The output stream with the state of @p __x inserted or in
  2084. * an error state.
  2085. */
  2086. template<typename _RealType1, typename _CharT, typename _Traits>
  2087. friend std::basic_ostream<_CharT, _Traits>&
  2088. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2089. const gamma_distribution<_RealType1>& __x);
  2090. /**
  2091. * Extracts a %gamma_distribution random number distribution
  2092. * @p __x from the input stream @p __is.
  2093. *
  2094. * @param __is An input stream.
  2095. * @param __x A %gamma_distribution random number generator engine.
  2096. *
  2097. * @returns The input stream with @p __x extracted or in an error state.
  2098. */
  2099. template<typename _CharT, typename _Traits>
  2100. friend std::basic_istream<_CharT, _Traits>&
  2101. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2102. gamma_distribution& __x)
  2103. {
  2104. __is >> __x._M_alpha;
  2105. __x._M_initialize();
  2106. return __is;
  2107. }
  2108. private:
  2109. void
  2110. _M_initialize();
  2111. result_type _M_alpha;
  2112. // Hosts either lambda of GB or d of modified Vaduva's.
  2113. result_type _M_l_d;
  2114. };
  2115. /* @} */ // group tr1_random_distributions_continuous
  2116. /* @} */ // group tr1_random_distributions
  2117. /* @} */ // group tr1_random
  2118. _GLIBCXX_END_NAMESPACE_VERSION
  2119. }
  2120. }
  2121. #endif // _GLIBCXX_TR1_RANDOM_H