1. // random number generation -*- C++ -*-
  2. // Copyright (C) 2009-2017 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 bits/random.h
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{random}
  24. */
  25. #ifndef _RANDOM_H
  26. #define _RANDOM_H 1
  27. #include <vector>
  28. #include <bits/uniform_int_dist.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. // [26.4] Random number generation
  33. /**
  34. * @defgroup random Random Number Generation
  35. * @ingroup numerics
  36. *
  37. * A facility for generating random numbers on selected distributions.
  38. * @{
  39. */
  40. /**
  41. * @brief A function template for converting the output of a (integral)
  42. * uniform random number generator to a floatng point result in the range
  43. * [0-1).
  44. */
  45. template<typename _RealType, size_t __bits,
  46. typename _UniformRandomNumberGenerator>
  47. _RealType
  48. generate_canonical(_UniformRandomNumberGenerator& __g);
  49. _GLIBCXX_END_NAMESPACE_VERSION
  50. /*
  51. * Implementation-space details.
  52. */
  53. namespace __detail
  54. {
  55. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  56. template<typename _UIntType, size_t __w,
  57. bool = __w < static_cast<size_t>
  58. (std::numeric_limits<_UIntType>::digits)>
  59. struct _Shift
  60. { static const _UIntType __value = 0; };
  61. template<typename _UIntType, size_t __w>
  62. struct _Shift<_UIntType, __w, true>
  63. { static const _UIntType __value = _UIntType(1) << __w; };
  64. template<int __s,
  65. int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
  66. + (__s <= __CHAR_BIT__ * sizeof (long))
  67. + (__s <= __CHAR_BIT__ * sizeof (long long))
  68. /* assume long long no bigger than __int128 */
  69. + (__s <= 128))>
  70. struct _Select_uint_least_t
  71. {
  72. static_assert(__which < 0, /* needs to be dependent */
  73. "sorry, would be too much trouble for a slow result");
  74. };
  75. template<int __s>
  76. struct _Select_uint_least_t<__s, 4>
  77. { typedef unsigned int type; };
  78. template<int __s>
  79. struct _Select_uint_least_t<__s, 3>
  80. { typedef unsigned long type; };
  81. template<int __s>
  82. struct _Select_uint_least_t<__s, 2>
  83. { typedef unsigned long long type; };
  84. #ifdef _GLIBCXX_USE_INT128
  85. template<int __s>
  86. struct _Select_uint_least_t<__s, 1>
  87. { typedef unsigned long __int128 type; };
  88. #endif
  89. // Assume a != 0, a < m, c < m, x < m.
  90. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
  91. bool __big_enough = (!(__m & (__m - 1))
  92. || (_Tp(-1) - __c) / __a >= __m - 1),
  93. bool __schrage_ok = __m % __a < __m / __a>
  94. struct _Mod
  95. {
  96. typedef typename _Select_uint_least_t<std::__lg(__a)
  97. + std::__lg(__m) + 2>::type _Tp2;
  98. static _Tp
  99. __calc(_Tp __x)
  100. { return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m); }
  101. };
  102. // Schrage.
  103. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
  104. struct _Mod<_Tp, __m, __a, __c, false, true>
  105. {
  106. static _Tp
  107. __calc(_Tp __x);
  108. };
  109. // Special cases:
  110. // - for m == 2^n or m == 0, unsigned integer overflow is safe.
  111. // - a * (m - 1) + c fits in _Tp, there is no overflow.
  112. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
  113. struct _Mod<_Tp, __m, __a, __c, true, __s>
  114. {
  115. static _Tp
  116. __calc(_Tp __x)
  117. {
  118. _Tp __res = __a * __x + __c;
  119. if (__m)
  120. __res %= __m;
  121. return __res;
  122. }
  123. };
  124. template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
  125. inline _Tp
  126. __mod(_Tp __x)
  127. { return _Mod<_Tp, __m, __a, __c>::__calc(__x); }
  128. /*
  129. * An adaptor class for converting the output of any Generator into
  130. * the input for a specific Distribution.
  131. */
  132. template<typename _Engine, typename _DInputType>
  133. struct _Adaptor
  134. {
  135. static_assert(std::is_floating_point<_DInputType>::value,
  136. "template argument must be a floating point type");
  137. public:
  138. _Adaptor(_Engine& __g)
  139. : _M_g(__g) { }
  140. _DInputType
  141. min() const
  142. { return _DInputType(0); }
  143. _DInputType
  144. max() const
  145. { return _DInputType(1); }
  146. /*
  147. * Converts a value generated by the adapted random number generator
  148. * into a value in the input domain for the dependent random number
  149. * distribution.
  150. */
  151. _DInputType
  152. operator()()
  153. {
  154. return std::generate_canonical<_DInputType,
  155. std::numeric_limits<_DInputType>::digits,
  156. _Engine>(_M_g);
  157. }
  158. private:
  159. _Engine& _M_g;
  160. };
  161. _GLIBCXX_END_NAMESPACE_VERSION
  162. } // namespace __detail
  163. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  164. /**
  165. * @addtogroup random_generators Random Number Generators
  166. * @ingroup random
  167. *
  168. * These classes define objects which provide random or pseudorandom
  169. * numbers, either from a discrete or a continuous interval. The
  170. * random number generator supplied as a part of this library are
  171. * all uniform random number generators which provide a sequence of
  172. * random number uniformly distributed over their range.
  173. *
  174. * A number generator is a function object with an operator() that
  175. * takes zero arguments and returns a number.
  176. *
  177. * A compliant random number generator must satisfy the following
  178. * requirements. <table border=1 cellpadding=10 cellspacing=0>
  179. * <caption align=top>Random Number Generator Requirements</caption>
  180. * <tr><td>To be documented.</td></tr> </table>
  181. *
  182. * @{
  183. */
  184. /**
  185. * @brief A model of a linear congruential random number generator.
  186. *
  187. * A random number generator that produces pseudorandom numbers via
  188. * linear function:
  189. * @f[
  190. * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
  191. * @f]
  192. *
  193. * The template parameter @p _UIntType must be an unsigned integral type
  194. * large enough to store values up to (__m-1). If the template parameter
  195. * @p __m is 0, the modulus @p __m used is
  196. * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
  197. * parameters @p __a and @p __c must be less than @p __m.
  198. *
  199. * The size of the state is @f$1@f$.
  200. */
  201. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  202. class linear_congruential_engine
  203. {
  204. static_assert(std::is_unsigned<_UIntType>::value,
  205. "result_type must be an unsigned integral type");
  206. static_assert(__m == 0u || (__a < __m && __c < __m),
  207. "template argument substituting __m out of bounds");
  208. public:
  209. /** The type of the generated random value. */
  210. typedef _UIntType result_type;
  211. /** The multiplier. */
  212. static constexpr result_type multiplier = __a;
  213. /** An increment. */
  214. static constexpr result_type increment = __c;
  215. /** The modulus. */
  216. static constexpr result_type modulus = __m;
  217. static constexpr result_type default_seed = 1u;
  218. /**
  219. * @brief Constructs a %linear_congruential_engine random number
  220. * generator engine with seed @p __s. The default seed value
  221. * is 1.
  222. *
  223. * @param __s The initial seed value.
  224. */
  225. explicit
  226. linear_congruential_engine(result_type __s = default_seed)
  227. { seed(__s); }
  228. /**
  229. * @brief Constructs a %linear_congruential_engine random number
  230. * generator engine seeded from the seed sequence @p __q.
  231. *
  232. * @param __q the seed sequence.
  233. */
  234. template<typename _Sseq, typename = typename
  235. std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value>
  236. ::type>
  237. explicit
  238. linear_congruential_engine(_Sseq& __q)
  239. { seed(__q); }
  240. /**
  241. * @brief Reseeds the %linear_congruential_engine random number generator
  242. * engine sequence to the seed @p __s.
  243. *
  244. * @param __s The new seed.
  245. */
  246. void
  247. seed(result_type __s = default_seed);
  248. /**
  249. * @brief Reseeds the %linear_congruential_engine random number generator
  250. * engine
  251. * sequence using values from the seed sequence @p __q.
  252. *
  253. * @param __q the seed sequence.
  254. */
  255. template<typename _Sseq>
  256. typename std::enable_if<std::is_class<_Sseq>::value>::type
  257. seed(_Sseq& __q);
  258. /**
  259. * @brief Gets the smallest possible value in the output range.
  260. *
  261. * The minimum depends on the @p __c parameter: if it is zero, the
  262. * minimum generated must be > 0, otherwise 0 is allowed.
  263. */
  264. static constexpr result_type
  265. min()
  266. { return __c == 0u ? 1u : 0u; }
  267. /**
  268. * @brief Gets the largest possible value in the output range.
  269. */
  270. static constexpr result_type
  271. max()
  272. { return __m - 1u; }
  273. /**
  274. * @brief Discard a sequence of random numbers.
  275. */
  276. void
  277. discard(unsigned long long __z)
  278. {
  279. for (; __z != 0ULL; --__z)
  280. (*this)();
  281. }
  282. /**
  283. * @brief Gets the next random number in the sequence.
  284. */
  285. result_type
  286. operator()()
  287. {
  288. _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
  289. return _M_x;
  290. }
  291. /**
  292. * @brief Compares two linear congruential random number generator
  293. * objects of the same type for equality.
  294. *
  295. * @param __lhs A linear congruential random number generator object.
  296. * @param __rhs Another linear congruential random number generator
  297. * object.
  298. *
  299. * @returns true if the infinite sequences of generated values
  300. * would be equal, false otherwise.
  301. */
  302. friend bool
  303. operator==(const linear_congruential_engine& __lhs,
  304. const linear_congruential_engine& __rhs)
  305. { return __lhs._M_x == __rhs._M_x; }
  306. /**
  307. * @brief Writes the textual representation of the state x(i) of x to
  308. * @p __os.
  309. *
  310. * @param __os The output stream.
  311. * @param __lcr A % linear_congruential_engine random number generator.
  312. * @returns __os.
  313. */
  314. template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  315. _UIntType1 __m1, typename _CharT, typename _Traits>
  316. friend std::basic_ostream<_CharT, _Traits>&
  317. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  318. const std::linear_congruential_engine<_UIntType1,
  319. __a1, __c1, __m1>& __lcr);
  320. /**
  321. * @brief Sets the state of the engine by reading its textual
  322. * representation from @p __is.
  323. *
  324. * The textual representation must have been previously written using
  325. * an output stream whose imbued locale and whose type's template
  326. * specialization arguments _CharT and _Traits were the same as those
  327. * of @p __is.
  328. *
  329. * @param __is The input stream.
  330. * @param __lcr A % linear_congruential_engine random number generator.
  331. * @returns __is.
  332. */
  333. template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  334. _UIntType1 __m1, typename _CharT, typename _Traits>
  335. friend std::basic_istream<_CharT, _Traits>&
  336. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  337. std::linear_congruential_engine<_UIntType1, __a1,
  338. __c1, __m1>& __lcr);
  339. private:
  340. _UIntType _M_x;
  341. };
  342. /**
  343. * @brief Compares two linear congruential random number generator
  344. * objects of the same type for inequality.
  345. *
  346. * @param __lhs A linear congruential random number generator object.
  347. * @param __rhs Another linear congruential random number generator
  348. * object.
  349. *
  350. * @returns true if the infinite sequences of generated values
  351. * would be different, false otherwise.
  352. */
  353. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  354. inline bool
  355. operator!=(const std::linear_congruential_engine<_UIntType, __a,
  356. __c, __m>& __lhs,
  357. const std::linear_congruential_engine<_UIntType, __a,
  358. __c, __m>& __rhs)
  359. { return !(__lhs == __rhs); }
  360. /**
  361. * A generalized feedback shift register discrete random number generator.
  362. *
  363. * This algorithm avoids multiplication and division and is designed to be
  364. * friendly to a pipelined architecture. If the parameters are chosen
  365. * correctly, this generator will produce numbers with a very long period and
  366. * fairly good apparent entropy, although still not cryptographically strong.
  367. *
  368. * The best way to use this generator is with the predefined mt19937 class.
  369. *
  370. * This algorithm was originally invented by Makoto Matsumoto and
  371. * Takuji Nishimura.
  372. *
  373. * @tparam __w Word size, the number of bits in each element of
  374. * the state vector.
  375. * @tparam __n The degree of recursion.
  376. * @tparam __m The period parameter.
  377. * @tparam __r The separation point bit index.
  378. * @tparam __a The last row of the twist matrix.
  379. * @tparam __u The first right-shift tempering matrix parameter.
  380. * @tparam __d The first right-shift tempering matrix mask.
  381. * @tparam __s The first left-shift tempering matrix parameter.
  382. * @tparam __b The first left-shift tempering matrix mask.
  383. * @tparam __t The second left-shift tempering matrix parameter.
  384. * @tparam __c The second left-shift tempering matrix mask.
  385. * @tparam __l The second right-shift tempering matrix parameter.
  386. * @tparam __f Initialization multiplier.
  387. */
  388. template<typename _UIntType, size_t __w,
  389. size_t __n, size_t __m, size_t __r,
  390. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  391. _UIntType __b, size_t __t,
  392. _UIntType __c, size_t __l, _UIntType __f>
  393. class mersenne_twister_engine
  394. {
  395. static_assert(std::is_unsigned<_UIntType>::value,
  396. "result_type must be an unsigned integral type");
  397. static_assert(1u <= __m && __m <= __n,
  398. "template argument substituting __m out of bounds");
  399. static_assert(__r <= __w, "template argument substituting "
  400. "__r out of bound");
  401. static_assert(__u <= __w, "template argument substituting "
  402. "__u out of bound");
  403. static_assert(__s <= __w, "template argument substituting "
  404. "__s out of bound");
  405. static_assert(__t <= __w, "template argument substituting "
  406. "__t out of bound");
  407. static_assert(__l <= __w, "template argument substituting "
  408. "__l out of bound");
  409. static_assert(__w <= std::numeric_limits<_UIntType>::digits,
  410. "template argument substituting __w out of bound");
  411. static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  412. "template argument substituting __a out of bound");
  413. static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  414. "template argument substituting __b out of bound");
  415. static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  416. "template argument substituting __c out of bound");
  417. static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  418. "template argument substituting __d out of bound");
  419. static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  420. "template argument substituting __f out of bound");
  421. public:
  422. /** The type of the generated random value. */
  423. typedef _UIntType result_type;
  424. // parameter values
  425. static constexpr size_t word_size = __w;
  426. static constexpr size_t state_size = __n;
  427. static constexpr size_t shift_size = __m;
  428. static constexpr size_t mask_bits = __r;
  429. static constexpr result_type xor_mask = __a;
  430. static constexpr size_t tempering_u = __u;
  431. static constexpr result_type tempering_d = __d;
  432. static constexpr size_t tempering_s = __s;
  433. static constexpr result_type tempering_b = __b;
  434. static constexpr size_t tempering_t = __t;
  435. static constexpr result_type tempering_c = __c;
  436. static constexpr size_t tempering_l = __l;
  437. static constexpr result_type initialization_multiplier = __f;
  438. static constexpr result_type default_seed = 5489u;
  439. // constructors and member function
  440. explicit
  441. mersenne_twister_engine(result_type __sd = default_seed)
  442. { seed(__sd); }
  443. /**
  444. * @brief Constructs a %mersenne_twister_engine random number generator
  445. * engine seeded from the seed sequence @p __q.
  446. *
  447. * @param __q the seed sequence.
  448. */
  449. template<typename _Sseq, typename = typename
  450. std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value>
  451. ::type>
  452. explicit
  453. mersenne_twister_engine(_Sseq& __q)
  454. { seed(__q); }
  455. void
  456. seed(result_type __sd = default_seed);
  457. template<typename _Sseq>
  458. typename std::enable_if<std::is_class<_Sseq>::value>::type
  459. seed(_Sseq& __q);
  460. /**
  461. * @brief Gets the smallest possible value in the output range.
  462. */
  463. static constexpr result_type
  464. min()
  465. { return 0; };
  466. /**
  467. * @brief Gets the largest possible value in the output range.
  468. */
  469. static constexpr result_type
  470. max()
  471. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  472. /**
  473. * @brief Discard a sequence of random numbers.
  474. */
  475. void
  476. discard(unsigned long long __z);
  477. result_type
  478. operator()();
  479. /**
  480. * @brief Compares two % mersenne_twister_engine random number generator
  481. * objects of the same type for equality.
  482. *
  483. * @param __lhs A % mersenne_twister_engine random number generator
  484. * object.
  485. * @param __rhs Another % mersenne_twister_engine random number
  486. * generator object.
  487. *
  488. * @returns true if the infinite sequences of generated values
  489. * would be equal, false otherwise.
  490. */
  491. friend bool
  492. operator==(const mersenne_twister_engine& __lhs,
  493. const mersenne_twister_engine& __rhs)
  494. { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
  495. && __lhs._M_p == __rhs._M_p); }
  496. /**
  497. * @brief Inserts the current state of a % mersenne_twister_engine
  498. * random number generator engine @p __x into the output stream
  499. * @p __os.
  500. *
  501. * @param __os An output stream.
  502. * @param __x A % mersenne_twister_engine random number generator
  503. * engine.
  504. *
  505. * @returns The output stream with the state of @p __x inserted or in
  506. * an error state.
  507. */
  508. template<typename _UIntType1,
  509. size_t __w1, size_t __n1,
  510. size_t __m1, size_t __r1,
  511. _UIntType1 __a1, size_t __u1,
  512. _UIntType1 __d1, size_t __s1,
  513. _UIntType1 __b1, size_t __t1,
  514. _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
  515. typename _CharT, typename _Traits>
  516. friend std::basic_ostream<_CharT, _Traits>&
  517. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  518. const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
  519. __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
  520. __l1, __f1>& __x);
  521. /**
  522. * @brief Extracts the current state of a % mersenne_twister_engine
  523. * random number generator engine @p __x from the input stream
  524. * @p __is.
  525. *
  526. * @param __is An input stream.
  527. * @param __x A % mersenne_twister_engine random number generator
  528. * engine.
  529. *
  530. * @returns The input stream with the state of @p __x extracted or in
  531. * an error state.
  532. */
  533. template<typename _UIntType1,
  534. size_t __w1, size_t __n1,
  535. size_t __m1, size_t __r1,
  536. _UIntType1 __a1, size_t __u1,
  537. _UIntType1 __d1, size_t __s1,
  538. _UIntType1 __b1, size_t __t1,
  539. _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
  540. typename _CharT, typename _Traits>
  541. friend std::basic_istream<_CharT, _Traits>&
  542. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  543. std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
  544. __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
  545. __l1, __f1>& __x);
  546. private:
  547. void _M_gen_rand();
  548. _UIntType _M_x[state_size];
  549. size_t _M_p;
  550. };
  551. /**
  552. * @brief Compares two % mersenne_twister_engine random number generator
  553. * objects of the same type for inequality.
  554. *
  555. * @param __lhs A % mersenne_twister_engine random number generator
  556. * object.
  557. * @param __rhs Another % mersenne_twister_engine random number
  558. * generator object.
  559. *
  560. * @returns true if the infinite sequences of generated values
  561. * would be different, false otherwise.
  562. */
  563. template<typename _UIntType, size_t __w,
  564. size_t __n, size_t __m, size_t __r,
  565. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  566. _UIntType __b, size_t __t,
  567. _UIntType __c, size_t __l, _UIntType __f>
  568. inline bool
  569. operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
  570. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
  571. const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
  572. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
  573. { return !(__lhs == __rhs); }
  574. /**
  575. * @brief The Marsaglia-Zaman generator.
  576. *
  577. * This is a model of a Generalized Fibonacci discrete random number
  578. * generator, sometimes referred to as the SWC generator.
  579. *
  580. * A discrete random number generator that produces pseudorandom
  581. * numbers using:
  582. * @f[
  583. * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
  584. * @f]
  585. *
  586. * The size of the state is @f$r@f$
  587. * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
  588. */
  589. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  590. class subtract_with_carry_engine
  591. {
  592. static_assert(std::is_unsigned<_UIntType>::value,
  593. "result_type must be an unsigned integral type");
  594. static_assert(0u < __s && __s < __r,
  595. "0 < s < r");
  596. static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
  597. "template argument substituting __w out of bounds");
  598. public:
  599. /** The type of the generated random value. */
  600. typedef _UIntType result_type;
  601. // parameter values
  602. static constexpr size_t word_size = __w;
  603. static constexpr size_t short_lag = __s;
  604. static constexpr size_t long_lag = __r;
  605. static constexpr result_type default_seed = 19780503u;
  606. /**
  607. * @brief Constructs an explicitly seeded % subtract_with_carry_engine
  608. * random number generator.
  609. */
  610. explicit
  611. subtract_with_carry_engine(result_type __sd = default_seed)
  612. { seed(__sd); }
  613. /**
  614. * @brief Constructs a %subtract_with_carry_engine random number engine
  615. * seeded from the seed sequence @p __q.
  616. *
  617. * @param __q the seed sequence.
  618. */
  619. template<typename _Sseq, typename = typename
  620. std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value>
  621. ::type>
  622. explicit
  623. subtract_with_carry_engine(_Sseq& __q)
  624. { seed(__q); }
  625. /**
  626. * @brief Seeds the initial state @f$x_0@f$ of the random number
  627. * generator.
  628. *
  629. * N1688[4.19] modifies this as follows. If @p __value == 0,
  630. * sets value to 19780503. In any case, with a linear
  631. * congruential generator lcg(i) having parameters @f$ m_{lcg} =
  632. * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
  633. * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
  634. * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
  635. * set carry to 1, otherwise sets carry to 0.
  636. */
  637. void
  638. seed(result_type __sd = default_seed);
  639. /**
  640. * @brief Seeds the initial state @f$x_0@f$ of the
  641. * % subtract_with_carry_engine random number generator.
  642. */
  643. template<typename _Sseq>
  644. typename std::enable_if<std::is_class<_Sseq>::value>::type
  645. seed(_Sseq& __q);
  646. /**
  647. * @brief Gets the inclusive minimum value of the range of random
  648. * integers returned by this generator.
  649. */
  650. static constexpr result_type
  651. min()
  652. { return 0; }
  653. /**
  654. * @brief Gets the inclusive maximum value of the range of random
  655. * integers returned by this generator.
  656. */
  657. static constexpr result_type
  658. max()
  659. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  660. /**
  661. * @brief Discard a sequence of random numbers.
  662. */
  663. void
  664. discard(unsigned long long __z)
  665. {
  666. for (; __z != 0ULL; --__z)
  667. (*this)();
  668. }
  669. /**
  670. * @brief Gets the next random number in the sequence.
  671. */
  672. result_type
  673. operator()();
  674. /**
  675. * @brief Compares two % subtract_with_carry_engine random number
  676. * generator objects of the same type for equality.
  677. *
  678. * @param __lhs A % subtract_with_carry_engine random number generator
  679. * object.
  680. * @param __rhs Another % subtract_with_carry_engine random number
  681. * generator object.
  682. *
  683. * @returns true if the infinite sequences of generated values
  684. * would be equal, false otherwise.
  685. */
  686. friend bool
  687. operator==(const subtract_with_carry_engine& __lhs,
  688. const subtract_with_carry_engine& __rhs)
  689. { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
  690. && __lhs._M_carry == __rhs._M_carry
  691. && __lhs._M_p == __rhs._M_p); }
  692. /**
  693. * @brief Inserts the current state of a % subtract_with_carry_engine
  694. * random number generator engine @p __x into the output stream
  695. * @p __os.
  696. *
  697. * @param __os An output stream.
  698. * @param __x A % subtract_with_carry_engine random number generator
  699. * engine.
  700. *
  701. * @returns The output stream with the state of @p __x inserted or in
  702. * an error state.
  703. */
  704. template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
  705. typename _CharT, typename _Traits>
  706. friend std::basic_ostream<_CharT, _Traits>&
  707. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  708. const std::subtract_with_carry_engine<_UIntType1, __w1,
  709. __s1, __r1>& __x);
  710. /**
  711. * @brief Extracts the current state of a % subtract_with_carry_engine
  712. * random number generator engine @p __x from the input stream
  713. * @p __is.
  714. *
  715. * @param __is An input stream.
  716. * @param __x A % subtract_with_carry_engine random number generator
  717. * engine.
  718. *
  719. * @returns The input stream with the state of @p __x extracted or in
  720. * an error state.
  721. */
  722. template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
  723. typename _CharT, typename _Traits>
  724. friend std::basic_istream<_CharT, _Traits>&
  725. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  726. std::subtract_with_carry_engine<_UIntType1, __w1,
  727. __s1, __r1>& __x);
  728. private:
  729. /// The state of the generator. This is a ring buffer.
  730. _UIntType _M_x[long_lag];
  731. _UIntType _M_carry; ///< The carry
  732. size_t _M_p; ///< Current index of x(i - r).
  733. };
  734. /**
  735. * @brief Compares two % subtract_with_carry_engine random number
  736. * generator objects of the same type for inequality.
  737. *
  738. * @param __lhs A % subtract_with_carry_engine random number generator
  739. * object.
  740. * @param __rhs Another % subtract_with_carry_engine random number
  741. * generator object.
  742. *
  743. * @returns true if the infinite sequences of generated values
  744. * would be different, false otherwise.
  745. */
  746. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  747. inline bool
  748. operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
  749. __s, __r>& __lhs,
  750. const std::subtract_with_carry_engine<_UIntType, __w,
  751. __s, __r>& __rhs)
  752. { return !(__lhs == __rhs); }
  753. /**
  754. * Produces random numbers from some base engine by discarding blocks of
  755. * data.
  756. *
  757. * 0 <= @p __r <= @p __p
  758. */
  759. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  760. class discard_block_engine
  761. {
  762. static_assert(1 <= __r && __r <= __p,
  763. "template argument substituting __r out of bounds");
  764. public:
  765. /** The type of the generated random value. */
  766. typedef typename _RandomNumberEngine::result_type result_type;
  767. // parameter values
  768. static constexpr size_t block_size = __p;
  769. static constexpr size_t used_block = __r;
  770. /**
  771. * @brief Constructs a default %discard_block_engine engine.
  772. *
  773. * The underlying engine is default constructed as well.
  774. */
  775. discard_block_engine()
  776. : _M_b(), _M_n(0) { }
  777. /**
  778. * @brief Copy constructs a %discard_block_engine engine.
  779. *
  780. * Copies an existing base class random number generator.
  781. * @param __rng An existing (base class) engine object.
  782. */
  783. explicit
  784. discard_block_engine(const _RandomNumberEngine& __rng)
  785. : _M_b(__rng), _M_n(0) { }
  786. /**
  787. * @brief Move constructs a %discard_block_engine engine.
  788. *
  789. * Copies an existing base class random number generator.
  790. * @param __rng An existing (base class) engine object.
  791. */
  792. explicit
  793. discard_block_engine(_RandomNumberEngine&& __rng)
  794. : _M_b(std::move(__rng)), _M_n(0) { }
  795. /**
  796. * @brief Seed constructs a %discard_block_engine engine.
  797. *
  798. * Constructs the underlying generator engine seeded with @p __s.
  799. * @param __s A seed value for the base class engine.
  800. */
  801. explicit
  802. discard_block_engine(result_type __s)
  803. : _M_b(__s), _M_n(0) { }
  804. /**
  805. * @brief Generator construct a %discard_block_engine engine.
  806. *
  807. * @param __q A seed sequence.
  808. */
  809. template<typename _Sseq, typename = typename
  810. std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value
  811. && !std::is_same<_Sseq, _RandomNumberEngine>::value>
  812. ::type>
  813. explicit
  814. discard_block_engine(_Sseq& __q)
  815. : _M_b(__q), _M_n(0)
  816. { }
  817. /**
  818. * @brief Reseeds the %discard_block_engine object with the default
  819. * seed for the underlying base class generator engine.
  820. */
  821. void
  822. seed()
  823. {
  824. _M_b.seed();
  825. _M_n = 0;
  826. }
  827. /**
  828. * @brief Reseeds the %discard_block_engine object with the default
  829. * seed for the underlying base class generator engine.
  830. */
  831. void
  832. seed(result_type __s)
  833. {
  834. _M_b.seed(__s);
  835. _M_n = 0;
  836. }
  837. /**
  838. * @brief Reseeds the %discard_block_engine object with the given seed
  839. * sequence.
  840. * @param __q A seed generator function.
  841. */
  842. template<typename _Sseq>
  843. void
  844. seed(_Sseq& __q)
  845. {
  846. _M_b.seed(__q);
  847. _M_n = 0;
  848. }
  849. /**
  850. * @brief Gets a const reference to the underlying generator engine
  851. * object.
  852. */
  853. const _RandomNumberEngine&
  854. base() const noexcept
  855. { return _M_b; }
  856. /**
  857. * @brief Gets the minimum value in the generated random number range.
  858. */
  859. static constexpr result_type
  860. min()
  861. { return _RandomNumberEngine::min(); }
  862. /**
  863. * @brief Gets the maximum value in the generated random number range.
  864. */
  865. static constexpr result_type
  866. max()
  867. { return _RandomNumberEngine::max(); }
  868. /**
  869. * @brief Discard a sequence of random numbers.
  870. */
  871. void
  872. discard(unsigned long long __z)
  873. {
  874. for (; __z != 0ULL; --__z)
  875. (*this)();
  876. }
  877. /**
  878. * @brief Gets the next value in the generated random number sequence.
  879. */
  880. result_type
  881. operator()();
  882. /**
  883. * @brief Compares two %discard_block_engine random number generator
  884. * objects of the same type for equality.
  885. *
  886. * @param __lhs A %discard_block_engine random number generator object.
  887. * @param __rhs Another %discard_block_engine random number generator
  888. * object.
  889. *
  890. * @returns true if the infinite sequences of generated values
  891. * would be equal, false otherwise.
  892. */
  893. friend bool
  894. operator==(const discard_block_engine& __lhs,
  895. const discard_block_engine& __rhs)
  896. { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
  897. /**
  898. * @brief Inserts the current state of a %discard_block_engine random
  899. * number generator engine @p __x into the output stream
  900. * @p __os.
  901. *
  902. * @param __os An output stream.
  903. * @param __x A %discard_block_engine random number generator engine.
  904. *
  905. * @returns The output stream with the state of @p __x inserted or in
  906. * an error state.
  907. */
  908. template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
  909. typename _CharT, typename _Traits>
  910. friend std::basic_ostream<_CharT, _Traits>&
  911. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  912. const std::discard_block_engine<_RandomNumberEngine1,
  913. __p1, __r1>& __x);
  914. /**
  915. * @brief Extracts the current state of a % subtract_with_carry_engine
  916. * random number generator engine @p __x from the input stream
  917. * @p __is.
  918. *
  919. * @param __is An input stream.
  920. * @param __x A %discard_block_engine random number generator engine.
  921. *
  922. * @returns The input stream with the state of @p __x extracted or in
  923. * an error state.
  924. */
  925. template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
  926. typename _CharT, typename _Traits>
  927. friend std::basic_istream<_CharT, _Traits>&
  928. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  929. std::discard_block_engine<_RandomNumberEngine1,
  930. __p1, __r1>& __x);
  931. private:
  932. _RandomNumberEngine _M_b;
  933. size_t _M_n;
  934. };
  935. /**
  936. * @brief Compares two %discard_block_engine random number generator
  937. * objects of the same type for inequality.
  938. *
  939. * @param __lhs A %discard_block_engine random number generator object.
  940. * @param __rhs Another %discard_block_engine random number generator
  941. * object.
  942. *
  943. * @returns true if the infinite sequences of generated values
  944. * would be different, false otherwise.
  945. */
  946. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  947. inline bool
  948. operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
  949. __r>& __lhs,
  950. const std::discard_block_engine<_RandomNumberEngine, __p,
  951. __r>& __rhs)
  952. { return !(__lhs == __rhs); }
  953. /**
  954. * Produces random numbers by combining random numbers from some base
  955. * engine to produce random numbers with a specifies number of bits @p __w.
  956. */
  957. template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
  958. class independent_bits_engine
  959. {
  960. static_assert(std::is_unsigned<_UIntType>::value,
  961. "result_type must be an unsigned integral type");
  962. static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
  963. "template argument substituting __w out of bounds");
  964. public:
  965. /** The type of the generated random value. */
  966. typedef _UIntType result_type;
  967. /**
  968. * @brief Constructs a default %independent_bits_engine engine.
  969. *
  970. * The underlying engine is default constructed as well.
  971. */
  972. independent_bits_engine()
  973. : _M_b() { }
  974. /**
  975. * @brief Copy constructs a %independent_bits_engine engine.
  976. *
  977. * Copies an existing base class random number generator.
  978. * @param __rng An existing (base class) engine object.
  979. */
  980. explicit
  981. independent_bits_engine(const _RandomNumberEngine& __rng)
  982. : _M_b(__rng) { }
  983. /**
  984. * @brief Move constructs a %independent_bits_engine engine.
  985. *
  986. * Copies an existing base class random number generator.
  987. * @param __rng An existing (base class) engine object.
  988. */
  989. explicit
  990. independent_bits_engine(_RandomNumberEngine&& __rng)
  991. : _M_b(std::move(__rng)) { }
  992. /**
  993. * @brief Seed constructs a %independent_bits_engine engine.
  994. *
  995. * Constructs the underlying generator engine seeded with @p __s.
  996. * @param __s A seed value for the base class engine.
  997. */
  998. explicit
  999. independent_bits_engine(result_type __s)
  1000. : _M_b(__s) { }
  1001. /**
  1002. * @brief Generator construct a %independent_bits_engine engine.
  1003. *
  1004. * @param __q A seed sequence.
  1005. */
  1006. template<typename _Sseq, typename = typename
  1007. std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value
  1008. && !std::is_same<_Sseq, _RandomNumberEngine>::value>
  1009. ::type>
  1010. explicit
  1011. independent_bits_engine(_Sseq& __q)
  1012. : _M_b(__q)
  1013. { }
  1014. /**
  1015. * @brief Reseeds the %independent_bits_engine object with the default
  1016. * seed for the underlying base class generator engine.
  1017. */
  1018. void
  1019. seed()
  1020. { _M_b.seed(); }
  1021. /**
  1022. * @brief Reseeds the %independent_bits_engine object with the default
  1023. * seed for the underlying base class generator engine.
  1024. */
  1025. void
  1026. seed(result_type __s)
  1027. { _M_b.seed(__s); }
  1028. /**
  1029. * @brief Reseeds the %independent_bits_engine object with the given
  1030. * seed sequence.
  1031. * @param __q A seed generator function.
  1032. */
  1033. template<typename _Sseq>
  1034. void
  1035. seed(_Sseq& __q)
  1036. { _M_b.seed(__q); }
  1037. /**
  1038. * @brief Gets a const reference to the underlying generator engine
  1039. * object.
  1040. */
  1041. const _RandomNumberEngine&
  1042. base() const noexcept
  1043. { return _M_b; }
  1044. /**
  1045. * @brief Gets the minimum value in the generated random number range.
  1046. */
  1047. static constexpr result_type
  1048. min()
  1049. { return 0U; }
  1050. /**
  1051. * @brief Gets the maximum value in the generated random number range.
  1052. */
  1053. static constexpr result_type
  1054. max()
  1055. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  1056. /**
  1057. * @brief Discard a sequence of random numbers.
  1058. */
  1059. void
  1060. discard(unsigned long long __z)
  1061. {
  1062. for (; __z != 0ULL; --__z)
  1063. (*this)();
  1064. }
  1065. /**
  1066. * @brief Gets the next value in the generated random number sequence.
  1067. */
  1068. result_type
  1069. operator()();
  1070. /**
  1071. * @brief Compares two %independent_bits_engine random number generator
  1072. * objects of the same type for equality.
  1073. *
  1074. * @param __lhs A %independent_bits_engine random number generator
  1075. * object.
  1076. * @param __rhs Another %independent_bits_engine random number generator
  1077. * object.
  1078. *
  1079. * @returns true if the infinite sequences of generated values
  1080. * would be equal, false otherwise.
  1081. */
  1082. friend bool
  1083. operator==(const independent_bits_engine& __lhs,
  1084. const independent_bits_engine& __rhs)
  1085. { return __lhs._M_b == __rhs._M_b; }
  1086. /**
  1087. * @brief Extracts the current state of a % subtract_with_carry_engine
  1088. * random number generator engine @p __x from the input stream
  1089. * @p __is.
  1090. *
  1091. * @param __is An input stream.
  1092. * @param __x A %independent_bits_engine random number generator
  1093. * engine.
  1094. *
  1095. * @returns The input stream with the state of @p __x extracted or in
  1096. * an error state.
  1097. */
  1098. template<typename _CharT, typename _Traits>
  1099. friend std::basic_istream<_CharT, _Traits>&
  1100. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1101. std::independent_bits_engine<_RandomNumberEngine,
  1102. __w, _UIntType>& __x)
  1103. {
  1104. __is >> __x._M_b;
  1105. return __is;
  1106. }
  1107. private:
  1108. _RandomNumberEngine _M_b;
  1109. };
  1110. /**
  1111. * @brief Compares two %independent_bits_engine random number generator
  1112. * objects of the same type for inequality.
  1113. *
  1114. * @param __lhs A %independent_bits_engine random number generator
  1115. * object.
  1116. * @param __rhs Another %independent_bits_engine random number generator
  1117. * object.
  1118. *
  1119. * @returns true if the infinite sequences of generated values
  1120. * would be different, false otherwise.
  1121. */
  1122. template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
  1123. inline bool
  1124. operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
  1125. _UIntType>& __lhs,
  1126. const std::independent_bits_engine<_RandomNumberEngine, __w,
  1127. _UIntType>& __rhs)
  1128. { return !(__lhs == __rhs); }
  1129. /**
  1130. * @brief Inserts the current state of a %independent_bits_engine random
  1131. * number generator engine @p __x into the output stream @p __os.
  1132. *
  1133. * @param __os An output stream.
  1134. * @param __x A %independent_bits_engine random number generator engine.
  1135. *
  1136. * @returns The output stream with the state of @p __x inserted or in
  1137. * an error state.
  1138. */
  1139. template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
  1140. typename _CharT, typename _Traits>
  1141. std::basic_ostream<_CharT, _Traits>&
  1142. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1143. const std::independent_bits_engine<_RandomNumberEngine,
  1144. __w, _UIntType>& __x)
  1145. {
  1146. __os << __x.base();
  1147. return __os;
  1148. }
  1149. /**
  1150. * @brief Produces random numbers by combining random numbers from some
  1151. * base engine to produce random numbers with a specifies number of bits
  1152. * @p __k.
  1153. */
  1154. template<typename _RandomNumberEngine, size_t __k>
  1155. class shuffle_order_engine
  1156. {
  1157. static_assert(1u <= __k, "template argument substituting "
  1158. "__k out of bound");
  1159. public:
  1160. /** The type of the generated random value. */
  1161. typedef typename _RandomNumberEngine::result_type result_type;
  1162. static constexpr size_t table_size = __k;
  1163. /**
  1164. * @brief Constructs a default %shuffle_order_engine engine.
  1165. *
  1166. * The underlying engine is default constructed as well.
  1167. */
  1168. shuffle_order_engine()
  1169. : _M_b()
  1170. { _M_initialize(); }
  1171. /**
  1172. * @brief Copy constructs a %shuffle_order_engine engine.
  1173. *
  1174. * Copies an existing base class random number generator.
  1175. * @param __rng An existing (base class) engine object.
  1176. */
  1177. explicit
  1178. shuffle_order_engine(const _RandomNumberEngine& __rng)
  1179. : _M_b(__rng)
  1180. { _M_initialize(); }
  1181. /**
  1182. * @brief Move constructs a %shuffle_order_engine engine.
  1183. *
  1184. * Copies an existing base class random number generator.
  1185. * @param __rng An existing (base class) engine object.
  1186. */
  1187. explicit
  1188. shuffle_order_engine(_RandomNumberEngine&& __rng)
  1189. : _M_b(std::move(__rng))
  1190. { _M_initialize(); }
  1191. /**
  1192. * @brief Seed constructs a %shuffle_order_engine engine.
  1193. *
  1194. * Constructs the underlying generator engine seeded with @p __s.
  1195. * @param __s A seed value for the base class engine.
  1196. */
  1197. explicit
  1198. shuffle_order_engine(result_type __s)
  1199. : _M_b(__s)
  1200. { _M_initialize(); }
  1201. /**
  1202. * @brief Generator construct a %shuffle_order_engine engine.
  1203. *
  1204. * @param __q A seed sequence.
  1205. */
  1206. template<typename _Sseq, typename = typename
  1207. std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value
  1208. && !std::is_same<_Sseq, _RandomNumberEngine>::value>
  1209. ::type>
  1210. explicit
  1211. shuffle_order_engine(_Sseq& __q)
  1212. : _M_b(__q)
  1213. { _M_initialize(); }
  1214. /**
  1215. * @brief Reseeds the %shuffle_order_engine object with the default seed
  1216. for the underlying base class generator engine.
  1217. */
  1218. void
  1219. seed()
  1220. {
  1221. _M_b.seed();
  1222. _M_initialize();
  1223. }
  1224. /**
  1225. * @brief Reseeds the %shuffle_order_engine object with the default seed
  1226. * for the underlying base class generator engine.
  1227. */
  1228. void
  1229. seed(result_type __s)
  1230. {
  1231. _M_b.seed(__s);
  1232. _M_initialize();
  1233. }
  1234. /**
  1235. * @brief Reseeds the %shuffle_order_engine object with the given seed
  1236. * sequence.
  1237. * @param __q A seed generator function.
  1238. */
  1239. template<typename _Sseq>
  1240. void
  1241. seed(_Sseq& __q)
  1242. {
  1243. _M_b.seed(__q);
  1244. _M_initialize();
  1245. }
  1246. /**
  1247. * Gets a const reference to the underlying generator engine object.
  1248. */
  1249. const _RandomNumberEngine&
  1250. base() const noexcept
  1251. { return _M_b; }
  1252. /**
  1253. * Gets the minimum value in the generated random number range.
  1254. */
  1255. static constexpr result_type
  1256. min()
  1257. { return _RandomNumberEngine::min(); }
  1258. /**
  1259. * Gets the maximum value in the generated random number range.
  1260. */
  1261. static constexpr result_type
  1262. max()
  1263. { return _RandomNumberEngine::max(); }
  1264. /**
  1265. * Discard a sequence of random numbers.
  1266. */
  1267. void
  1268. discard(unsigned long long __z)
  1269. {
  1270. for (; __z != 0ULL; --__z)
  1271. (*this)();
  1272. }
  1273. /**
  1274. * Gets the next value in the generated random number sequence.
  1275. */
  1276. result_type
  1277. operator()();
  1278. /**
  1279. * Compares two %shuffle_order_engine random number generator objects
  1280. * of the same type for equality.
  1281. *
  1282. * @param __lhs A %shuffle_order_engine random number generator object.
  1283. * @param __rhs Another %shuffle_order_engine random number generator
  1284. * object.
  1285. *
  1286. * @returns true if the infinite sequences of generated values
  1287. * would be equal, false otherwise.
  1288. */
  1289. friend bool
  1290. operator==(const shuffle_order_engine& __lhs,
  1291. const shuffle_order_engine& __rhs)
  1292. { return (__lhs._M_b == __rhs._M_b
  1293. && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v)
  1294. && __lhs._M_y == __rhs._M_y); }
  1295. /**
  1296. * @brief Inserts the current state of a %shuffle_order_engine random
  1297. * number generator engine @p __x into the output stream
  1298. @p __os.
  1299. *
  1300. * @param __os An output stream.
  1301. * @param __x A %shuffle_order_engine random number generator engine.
  1302. *
  1303. * @returns The output stream with the state of @p __x inserted or in
  1304. * an error state.
  1305. */
  1306. template<typename _RandomNumberEngine1, size_t __k1,
  1307. typename _CharT, typename _Traits>
  1308. friend std::basic_ostream<_CharT, _Traits>&
  1309. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1310. const std::shuffle_order_engine<_RandomNumberEngine1,
  1311. __k1>& __x);
  1312. /**
  1313. * @brief Extracts the current state of a % subtract_with_carry_engine
  1314. * random number generator engine @p __x from the input stream
  1315. * @p __is.
  1316. *
  1317. * @param __is An input stream.
  1318. * @param __x A %shuffle_order_engine random number generator engine.
  1319. *
  1320. * @returns The input stream with the state of @p __x extracted or in
  1321. * an error state.
  1322. */
  1323. template<typename _RandomNumberEngine1, size_t __k1,
  1324. typename _CharT, typename _Traits>
  1325. friend std::basic_istream<_CharT, _Traits>&
  1326. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1327. std::shuffle_order_engine<_RandomNumberEngine1, __k1>& __x);
  1328. private:
  1329. void _M_initialize()
  1330. {
  1331. for (size_t __i = 0; __i < __k; ++__i)
  1332. _M_v[__i] = _M_b();
  1333. _M_y = _M_b();
  1334. }
  1335. _RandomNumberEngine _M_b;
  1336. result_type _M_v[__k];
  1337. result_type _M_y;
  1338. };
  1339. /**
  1340. * Compares two %shuffle_order_engine random number generator objects
  1341. * of the same type for inequality.
  1342. *
  1343. * @param __lhs A %shuffle_order_engine random number generator object.
  1344. * @param __rhs Another %shuffle_order_engine random number generator
  1345. * object.
  1346. *
  1347. * @returns true if the infinite sequences of generated values
  1348. * would be different, false otherwise.
  1349. */
  1350. template<typename _RandomNumberEngine, size_t __k>
  1351. inline bool
  1352. operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
  1353. __k>& __lhs,
  1354. const std::shuffle_order_engine<_RandomNumberEngine,
  1355. __k>& __rhs)
  1356. { return !(__lhs == __rhs); }
  1357. /**
  1358. * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
  1359. */
  1360. typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
  1361. minstd_rand0;
  1362. /**
  1363. * An alternative LCR (Lehmer Generator function).
  1364. */
  1365. typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
  1366. minstd_rand;
  1367. /**
  1368. * The classic Mersenne Twister.
  1369. *
  1370. * Reference:
  1371. * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
  1372. * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
  1373. * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
  1374. */
  1375. typedef mersenne_twister_engine<
  1376. uint_fast32_t,
  1377. 32, 624, 397, 31,
  1378. 0x9908b0dfUL, 11,
  1379. 0xffffffffUL, 7,
  1380. 0x9d2c5680UL, 15,
  1381. 0xefc60000UL, 18, 1812433253UL> mt19937;
  1382. /**
  1383. * An alternative Mersenne Twister.
  1384. */
  1385. typedef mersenne_twister_engine<
  1386. uint_fast64_t,
  1387. 64, 312, 156, 31,
  1388. 0xb5026f5aa96619e9ULL, 29,
  1389. 0x5555555555555555ULL, 17,
  1390. 0x71d67fffeda60000ULL, 37,
  1391. 0xfff7eee000000000ULL, 43,
  1392. 6364136223846793005ULL> mt19937_64;
  1393. typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
  1394. ranlux24_base;
  1395. typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
  1396. ranlux48_base;
  1397. typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
  1398. typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
  1399. typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
  1400. typedef minstd_rand0 default_random_engine;
  1401. /**
  1402. * A standard interface to a platform-specific non-deterministic
  1403. * random number generator (if any are available).
  1404. */
  1405. class random_device
  1406. {
  1407. public:
  1408. /** The type of the generated random value. */
  1409. typedef unsigned int result_type;
  1410. // constructors, destructors and member functions
  1411. #ifdef _GLIBCXX_USE_RANDOM_TR1
  1412. explicit
  1413. random_device(const std::string& __token = "default")
  1414. {
  1415. _M_init(__token);
  1416. }
  1417. ~random_device()
  1418. { _M_fini(); }
  1419. #else
  1420. explicit
  1421. random_device(const std::string& __token = "mt19937")
  1422. { _M_init_pretr1(__token); }
  1423. public:
  1424. #endif
  1425. static constexpr result_type
  1426. min()
  1427. { return std::numeric_limits<result_type>::min(); }
  1428. static constexpr result_type
  1429. max()
  1430. { return std::numeric_limits<result_type>::max(); }
  1431. double
  1432. entropy() const noexcept
  1433. { return 0.0; }
  1434. result_type
  1435. operator()()
  1436. {
  1437. #ifdef _GLIBCXX_USE_RANDOM_TR1
  1438. return this->_M_getval();
  1439. #else
  1440. return this->_M_getval_pretr1();
  1441. #endif
  1442. }
  1443. // No copy functions.
  1444. random_device(const random_device&) = delete;
  1445. void operator=(const random_device&) = delete;
  1446. private:
  1447. void _M_init(const std::string& __token);
  1448. void _M_init_pretr1(const std::string& __token);
  1449. void _M_fini();
  1450. result_type _M_getval();
  1451. result_type _M_getval_pretr1();
  1452. union
  1453. {
  1454. void* _M_file;
  1455. mt19937 _M_mt;
  1456. };
  1457. };
  1458. /* @} */ // group random_generators
  1459. /**
  1460. * @addtogroup random_distributions Random Number Distributions
  1461. * @ingroup random
  1462. * @{
  1463. */
  1464. /**
  1465. * @addtogroup random_distributions_uniform Uniform Distributions
  1466. * @ingroup random_distributions
  1467. * @{
  1468. */
  1469. // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
  1470. /**
  1471. * @brief Return true if two uniform integer distributions have
  1472. * different parameters.
  1473. */
  1474. template<typename _IntType>
  1475. inline bool
  1476. operator!=(const std::uniform_int_distribution<_IntType>& __d1,
  1477. const std::uniform_int_distribution<_IntType>& __d2)
  1478. { return !(__d1 == __d2); }
  1479. /**
  1480. * @brief Inserts a %uniform_int_distribution random number
  1481. * distribution @p __x into the output stream @p os.
  1482. *
  1483. * @param __os An output stream.
  1484. * @param __x A %uniform_int_distribution random number distribution.
  1485. *
  1486. * @returns The output stream with the state of @p __x inserted or in
  1487. * an error state.
  1488. */
  1489. template<typename _IntType, typename _CharT, typename _Traits>
  1490. std::basic_ostream<_CharT, _Traits>&
  1491. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1492. const std::uniform_int_distribution<_IntType>&);
  1493. /**
  1494. * @brief Extracts a %uniform_int_distribution random number distribution
  1495. * @p __x from the input stream @p __is.
  1496. *
  1497. * @param __is An input stream.
  1498. * @param __x A %uniform_int_distribution random number generator engine.
  1499. *
  1500. * @returns The input stream with @p __x extracted or in an error state.
  1501. */
  1502. template<typename _IntType, typename _CharT, typename _Traits>
  1503. std::basic_istream<_CharT, _Traits>&
  1504. operator>>(std::basic_istream<_CharT, _Traits>&,
  1505. std::uniform_int_distribution<_IntType>&);
  1506. /**
  1507. * @brief Uniform continuous distribution for random numbers.
  1508. *
  1509. * A continuous random distribution on the range [min, max) with equal
  1510. * probability throughout the range. The URNG should be real-valued and
  1511. * deliver number in the range [0, 1).
  1512. */
  1513. template<typename _RealType = double>
  1514. class uniform_real_distribution
  1515. {
  1516. static_assert(std::is_floating_point<_RealType>::value,
  1517. "result_type must be a floating point type");
  1518. public:
  1519. /** The type of the range of the distribution. */
  1520. typedef _RealType result_type;
  1521. /** Parameter type. */
  1522. struct param_type
  1523. {
  1524. typedef uniform_real_distribution<_RealType> distribution_type;
  1525. explicit
  1526. param_type(_RealType __a = _RealType(0),
  1527. _RealType __b = _RealType(1))
  1528. : _M_a(__a), _M_b(__b)
  1529. {
  1530. __glibcxx_assert(_M_a <= _M_b);
  1531. }
  1532. result_type
  1533. a() const
  1534. { return _M_a; }
  1535. result_type
  1536. b() const
  1537. { return _M_b; }
  1538. friend bool
  1539. operator==(const param_type& __p1, const param_type& __p2)
  1540. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  1541. friend bool
  1542. operator!=(const param_type& __p1, const param_type& __p2)
  1543. { return !(__p1 == __p2); }
  1544. private:
  1545. _RealType _M_a;
  1546. _RealType _M_b;
  1547. };
  1548. public:
  1549. /**
  1550. * @brief Constructs a uniform_real_distribution object.
  1551. *
  1552. * @param __a [IN] The lower bound of the distribution.
  1553. * @param __b [IN] The upper bound of the distribution.
  1554. */
  1555. explicit
  1556. uniform_real_distribution(_RealType __a = _RealType(0),
  1557. _RealType __b = _RealType(1))
  1558. : _M_param(__a, __b)
  1559. { }
  1560. explicit
  1561. uniform_real_distribution(const param_type& __p)
  1562. : _M_param(__p)
  1563. { }
  1564. /**
  1565. * @brief Resets the distribution state.
  1566. *
  1567. * Does nothing for the uniform real distribution.
  1568. */
  1569. void
  1570. reset() { }
  1571. result_type
  1572. a() const
  1573. { return _M_param.a(); }
  1574. result_type
  1575. b() const
  1576. { return _M_param.b(); }
  1577. /**
  1578. * @brief Returns the parameter set of the distribution.
  1579. */
  1580. param_type
  1581. param() const
  1582. { return _M_param; }
  1583. /**
  1584. * @brief Sets the parameter set of the distribution.
  1585. * @param __param The new parameter set of the distribution.
  1586. */
  1587. void
  1588. param(const param_type& __param)
  1589. { _M_param = __param; }
  1590. /**
  1591. * @brief Returns the inclusive lower bound of the distribution range.
  1592. */
  1593. result_type
  1594. min() const
  1595. { return this->a(); }
  1596. /**
  1597. * @brief Returns the inclusive upper bound of the distribution range.
  1598. */
  1599. result_type
  1600. max() const
  1601. { return this->b(); }
  1602. /**
  1603. * @brief Generating functions.
  1604. */
  1605. template<typename _UniformRandomNumberGenerator>
  1606. result_type
  1607. operator()(_UniformRandomNumberGenerator& __urng)
  1608. { return this->operator()(__urng, _M_param); }
  1609. template<typename _UniformRandomNumberGenerator>
  1610. result_type
  1611. operator()(_UniformRandomNumberGenerator& __urng,
  1612. const param_type& __p)
  1613. {
  1614. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1615. __aurng(__urng);
  1616. return (__aurng() * (__p.b() - __p.a())) + __p.a();
  1617. }
  1618. template<typename _ForwardIterator,
  1619. typename _UniformRandomNumberGenerator>
  1620. void
  1621. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1622. _UniformRandomNumberGenerator& __urng)
  1623. { this->__generate(__f, __t, __urng, _M_param); }
  1624. template<typename _ForwardIterator,
  1625. typename _UniformRandomNumberGenerator>
  1626. void
  1627. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1628. _UniformRandomNumberGenerator& __urng,
  1629. const param_type& __p)
  1630. { this->__generate_impl(__f, __t, __urng, __p); }
  1631. template<typename _UniformRandomNumberGenerator>
  1632. void
  1633. __generate(result_type* __f, result_type* __t,
  1634. _UniformRandomNumberGenerator& __urng,
  1635. const param_type& __p)
  1636. { this->__generate_impl(__f, __t, __urng, __p); }
  1637. /**
  1638. * @brief Return true if two uniform real distributions have
  1639. * the same parameters.
  1640. */
  1641. friend bool
  1642. operator==(const uniform_real_distribution& __d1,
  1643. const uniform_real_distribution& __d2)
  1644. { return __d1._M_param == __d2._M_param; }
  1645. private:
  1646. template<typename _ForwardIterator,
  1647. typename _UniformRandomNumberGenerator>
  1648. void
  1649. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1650. _UniformRandomNumberGenerator& __urng,
  1651. const param_type& __p);
  1652. param_type _M_param;
  1653. };
  1654. /**
  1655. * @brief Return true if two uniform real distributions have
  1656. * different parameters.
  1657. */
  1658. template<typename _IntType>
  1659. inline bool
  1660. operator!=(const std::uniform_real_distribution<_IntType>& __d1,
  1661. const std::uniform_real_distribution<_IntType>& __d2)
  1662. { return !(__d1 == __d2); }
  1663. /**
  1664. * @brief Inserts a %uniform_real_distribution random number
  1665. * distribution @p __x into the output stream @p __os.
  1666. *
  1667. * @param __os An output stream.
  1668. * @param __x A %uniform_real_distribution random number distribution.
  1669. *
  1670. * @returns The output stream with the state of @p __x inserted or in
  1671. * an error state.
  1672. */
  1673. template<typename _RealType, typename _CharT, typename _Traits>
  1674. std::basic_ostream<_CharT, _Traits>&
  1675. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1676. const std::uniform_real_distribution<_RealType>&);
  1677. /**
  1678. * @brief Extracts a %uniform_real_distribution random number distribution
  1679. * @p __x from the input stream @p __is.
  1680. *
  1681. * @param __is An input stream.
  1682. * @param __x A %uniform_real_distribution random number generator engine.
  1683. *
  1684. * @returns The input stream with @p __x extracted or in an error state.
  1685. */
  1686. template<typename _RealType, typename _CharT, typename _Traits>
  1687. std::basic_istream<_CharT, _Traits>&
  1688. operator>>(std::basic_istream<_CharT, _Traits>&,
  1689. std::uniform_real_distribution<_RealType>&);
  1690. /* @} */ // group random_distributions_uniform
  1691. /**
  1692. * @addtogroup random_distributions_normal Normal Distributions
  1693. * @ingroup random_distributions
  1694. * @{
  1695. */
  1696. /**
  1697. * @brief A normal continuous distribution for random numbers.
  1698. *
  1699. * The formula for the normal probability density function is
  1700. * @f[
  1701. * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
  1702. * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
  1703. * @f]
  1704. */
  1705. template<typename _RealType = double>
  1706. class normal_distribution
  1707. {
  1708. static_assert(std::is_floating_point<_RealType>::value,
  1709. "result_type must be a floating point type");
  1710. public:
  1711. /** The type of the range of the distribution. */
  1712. typedef _RealType result_type;
  1713. /** Parameter type. */
  1714. struct param_type
  1715. {
  1716. typedef normal_distribution<_RealType> distribution_type;
  1717. explicit
  1718. param_type(_RealType __mean = _RealType(0),
  1719. _RealType __stddev = _RealType(1))
  1720. : _M_mean(__mean), _M_stddev(__stddev)
  1721. {
  1722. __glibcxx_assert(_M_stddev > _RealType(0));
  1723. }
  1724. _RealType
  1725. mean() const
  1726. { return _M_mean; }
  1727. _RealType
  1728. stddev() const
  1729. { return _M_stddev; }
  1730. friend bool
  1731. operator==(const param_type& __p1, const param_type& __p2)
  1732. { return (__p1._M_mean == __p2._M_mean
  1733. && __p1._M_stddev == __p2._M_stddev); }
  1734. friend bool
  1735. operator!=(const param_type& __p1, const param_type& __p2)
  1736. { return !(__p1 == __p2); }
  1737. private:
  1738. _RealType _M_mean;
  1739. _RealType _M_stddev;
  1740. };
  1741. public:
  1742. /**
  1743. * Constructs a normal distribution with parameters @f$mean@f$ and
  1744. * standard deviation.
  1745. */
  1746. explicit
  1747. normal_distribution(result_type __mean = result_type(0),
  1748. result_type __stddev = result_type(1))
  1749. : _M_param(__mean, __stddev), _M_saved_available(false)
  1750. { }
  1751. explicit
  1752. normal_distribution(const param_type& __p)
  1753. : _M_param(__p), _M_saved_available(false)
  1754. { }
  1755. /**
  1756. * @brief Resets the distribution state.
  1757. */
  1758. void
  1759. reset()
  1760. { _M_saved_available = false; }
  1761. /**
  1762. * @brief Returns the mean of the distribution.
  1763. */
  1764. _RealType
  1765. mean() const
  1766. { return _M_param.mean(); }
  1767. /**
  1768. * @brief Returns the standard deviation of the distribution.
  1769. */
  1770. _RealType
  1771. stddev() const
  1772. { return _M_param.stddev(); }
  1773. /**
  1774. * @brief Returns the parameter set of the distribution.
  1775. */
  1776. param_type
  1777. param() const
  1778. { return _M_param; }
  1779. /**
  1780. * @brief Sets the parameter set of the distribution.
  1781. * @param __param The new parameter set of the distribution.
  1782. */
  1783. void
  1784. param(const param_type& __param)
  1785. { _M_param = __param; }
  1786. /**
  1787. * @brief Returns the greatest lower bound value of the distribution.
  1788. */
  1789. result_type
  1790. min() const
  1791. { return std::numeric_limits<result_type>::lowest(); }
  1792. /**
  1793. * @brief Returns the least upper bound value of the distribution.
  1794. */
  1795. result_type
  1796. max() const
  1797. { return std::numeric_limits<result_type>::max(); }
  1798. /**
  1799. * @brief Generating functions.
  1800. */
  1801. template<typename _UniformRandomNumberGenerator>
  1802. result_type
  1803. operator()(_UniformRandomNumberGenerator& __urng)
  1804. { return this->operator()(__urng, _M_param); }
  1805. template<typename _UniformRandomNumberGenerator>
  1806. result_type
  1807. operator()(_UniformRandomNumberGenerator& __urng,
  1808. const param_type& __p);
  1809. template<typename _ForwardIterator,
  1810. typename _UniformRandomNumberGenerator>
  1811. void
  1812. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1813. _UniformRandomNumberGenerator& __urng)
  1814. { this->__generate(__f, __t, __urng, _M_param); }
  1815. template<typename _ForwardIterator,
  1816. typename _UniformRandomNumberGenerator>
  1817. void
  1818. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1819. _UniformRandomNumberGenerator& __urng,
  1820. const param_type& __p)
  1821. { this->__generate_impl(__f, __t, __urng, __p); }
  1822. template<typename _UniformRandomNumberGenerator>
  1823. void
  1824. __generate(result_type* __f, result_type* __t,
  1825. _UniformRandomNumberGenerator& __urng,
  1826. const param_type& __p)
  1827. { this->__generate_impl(__f, __t, __urng, __p); }
  1828. /**
  1829. * @brief Return true if two normal distributions have
  1830. * the same parameters and the sequences that would
  1831. * be generated are equal.
  1832. */
  1833. template<typename _RealType1>
  1834. friend bool
  1835. operator==(const std::normal_distribution<_RealType1>& __d1,
  1836. const std::normal_distribution<_RealType1>& __d2);
  1837. /**
  1838. * @brief Inserts a %normal_distribution random number distribution
  1839. * @p __x into the output stream @p __os.
  1840. *
  1841. * @param __os An output stream.
  1842. * @param __x A %normal_distribution random number distribution.
  1843. *
  1844. * @returns The output stream with the state of @p __x inserted or in
  1845. * an error state.
  1846. */
  1847. template<typename _RealType1, typename _CharT, typename _Traits>
  1848. friend std::basic_ostream<_CharT, _Traits>&
  1849. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1850. const std::normal_distribution<_RealType1>& __x);
  1851. /**
  1852. * @brief Extracts a %normal_distribution random number distribution
  1853. * @p __x from the input stream @p __is.
  1854. *
  1855. * @param __is An input stream.
  1856. * @param __x A %normal_distribution random number generator engine.
  1857. *
  1858. * @returns The input stream with @p __x extracted or in an error
  1859. * state.
  1860. */
  1861. template<typename _RealType1, typename _CharT, typename _Traits>
  1862. friend std::basic_istream<_CharT, _Traits>&
  1863. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1864. std::normal_distribution<_RealType1>& __x);
  1865. private:
  1866. template<typename _ForwardIterator,
  1867. typename _UniformRandomNumberGenerator>
  1868. void
  1869. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1870. _UniformRandomNumberGenerator& __urng,
  1871. const param_type& __p);
  1872. param_type _M_param;
  1873. result_type _M_saved;
  1874. bool _M_saved_available;
  1875. };
  1876. /**
  1877. * @brief Return true if two normal distributions are different.
  1878. */
  1879. template<typename _RealType>
  1880. inline bool
  1881. operator!=(const std::normal_distribution<_RealType>& __d1,
  1882. const std::normal_distribution<_RealType>& __d2)
  1883. { return !(__d1 == __d2); }
  1884. /**
  1885. * @brief A lognormal_distribution random number distribution.
  1886. *
  1887. * The formula for the normal probability mass function is
  1888. * @f[
  1889. * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
  1890. * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
  1891. * @f]
  1892. */
  1893. template<typename _RealType = double>
  1894. class lognormal_distribution
  1895. {
  1896. static_assert(std::is_floating_point<_RealType>::value,
  1897. "result_type must be a floating point type");
  1898. public:
  1899. /** The type of the range of the distribution. */
  1900. typedef _RealType result_type;
  1901. /** Parameter type. */
  1902. struct param_type
  1903. {
  1904. typedef lognormal_distribution<_RealType> distribution_type;
  1905. explicit
  1906. param_type(_RealType __m = _RealType(0),
  1907. _RealType __s = _RealType(1))
  1908. : _M_m(__m), _M_s(__s)
  1909. { }
  1910. _RealType
  1911. m() const
  1912. { return _M_m; }
  1913. _RealType
  1914. s() const
  1915. { return _M_s; }
  1916. friend bool
  1917. operator==(const param_type& __p1, const param_type& __p2)
  1918. { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
  1919. friend bool
  1920. operator!=(const param_type& __p1, const param_type& __p2)
  1921. { return !(__p1 == __p2); }
  1922. private:
  1923. _RealType _M_m;
  1924. _RealType _M_s;
  1925. };
  1926. explicit
  1927. lognormal_distribution(_RealType __m = _RealType(0),
  1928. _RealType __s = _RealType(1))
  1929. : _M_param(__m, __s), _M_nd()
  1930. { }
  1931. explicit
  1932. lognormal_distribution(const param_type& __p)
  1933. : _M_param(__p), _M_nd()
  1934. { }
  1935. /**
  1936. * Resets the distribution state.
  1937. */
  1938. void
  1939. reset()
  1940. { _M_nd.reset(); }
  1941. /**
  1942. *
  1943. */
  1944. _RealType
  1945. m() const
  1946. { return _M_param.m(); }
  1947. _RealType
  1948. s() const
  1949. { return _M_param.s(); }
  1950. /**
  1951. * @brief Returns the parameter set of the distribution.
  1952. */
  1953. param_type
  1954. param() const
  1955. { return _M_param; }
  1956. /**
  1957. * @brief Sets the parameter set of the distribution.
  1958. * @param __param The new parameter set of the distribution.
  1959. */
  1960. void
  1961. param(const param_type& __param)
  1962. { _M_param = __param; }
  1963. /**
  1964. * @brief Returns the greatest lower bound value of the distribution.
  1965. */
  1966. result_type
  1967. min() const
  1968. { return result_type(0); }
  1969. /**
  1970. * @brief Returns the least upper bound value of the distribution.
  1971. */
  1972. result_type
  1973. max() const
  1974. { return std::numeric_limits<result_type>::max(); }
  1975. /**
  1976. * @brief Generating functions.
  1977. */
  1978. template<typename _UniformRandomNumberGenerator>
  1979. result_type
  1980. operator()(_UniformRandomNumberGenerator& __urng)
  1981. { return this->operator()(__urng, _M_param); }
  1982. template<typename _UniformRandomNumberGenerator>
  1983. result_type
  1984. operator()(_UniformRandomNumberGenerator& __urng,
  1985. const param_type& __p)
  1986. { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
  1987. template<typename _ForwardIterator,
  1988. typename _UniformRandomNumberGenerator>
  1989. void
  1990. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1991. _UniformRandomNumberGenerator& __urng)
  1992. { this->__generate(__f, __t, __urng, _M_param); }
  1993. template<typename _ForwardIterator,
  1994. typename _UniformRandomNumberGenerator>
  1995. void
  1996. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1997. _UniformRandomNumberGenerator& __urng,
  1998. const param_type& __p)
  1999. { this->__generate_impl(__f, __t, __urng, __p); }
  2000. template<typename _UniformRandomNumberGenerator>
  2001. void
  2002. __generate(result_type* __f, result_type* __t,
  2003. _UniformRandomNumberGenerator& __urng,
  2004. const param_type& __p)
  2005. { this->__generate_impl(__f, __t, __urng, __p); }
  2006. /**
  2007. * @brief Return true if two lognormal distributions have
  2008. * the same parameters and the sequences that would
  2009. * be generated are equal.
  2010. */
  2011. friend bool
  2012. operator==(const lognormal_distribution& __d1,
  2013. const lognormal_distribution& __d2)
  2014. { return (__d1._M_param == __d2._M_param
  2015. && __d1._M_nd == __d2._M_nd); }
  2016. /**
  2017. * @brief Inserts a %lognormal_distribution random number distribution
  2018. * @p __x into the output stream @p __os.
  2019. *
  2020. * @param __os An output stream.
  2021. * @param __x A %lognormal_distribution random number distribution.
  2022. *
  2023. * @returns The output stream with the state of @p __x inserted or in
  2024. * an error state.
  2025. */
  2026. template<typename _RealType1, typename _CharT, typename _Traits>
  2027. friend std::basic_ostream<_CharT, _Traits>&
  2028. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2029. const std::lognormal_distribution<_RealType1>& __x);
  2030. /**
  2031. * @brief Extracts a %lognormal_distribution random number distribution
  2032. * @p __x from the input stream @p __is.
  2033. *
  2034. * @param __is An input stream.
  2035. * @param __x A %lognormal_distribution random number
  2036. * generator engine.
  2037. *
  2038. * @returns The input stream with @p __x extracted or in an error state.
  2039. */
  2040. template<typename _RealType1, typename _CharT, typename _Traits>
  2041. friend std::basic_istream<_CharT, _Traits>&
  2042. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2043. std::lognormal_distribution<_RealType1>& __x);
  2044. private:
  2045. template<typename _ForwardIterator,
  2046. typename _UniformRandomNumberGenerator>
  2047. void
  2048. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2049. _UniformRandomNumberGenerator& __urng,
  2050. const param_type& __p);
  2051. param_type _M_param;
  2052. std::normal_distribution<result_type> _M_nd;
  2053. };
  2054. /**
  2055. * @brief Return true if two lognormal distributions are different.
  2056. */
  2057. template<typename _RealType>
  2058. inline bool
  2059. operator!=(const std::lognormal_distribution<_RealType>& __d1,
  2060. const std::lognormal_distribution<_RealType>& __d2)
  2061. { return !(__d1 == __d2); }
  2062. /**
  2063. * @brief A gamma continuous distribution for random numbers.
  2064. *
  2065. * The formula for the gamma probability density function is:
  2066. * @f[
  2067. * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
  2068. * (x/\beta)^{\alpha - 1} e^{-x/\beta}
  2069. * @f]
  2070. */
  2071. template<typename _RealType = double>
  2072. class gamma_distribution
  2073. {
  2074. static_assert(std::is_floating_point<_RealType>::value,
  2075. "result_type must be a floating point type");
  2076. public:
  2077. /** The type of the range of the distribution. */
  2078. typedef _RealType result_type;
  2079. /** Parameter type. */
  2080. struct param_type
  2081. {
  2082. typedef gamma_distribution<_RealType> distribution_type;
  2083. friend class gamma_distribution<_RealType>;
  2084. explicit
  2085. param_type(_RealType __alpha_val = _RealType(1),
  2086. _RealType __beta_val = _RealType(1))
  2087. : _M_alpha(__alpha_val), _M_beta(__beta_val)
  2088. {
  2089. __glibcxx_assert(_M_alpha > _RealType(0));
  2090. _M_initialize();
  2091. }
  2092. _RealType
  2093. alpha() const
  2094. { return _M_alpha; }
  2095. _RealType
  2096. beta() const
  2097. { return _M_beta; }
  2098. friend bool
  2099. operator==(const param_type& __p1, const param_type& __p2)
  2100. { return (__p1._M_alpha == __p2._M_alpha
  2101. && __p1._M_beta == __p2._M_beta); }
  2102. friend bool
  2103. operator!=(const param_type& __p1, const param_type& __p2)
  2104. { return !(__p1 == __p2); }
  2105. private:
  2106. void
  2107. _M_initialize();
  2108. _RealType _M_alpha;
  2109. _RealType _M_beta;
  2110. _RealType _M_malpha, _M_a2;
  2111. };
  2112. public:
  2113. /**
  2114. * @brief Constructs a gamma distribution with parameters
  2115. * @f$\alpha@f$ and @f$\beta@f$.
  2116. */
  2117. explicit
  2118. gamma_distribution(_RealType __alpha_val = _RealType(1),
  2119. _RealType __beta_val = _RealType(1))
  2120. : _M_param(__alpha_val, __beta_val), _M_nd()
  2121. { }
  2122. explicit
  2123. gamma_distribution(const param_type& __p)
  2124. : _M_param(__p), _M_nd()
  2125. { }
  2126. /**
  2127. * @brief Resets the distribution state.
  2128. */
  2129. void
  2130. reset()
  2131. { _M_nd.reset(); }
  2132. /**
  2133. * @brief Returns the @f$\alpha@f$ of the distribution.
  2134. */
  2135. _RealType
  2136. alpha() const
  2137. { return _M_param.alpha(); }
  2138. /**
  2139. * @brief Returns the @f$\beta@f$ of the distribution.
  2140. */
  2141. _RealType
  2142. beta() const
  2143. { return _M_param.beta(); }
  2144. /**
  2145. * @brief Returns the parameter set of the distribution.
  2146. */
  2147. param_type
  2148. param() const
  2149. { return _M_param; }
  2150. /**
  2151. * @brief Sets the parameter set of the distribution.
  2152. * @param __param The new parameter set of the distribution.
  2153. */
  2154. void
  2155. param(const param_type& __param)
  2156. { _M_param = __param; }
  2157. /**
  2158. * @brief Returns the greatest lower bound value of the distribution.
  2159. */
  2160. result_type
  2161. min() const
  2162. { return result_type(0); }
  2163. /**
  2164. * @brief Returns the least upper bound value of the distribution.
  2165. */
  2166. result_type
  2167. max() const
  2168. { return std::numeric_limits<result_type>::max(); }
  2169. /**
  2170. * @brief Generating functions.
  2171. */
  2172. template<typename _UniformRandomNumberGenerator>
  2173. result_type
  2174. operator()(_UniformRandomNumberGenerator& __urng)
  2175. { return this->operator()(__urng, _M_param); }
  2176. template<typename _UniformRandomNumberGenerator>
  2177. result_type
  2178. operator()(_UniformRandomNumberGenerator& __urng,
  2179. const param_type& __p);
  2180. template<typename _ForwardIterator,
  2181. typename _UniformRandomNumberGenerator>
  2182. void
  2183. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2184. _UniformRandomNumberGenerator& __urng)
  2185. { this->__generate(__f, __t, __urng, _M_param); }
  2186. template<typename _ForwardIterator,
  2187. typename _UniformRandomNumberGenerator>
  2188. void
  2189. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2190. _UniformRandomNumberGenerator& __urng,
  2191. const param_type& __p)
  2192. { this->__generate_impl(__f, __t, __urng, __p); }
  2193. template<typename _UniformRandomNumberGenerator>
  2194. void
  2195. __generate(result_type* __f, result_type* __t,
  2196. _UniformRandomNumberGenerator& __urng,
  2197. const param_type& __p)
  2198. { this->__generate_impl(__f, __t, __urng, __p); }
  2199. /**
  2200. * @brief Return true if two gamma distributions have the same
  2201. * parameters and the sequences that would be generated
  2202. * are equal.
  2203. */
  2204. friend bool
  2205. operator==(const gamma_distribution& __d1,
  2206. const gamma_distribution& __d2)
  2207. { return (__d1._M_param == __d2._M_param
  2208. && __d1._M_nd == __d2._M_nd); }
  2209. /**
  2210. * @brief Inserts a %gamma_distribution random number distribution
  2211. * @p __x into the output stream @p __os.
  2212. *
  2213. * @param __os An output stream.
  2214. * @param __x A %gamma_distribution random number distribution.
  2215. *
  2216. * @returns The output stream with the state of @p __x inserted or in
  2217. * an error state.
  2218. */
  2219. template<typename _RealType1, typename _CharT, typename _Traits>
  2220. friend std::basic_ostream<_CharT, _Traits>&
  2221. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2222. const std::gamma_distribution<_RealType1>& __x);
  2223. /**
  2224. * @brief Extracts a %gamma_distribution random number distribution
  2225. * @p __x from the input stream @p __is.
  2226. *
  2227. * @param __is An input stream.
  2228. * @param __x A %gamma_distribution random number generator engine.
  2229. *
  2230. * @returns The input stream with @p __x extracted or in an error state.
  2231. */
  2232. template<typename _RealType1, typename _CharT, typename _Traits>
  2233. friend std::basic_istream<_CharT, _Traits>&
  2234. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2235. std::gamma_distribution<_RealType1>& __x);
  2236. private:
  2237. template<typename _ForwardIterator,
  2238. typename _UniformRandomNumberGenerator>
  2239. void
  2240. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2241. _UniformRandomNumberGenerator& __urng,
  2242. const param_type& __p);
  2243. param_type _M_param;
  2244. std::normal_distribution<result_type> _M_nd;
  2245. };
  2246. /**
  2247. * @brief Return true if two gamma distributions are different.
  2248. */
  2249. template<typename _RealType>
  2250. inline bool
  2251. operator!=(const std::gamma_distribution<_RealType>& __d1,
  2252. const std::gamma_distribution<_RealType>& __d2)
  2253. { return !(__d1 == __d2); }
  2254. /**
  2255. * @brief A chi_squared_distribution random number distribution.
  2256. *
  2257. * The formula for the normal probability mass function is
  2258. * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
  2259. */
  2260. template<typename _RealType = double>
  2261. class chi_squared_distribution
  2262. {
  2263. static_assert(std::is_floating_point<_RealType>::value,
  2264. "result_type must be a floating point type");
  2265. public:
  2266. /** The type of the range of the distribution. */
  2267. typedef _RealType result_type;
  2268. /** Parameter type. */
  2269. struct param_type
  2270. {
  2271. typedef chi_squared_distribution<_RealType> distribution_type;
  2272. explicit
  2273. param_type(_RealType __n = _RealType(1))
  2274. : _M_n(__n)
  2275. { }
  2276. _RealType
  2277. n() const
  2278. { return _M_n; }
  2279. friend bool
  2280. operator==(const param_type& __p1, const param_type& __p2)
  2281. { return __p1._M_n == __p2._M_n; }
  2282. friend bool
  2283. operator!=(const param_type& __p1, const param_type& __p2)
  2284. { return !(__p1 == __p2); }
  2285. private:
  2286. _RealType _M_n;
  2287. };
  2288. explicit
  2289. chi_squared_distribution(_RealType __n = _RealType(1))
  2290. : _M_param(__n), _M_gd(__n / 2)
  2291. { }
  2292. explicit
  2293. chi_squared_distribution(const param_type& __p)
  2294. : _M_param(__p), _M_gd(__p.n() / 2)
  2295. { }
  2296. /**
  2297. * @brief Resets the distribution state.
  2298. */
  2299. void
  2300. reset()
  2301. { _M_gd.reset(); }
  2302. /**
  2303. *
  2304. */
  2305. _RealType
  2306. n() const
  2307. { return _M_param.n(); }
  2308. /**
  2309. * @brief Returns the parameter set of the distribution.
  2310. */
  2311. param_type
  2312. param() const
  2313. { return _M_param; }
  2314. /**
  2315. * @brief Sets the parameter set of the distribution.
  2316. * @param __param The new parameter set of the distribution.
  2317. */
  2318. void
  2319. param(const param_type& __param)
  2320. {
  2321. _M_param = __param;
  2322. typedef typename std::gamma_distribution<result_type>::param_type
  2323. param_type;
  2324. _M_gd.param(param_type{__param.n() / 2});
  2325. }
  2326. /**
  2327. * @brief Returns the greatest lower bound value of the distribution.
  2328. */
  2329. result_type
  2330. min() const
  2331. { return result_type(0); }
  2332. /**
  2333. * @brief Returns the least upper bound value of the distribution.
  2334. */
  2335. result_type
  2336. max() const
  2337. { return std::numeric_limits<result_type>::max(); }
  2338. /**
  2339. * @brief Generating functions.
  2340. */
  2341. template<typename _UniformRandomNumberGenerator>
  2342. result_type
  2343. operator()(_UniformRandomNumberGenerator& __urng)
  2344. { return 2 * _M_gd(__urng); }
  2345. template<typename _UniformRandomNumberGenerator>
  2346. result_type
  2347. operator()(_UniformRandomNumberGenerator& __urng,
  2348. const param_type& __p)
  2349. {
  2350. typedef typename std::gamma_distribution<result_type>::param_type
  2351. param_type;
  2352. return 2 * _M_gd(__urng, param_type(__p.n() / 2));
  2353. }
  2354. template<typename _ForwardIterator,
  2355. typename _UniformRandomNumberGenerator>
  2356. void
  2357. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2358. _UniformRandomNumberGenerator& __urng)
  2359. { this->__generate_impl(__f, __t, __urng); }
  2360. template<typename _ForwardIterator,
  2361. typename _UniformRandomNumberGenerator>
  2362. void
  2363. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2364. _UniformRandomNumberGenerator& __urng,
  2365. const param_type& __p)
  2366. { typename std::gamma_distribution<result_type>::param_type
  2367. __p2(__p.n() / 2);
  2368. this->__generate_impl(__f, __t, __urng, __p2); }
  2369. template<typename _UniformRandomNumberGenerator>
  2370. void
  2371. __generate(result_type* __f, result_type* __t,
  2372. _UniformRandomNumberGenerator& __urng)
  2373. { this->__generate_impl(__f, __t, __urng); }
  2374. template<typename _UniformRandomNumberGenerator>
  2375. void
  2376. __generate(result_type* __f, result_type* __t,
  2377. _UniformRandomNumberGenerator& __urng,
  2378. const param_type& __p)
  2379. { typename std::gamma_distribution<result_type>::param_type
  2380. __p2(__p.n() / 2);
  2381. this->__generate_impl(__f, __t, __urng, __p2); }
  2382. /**
  2383. * @brief Return true if two Chi-squared distributions have
  2384. * the same parameters and the sequences that would be
  2385. * generated are equal.
  2386. */
  2387. friend bool
  2388. operator==(const chi_squared_distribution& __d1,
  2389. const chi_squared_distribution& __d2)
  2390. { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; }
  2391. /**
  2392. * @brief Inserts a %chi_squared_distribution random number distribution
  2393. * @p __x into the output stream @p __os.
  2394. *
  2395. * @param __os An output stream.
  2396. * @param __x A %chi_squared_distribution random number distribution.
  2397. *
  2398. * @returns The output stream with the state of @p __x inserted or in
  2399. * an error state.
  2400. */
  2401. template<typename _RealType1, typename _CharT, typename _Traits>
  2402. friend std::basic_ostream<_CharT, _Traits>&
  2403. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2404. const std::chi_squared_distribution<_RealType1>& __x);
  2405. /**
  2406. * @brief Extracts a %chi_squared_distribution random number distribution
  2407. * @p __x from the input stream @p __is.
  2408. *
  2409. * @param __is An input stream.
  2410. * @param __x A %chi_squared_distribution random number
  2411. * generator engine.
  2412. *
  2413. * @returns The input stream with @p __x extracted or in an error state.
  2414. */
  2415. template<typename _RealType1, typename _CharT, typename _Traits>
  2416. friend std::basic_istream<_CharT, _Traits>&
  2417. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2418. std::chi_squared_distribution<_RealType1>& __x);
  2419. private:
  2420. template<typename _ForwardIterator,
  2421. typename _UniformRandomNumberGenerator>
  2422. void
  2423. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2424. _UniformRandomNumberGenerator& __urng);
  2425. template<typename _ForwardIterator,
  2426. typename _UniformRandomNumberGenerator>
  2427. void
  2428. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2429. _UniformRandomNumberGenerator& __urng,
  2430. const typename
  2431. std::gamma_distribution<result_type>::param_type& __p);
  2432. param_type _M_param;
  2433. std::gamma_distribution<result_type> _M_gd;
  2434. };
  2435. /**
  2436. * @brief Return true if two Chi-squared distributions are different.
  2437. */
  2438. template<typename _RealType>
  2439. inline bool
  2440. operator!=(const std::chi_squared_distribution<_RealType>& __d1,
  2441. const std::chi_squared_distribution<_RealType>& __d2)
  2442. { return !(__d1 == __d2); }
  2443. /**
  2444. * @brief A cauchy_distribution random number distribution.
  2445. *
  2446. * The formula for the normal probability mass function is
  2447. * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
  2448. */
  2449. template<typename _RealType = double>
  2450. class cauchy_distribution
  2451. {
  2452. static_assert(std::is_floating_point<_RealType>::value,
  2453. "result_type must be a floating point type");
  2454. public:
  2455. /** The type of the range of the distribution. */
  2456. typedef _RealType result_type;
  2457. /** Parameter type. */
  2458. struct param_type
  2459. {
  2460. typedef cauchy_distribution<_RealType> distribution_type;
  2461. explicit
  2462. param_type(_RealType __a = _RealType(0),
  2463. _RealType __b = _RealType(1))
  2464. : _M_a(__a), _M_b(__b)
  2465. { }
  2466. _RealType
  2467. a() const
  2468. { return _M_a; }
  2469. _RealType
  2470. b() const
  2471. { return _M_b; }
  2472. friend bool
  2473. operator==(const param_type& __p1, const param_type& __p2)
  2474. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  2475. friend bool
  2476. operator!=(const param_type& __p1, const param_type& __p2)
  2477. { return !(__p1 == __p2); }
  2478. private:
  2479. _RealType _M_a;
  2480. _RealType _M_b;
  2481. };
  2482. explicit
  2483. cauchy_distribution(_RealType __a = _RealType(0),
  2484. _RealType __b = _RealType(1))
  2485. : _M_param(__a, __b)
  2486. { }
  2487. explicit
  2488. cauchy_distribution(const param_type& __p)
  2489. : _M_param(__p)
  2490. { }
  2491. /**
  2492. * @brief Resets the distribution state.
  2493. */
  2494. void
  2495. reset()
  2496. { }
  2497. /**
  2498. *
  2499. */
  2500. _RealType
  2501. a() const
  2502. { return _M_param.a(); }
  2503. _RealType
  2504. b() const
  2505. { return _M_param.b(); }
  2506. /**
  2507. * @brief Returns the parameter set of the distribution.
  2508. */
  2509. param_type
  2510. param() const
  2511. { return _M_param; }
  2512. /**
  2513. * @brief Sets the parameter set of the distribution.
  2514. * @param __param The new parameter set of the distribution.
  2515. */
  2516. void
  2517. param(const param_type& __param)
  2518. { _M_param = __param; }
  2519. /**
  2520. * @brief Returns the greatest lower bound value of the distribution.
  2521. */
  2522. result_type
  2523. min() const
  2524. { return std::numeric_limits<result_type>::lowest(); }
  2525. /**
  2526. * @brief Returns the least upper bound value of the distribution.
  2527. */
  2528. result_type
  2529. max() const
  2530. { return std::numeric_limits<result_type>::max(); }
  2531. /**
  2532. * @brief Generating functions.
  2533. */
  2534. template<typename _UniformRandomNumberGenerator>
  2535. result_type
  2536. operator()(_UniformRandomNumberGenerator& __urng)
  2537. { return this->operator()(__urng, _M_param); }
  2538. template<typename _UniformRandomNumberGenerator>
  2539. result_type
  2540. operator()(_UniformRandomNumberGenerator& __urng,
  2541. const param_type& __p);
  2542. template<typename _ForwardIterator,
  2543. typename _UniformRandomNumberGenerator>
  2544. void
  2545. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2546. _UniformRandomNumberGenerator& __urng)
  2547. { this->__generate(__f, __t, __urng, _M_param); }
  2548. template<typename _ForwardIterator,
  2549. typename _UniformRandomNumberGenerator>
  2550. void
  2551. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2552. _UniformRandomNumberGenerator& __urng,
  2553. const param_type& __p)
  2554. { this->__generate_impl(__f, __t, __urng, __p); }
  2555. template<typename _UniformRandomNumberGenerator>
  2556. void
  2557. __generate(result_type* __f, result_type* __t,
  2558. _UniformRandomNumberGenerator& __urng,
  2559. const param_type& __p)
  2560. { this->__generate_impl(__f, __t, __urng, __p); }
  2561. /**
  2562. * @brief Return true if two Cauchy distributions have
  2563. * the same parameters.
  2564. */
  2565. friend bool
  2566. operator==(const cauchy_distribution& __d1,
  2567. const cauchy_distribution& __d2)
  2568. { return __d1._M_param == __d2._M_param; }
  2569. private:
  2570. template<typename _ForwardIterator,
  2571. typename _UniformRandomNumberGenerator>
  2572. void
  2573. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2574. _UniformRandomNumberGenerator& __urng,
  2575. const param_type& __p);
  2576. param_type _M_param;
  2577. };
  2578. /**
  2579. * @brief Return true if two Cauchy distributions have
  2580. * different parameters.
  2581. */
  2582. template<typename _RealType>
  2583. inline bool
  2584. operator!=(const std::cauchy_distribution<_RealType>& __d1,
  2585. const std::cauchy_distribution<_RealType>& __d2)
  2586. { return !(__d1 == __d2); }
  2587. /**
  2588. * @brief Inserts a %cauchy_distribution random number distribution
  2589. * @p __x into the output stream @p __os.
  2590. *
  2591. * @param __os An output stream.
  2592. * @param __x A %cauchy_distribution random number distribution.
  2593. *
  2594. * @returns The output stream with the state of @p __x inserted or in
  2595. * an error state.
  2596. */
  2597. template<typename _RealType, typename _CharT, typename _Traits>
  2598. std::basic_ostream<_CharT, _Traits>&
  2599. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2600. const std::cauchy_distribution<_RealType>& __x);
  2601. /**
  2602. * @brief Extracts a %cauchy_distribution random number distribution
  2603. * @p __x from the input stream @p __is.
  2604. *
  2605. * @param __is An input stream.
  2606. * @param __x A %cauchy_distribution random number
  2607. * generator engine.
  2608. *
  2609. * @returns The input stream with @p __x extracted or in an error state.
  2610. */
  2611. template<typename _RealType, typename _CharT, typename _Traits>
  2612. std::basic_istream<_CharT, _Traits>&
  2613. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2614. std::cauchy_distribution<_RealType>& __x);
  2615. /**
  2616. * @brief A fisher_f_distribution random number distribution.
  2617. *
  2618. * The formula for the normal probability mass function is
  2619. * @f[
  2620. * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
  2621. * (\frac{m}{n})^{m/2} x^{(m/2)-1}
  2622. * (1 + \frac{mx}{n})^{-(m+n)/2}
  2623. * @f]
  2624. */
  2625. template<typename _RealType = double>
  2626. class fisher_f_distribution
  2627. {
  2628. static_assert(std::is_floating_point<_RealType>::value,
  2629. "result_type must be a floating point type");
  2630. public:
  2631. /** The type of the range of the distribution. */
  2632. typedef _RealType result_type;
  2633. /** Parameter type. */
  2634. struct param_type
  2635. {
  2636. typedef fisher_f_distribution<_RealType> distribution_type;
  2637. explicit
  2638. param_type(_RealType __m = _RealType(1),
  2639. _RealType __n = _RealType(1))
  2640. : _M_m(__m), _M_n(__n)
  2641. { }
  2642. _RealType
  2643. m() const
  2644. { return _M_m; }
  2645. _RealType
  2646. n() const
  2647. { return _M_n; }
  2648. friend bool
  2649. operator==(const param_type& __p1, const param_type& __p2)
  2650. { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; }
  2651. friend bool
  2652. operator!=(const param_type& __p1, const param_type& __p2)
  2653. { return !(__p1 == __p2); }
  2654. private:
  2655. _RealType _M_m;
  2656. _RealType _M_n;
  2657. };
  2658. explicit
  2659. fisher_f_distribution(_RealType __m = _RealType(1),
  2660. _RealType __n = _RealType(1))
  2661. : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
  2662. { }
  2663. explicit
  2664. fisher_f_distribution(const param_type& __p)
  2665. : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
  2666. { }
  2667. /**
  2668. * @brief Resets the distribution state.
  2669. */
  2670. void
  2671. reset()
  2672. {
  2673. _M_gd_x.reset();
  2674. _M_gd_y.reset();
  2675. }
  2676. /**
  2677. *
  2678. */
  2679. _RealType
  2680. m() const
  2681. { return _M_param.m(); }
  2682. _RealType
  2683. n() const
  2684. { return _M_param.n(); }
  2685. /**
  2686. * @brief Returns the parameter set of the distribution.
  2687. */
  2688. param_type
  2689. param() const
  2690. { return _M_param; }
  2691. /**
  2692. * @brief Sets the parameter set of the distribution.
  2693. * @param __param The new parameter set of the distribution.
  2694. */
  2695. void
  2696. param(const param_type& __param)
  2697. { _M_param = __param; }
  2698. /**
  2699. * @brief Returns the greatest lower bound value of the distribution.
  2700. */
  2701. result_type
  2702. min() const
  2703. { return result_type(0); }
  2704. /**
  2705. * @brief Returns the least upper bound value of the distribution.
  2706. */
  2707. result_type
  2708. max() const
  2709. { return std::numeric_limits<result_type>::max(); }
  2710. /**
  2711. * @brief Generating functions.
  2712. */
  2713. template<typename _UniformRandomNumberGenerator>
  2714. result_type
  2715. operator()(_UniformRandomNumberGenerator& __urng)
  2716. { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
  2717. template<typename _UniformRandomNumberGenerator>
  2718. result_type
  2719. operator()(_UniformRandomNumberGenerator& __urng,
  2720. const param_type& __p)
  2721. {
  2722. typedef typename std::gamma_distribution<result_type>::param_type
  2723. param_type;
  2724. return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
  2725. / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
  2726. }
  2727. template<typename _ForwardIterator,
  2728. typename _UniformRandomNumberGenerator>
  2729. void
  2730. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2731. _UniformRandomNumberGenerator& __urng)
  2732. { this->__generate_impl(__f, __t, __urng); }
  2733. template<typename _ForwardIterator,
  2734. typename _UniformRandomNumberGenerator>
  2735. void
  2736. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2737. _UniformRandomNumberGenerator& __urng,
  2738. const param_type& __p)
  2739. { this->__generate_impl(__f, __t, __urng, __p); }
  2740. template<typename _UniformRandomNumberGenerator>
  2741. void
  2742. __generate(result_type* __f, result_type* __t,
  2743. _UniformRandomNumberGenerator& __urng)
  2744. { this->__generate_impl(__f, __t, __urng); }
  2745. template<typename _UniformRandomNumberGenerator>
  2746. void
  2747. __generate(result_type* __f, result_type* __t,
  2748. _UniformRandomNumberGenerator& __urng,
  2749. const param_type& __p)
  2750. { this->__generate_impl(__f, __t, __urng, __p); }
  2751. /**
  2752. * @brief Return true if two Fisher f distributions have
  2753. * the same parameters and the sequences that would
  2754. * be generated are equal.
  2755. */
  2756. friend bool
  2757. operator==(const fisher_f_distribution& __d1,
  2758. const fisher_f_distribution& __d2)
  2759. { return (__d1._M_param == __d2._M_param
  2760. && __d1._M_gd_x == __d2._M_gd_x
  2761. && __d1._M_gd_y == __d2._M_gd_y); }
  2762. /**
  2763. * @brief Inserts a %fisher_f_distribution random number distribution
  2764. * @p __x into the output stream @p __os.
  2765. *
  2766. * @param __os An output stream.
  2767. * @param __x A %fisher_f_distribution random number distribution.
  2768. *
  2769. * @returns The output stream with the state of @p __x inserted or in
  2770. * an error state.
  2771. */
  2772. template<typename _RealType1, typename _CharT, typename _Traits>
  2773. friend std::basic_ostream<_CharT, _Traits>&
  2774. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2775. const std::fisher_f_distribution<_RealType1>& __x);
  2776. /**
  2777. * @brief Extracts a %fisher_f_distribution random number distribution
  2778. * @p __x from the input stream @p __is.
  2779. *
  2780. * @param __is An input stream.
  2781. * @param __x A %fisher_f_distribution random number
  2782. * generator engine.
  2783. *
  2784. * @returns The input stream with @p __x extracted or in an error state.
  2785. */
  2786. template<typename _RealType1, typename _CharT, typename _Traits>
  2787. friend std::basic_istream<_CharT, _Traits>&
  2788. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2789. std::fisher_f_distribution<_RealType1>& __x);
  2790. private:
  2791. template<typename _ForwardIterator,
  2792. typename _UniformRandomNumberGenerator>
  2793. void
  2794. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2795. _UniformRandomNumberGenerator& __urng);
  2796. template<typename _ForwardIterator,
  2797. typename _UniformRandomNumberGenerator>
  2798. void
  2799. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2800. _UniformRandomNumberGenerator& __urng,
  2801. const param_type& __p);
  2802. param_type _M_param;
  2803. std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
  2804. };
  2805. /**
  2806. * @brief Return true if two Fisher f distributions are different.
  2807. */
  2808. template<typename _RealType>
  2809. inline bool
  2810. operator!=(const std::fisher_f_distribution<_RealType>& __d1,
  2811. const std::fisher_f_distribution<_RealType>& __d2)
  2812. { return !(__d1 == __d2); }
  2813. /**
  2814. * @brief A student_t_distribution random number distribution.
  2815. *
  2816. * The formula for the normal probability mass function is:
  2817. * @f[
  2818. * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
  2819. * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
  2820. * @f]
  2821. */
  2822. template<typename _RealType = double>
  2823. class student_t_distribution
  2824. {
  2825. static_assert(std::is_floating_point<_RealType>::value,
  2826. "result_type must be a floating point type");
  2827. public:
  2828. /** The type of the range of the distribution. */
  2829. typedef _RealType result_type;
  2830. /** Parameter type. */
  2831. struct param_type
  2832. {
  2833. typedef student_t_distribution<_RealType> distribution_type;
  2834. explicit
  2835. param_type(_RealType __n = _RealType(1))
  2836. : _M_n(__n)
  2837. { }
  2838. _RealType
  2839. n() const
  2840. { return _M_n; }
  2841. friend bool
  2842. operator==(const param_type& __p1, const param_type& __p2)
  2843. { return __p1._M_n == __p2._M_n; }
  2844. friend bool
  2845. operator!=(const param_type& __p1, const param_type& __p2)
  2846. { return !(__p1 == __p2); }
  2847. private:
  2848. _RealType _M_n;
  2849. };
  2850. explicit
  2851. student_t_distribution(_RealType __n = _RealType(1))
  2852. : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
  2853. { }
  2854. explicit
  2855. student_t_distribution(const param_type& __p)
  2856. : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
  2857. { }
  2858. /**
  2859. * @brief Resets the distribution state.
  2860. */
  2861. void
  2862. reset()
  2863. {
  2864. _M_nd.reset();
  2865. _M_gd.reset();
  2866. }
  2867. /**
  2868. *
  2869. */
  2870. _RealType
  2871. n() const
  2872. { return _M_param.n(); }
  2873. /**
  2874. * @brief Returns the parameter set of the distribution.
  2875. */
  2876. param_type
  2877. param() const
  2878. { return _M_param; }
  2879. /**
  2880. * @brief Sets the parameter set of the distribution.
  2881. * @param __param The new parameter set of the distribution.
  2882. */
  2883. void
  2884. param(const param_type& __param)
  2885. { _M_param = __param; }
  2886. /**
  2887. * @brief Returns the greatest lower bound value of the distribution.
  2888. */
  2889. result_type
  2890. min() const
  2891. { return std::numeric_limits<result_type>::lowest(); }
  2892. /**
  2893. * @brief Returns the least upper bound value of the distribution.
  2894. */
  2895. result_type
  2896. max() const
  2897. { return std::numeric_limits<result_type>::max(); }
  2898. /**
  2899. * @brief Generating functions.
  2900. */
  2901. template<typename _UniformRandomNumberGenerator>
  2902. result_type
  2903. operator()(_UniformRandomNumberGenerator& __urng)
  2904. { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
  2905. template<typename _UniformRandomNumberGenerator>
  2906. result_type
  2907. operator()(_UniformRandomNumberGenerator& __urng,
  2908. const param_type& __p)
  2909. {
  2910. typedef typename std::gamma_distribution<result_type>::param_type
  2911. param_type;
  2912. const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
  2913. return _M_nd(__urng) * std::sqrt(__p.n() / __g);
  2914. }
  2915. template<typename _ForwardIterator,
  2916. typename _UniformRandomNumberGenerator>
  2917. void
  2918. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2919. _UniformRandomNumberGenerator& __urng)
  2920. { this->__generate_impl(__f, __t, __urng); }
  2921. template<typename _ForwardIterator,
  2922. typename _UniformRandomNumberGenerator>
  2923. void
  2924. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2925. _UniformRandomNumberGenerator& __urng,
  2926. const param_type& __p)
  2927. { this->__generate_impl(__f, __t, __urng, __p); }
  2928. template<typename _UniformRandomNumberGenerator>
  2929. void
  2930. __generate(result_type* __f, result_type* __t,
  2931. _UniformRandomNumberGenerator& __urng)
  2932. { this->__generate_impl(__f, __t, __urng); }
  2933. template<typename _UniformRandomNumberGenerator>
  2934. void
  2935. __generate(result_type* __f, result_type* __t,
  2936. _UniformRandomNumberGenerator& __urng,
  2937. const param_type& __p)
  2938. { this->__generate_impl(__f, __t, __urng, __p); }
  2939. /**
  2940. * @brief Return true if two Student t distributions have
  2941. * the same parameters and the sequences that would
  2942. * be generated are equal.
  2943. */
  2944. friend bool
  2945. operator==(const student_t_distribution& __d1,
  2946. const student_t_distribution& __d2)
  2947. { return (__d1._M_param == __d2._M_param
  2948. && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); }
  2949. /**
  2950. * @brief Inserts a %student_t_distribution random number distribution
  2951. * @p __x into the output stream @p __os.
  2952. *
  2953. * @param __os An output stream.
  2954. * @param __x A %student_t_distribution random number distribution.
  2955. *
  2956. * @returns The output stream with the state of @p __x inserted or in
  2957. * an error state.
  2958. */
  2959. template<typename _RealType1, typename _CharT, typename _Traits>
  2960. friend std::basic_ostream<_CharT, _Traits>&
  2961. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2962. const std::student_t_distribution<_RealType1>& __x);
  2963. /**
  2964. * @brief Extracts a %student_t_distribution random number distribution
  2965. * @p __x from the input stream @p __is.
  2966. *
  2967. * @param __is An input stream.
  2968. * @param __x A %student_t_distribution random number
  2969. * generator engine.
  2970. *
  2971. * @returns The input stream with @p __x extracted or in an error state.
  2972. */
  2973. template<typename _RealType1, typename _CharT, typename _Traits>
  2974. friend std::basic_istream<_CharT, _Traits>&
  2975. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2976. std::student_t_distribution<_RealType1>& __x);
  2977. private:
  2978. template<typename _ForwardIterator,
  2979. typename _UniformRandomNumberGenerator>
  2980. void
  2981. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2982. _UniformRandomNumberGenerator& __urng);
  2983. template<typename _ForwardIterator,
  2984. typename _UniformRandomNumberGenerator>
  2985. void
  2986. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2987. _UniformRandomNumberGenerator& __urng,
  2988. const param_type& __p);
  2989. param_type _M_param;
  2990. std::normal_distribution<result_type> _M_nd;
  2991. std::gamma_distribution<result_type> _M_gd;
  2992. };
  2993. /**
  2994. * @brief Return true if two Student t distributions are different.
  2995. */
  2996. template<typename _RealType>
  2997. inline bool
  2998. operator!=(const std::student_t_distribution<_RealType>& __d1,
  2999. const std::student_t_distribution<_RealType>& __d2)
  3000. { return !(__d1 == __d2); }
  3001. /* @} */ // group random_distributions_normal
  3002. /**
  3003. * @addtogroup random_distributions_bernoulli Bernoulli Distributions
  3004. * @ingroup random_distributions
  3005. * @{
  3006. */
  3007. /**
  3008. * @brief A Bernoulli random number distribution.
  3009. *
  3010. * Generates a sequence of true and false values with likelihood @f$p@f$
  3011. * that true will come up and @f$(1 - p)@f$ that false will appear.
  3012. */
  3013. class bernoulli_distribution
  3014. {
  3015. public:
  3016. /** The type of the range of the distribution. */
  3017. typedef bool result_type;
  3018. /** Parameter type. */
  3019. struct param_type
  3020. {
  3021. typedef bernoulli_distribution distribution_type;
  3022. explicit
  3023. param_type(double __p = 0.5)
  3024. : _M_p(__p)
  3025. {
  3026. __glibcxx_assert((_M_p >= 0.0) && (_M_p <= 1.0));
  3027. }
  3028. double
  3029. p() const
  3030. { return _M_p; }
  3031. friend bool
  3032. operator==(const param_type& __p1, const param_type& __p2)
  3033. { return __p1._M_p == __p2._M_p; }
  3034. friend bool
  3035. operator!=(const param_type& __p1, const param_type& __p2)
  3036. { return !(__p1 == __p2); }
  3037. private:
  3038. double _M_p;
  3039. };
  3040. public:
  3041. /**
  3042. * @brief Constructs a Bernoulli distribution with likelihood @p p.
  3043. *
  3044. * @param __p [IN] The likelihood of a true result being returned.
  3045. * Must be in the interval @f$[0, 1]@f$.
  3046. */
  3047. explicit
  3048. bernoulli_distribution(double __p = 0.5)
  3049. : _M_param(__p)
  3050. { }
  3051. explicit
  3052. bernoulli_distribution(const param_type& __p)
  3053. : _M_param(__p)
  3054. { }
  3055. /**
  3056. * @brief Resets the distribution state.
  3057. *
  3058. * Does nothing for a Bernoulli distribution.
  3059. */
  3060. void
  3061. reset() { }
  3062. /**
  3063. * @brief Returns the @p p parameter of the distribution.
  3064. */
  3065. double
  3066. p() const
  3067. { return _M_param.p(); }
  3068. /**
  3069. * @brief Returns the parameter set of the distribution.
  3070. */
  3071. param_type
  3072. param() const
  3073. { return _M_param; }
  3074. /**
  3075. * @brief Sets the parameter set of the distribution.
  3076. * @param __param The new parameter set of the distribution.
  3077. */
  3078. void
  3079. param(const param_type& __param)
  3080. { _M_param = __param; }
  3081. /**
  3082. * @brief Returns the greatest lower bound value of the distribution.
  3083. */
  3084. result_type
  3085. min() const
  3086. { return std::numeric_limits<result_type>::min(); }
  3087. /**
  3088. * @brief Returns the least upper bound value of the distribution.
  3089. */
  3090. result_type
  3091. max() const
  3092. { return std::numeric_limits<result_type>::max(); }
  3093. /**
  3094. * @brief Generating functions.
  3095. */
  3096. template<typename _UniformRandomNumberGenerator>
  3097. result_type
  3098. operator()(_UniformRandomNumberGenerator& __urng)
  3099. { return this->operator()(__urng, _M_param); }
  3100. template<typename _UniformRandomNumberGenerator>
  3101. result_type
  3102. operator()(_UniformRandomNumberGenerator& __urng,
  3103. const param_type& __p)
  3104. {
  3105. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  3106. __aurng(__urng);
  3107. if ((__aurng() - __aurng.min())
  3108. < __p.p() * (__aurng.max() - __aurng.min()))
  3109. return true;
  3110. return false;
  3111. }
  3112. template<typename _ForwardIterator,
  3113. typename _UniformRandomNumberGenerator>
  3114. void
  3115. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3116. _UniformRandomNumberGenerator& __urng)
  3117. { this->__generate(__f, __t, __urng, _M_param); }
  3118. template<typename _ForwardIterator,
  3119. typename _UniformRandomNumberGenerator>
  3120. void
  3121. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3122. _UniformRandomNumberGenerator& __urng, const param_type& __p)
  3123. { this->__generate_impl(__f, __t, __urng, __p); }
  3124. template<typename _UniformRandomNumberGenerator>
  3125. void
  3126. __generate(result_type* __f, result_type* __t,
  3127. _UniformRandomNumberGenerator& __urng,
  3128. const param_type& __p)
  3129. { this->__generate_impl(__f, __t, __urng, __p); }
  3130. /**
  3131. * @brief Return true if two Bernoulli distributions have
  3132. * the same parameters.
  3133. */
  3134. friend bool
  3135. operator==(const bernoulli_distribution& __d1,
  3136. const bernoulli_distribution& __d2)
  3137. { return __d1._M_param == __d2._M_param; }
  3138. private:
  3139. template<typename _ForwardIterator,
  3140. typename _UniformRandomNumberGenerator>
  3141. void
  3142. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3143. _UniformRandomNumberGenerator& __urng,
  3144. const param_type& __p);
  3145. param_type _M_param;
  3146. };
  3147. /**
  3148. * @brief Return true if two Bernoulli distributions have
  3149. * different parameters.
  3150. */
  3151. inline bool
  3152. operator!=(const std::bernoulli_distribution& __d1,
  3153. const std::bernoulli_distribution& __d2)
  3154. { return !(__d1 == __d2); }
  3155. /**
  3156. * @brief Inserts a %bernoulli_distribution random number distribution
  3157. * @p __x into the output stream @p __os.
  3158. *
  3159. * @param __os An output stream.
  3160. * @param __x A %bernoulli_distribution random number distribution.
  3161. *
  3162. * @returns The output stream with the state of @p __x inserted or in
  3163. * an error state.
  3164. */
  3165. template<typename _CharT, typename _Traits>
  3166. std::basic_ostream<_CharT, _Traits>&
  3167. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3168. const std::bernoulli_distribution& __x);
  3169. /**
  3170. * @brief Extracts a %bernoulli_distribution random number distribution
  3171. * @p __x from the input stream @p __is.
  3172. *
  3173. * @param __is An input stream.
  3174. * @param __x A %bernoulli_distribution random number generator engine.
  3175. *
  3176. * @returns The input stream with @p __x extracted or in an error state.
  3177. */
  3178. template<typename _CharT, typename _Traits>
  3179. std::basic_istream<_CharT, _Traits>&
  3180. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3181. std::bernoulli_distribution& __x)
  3182. {
  3183. double __p;
  3184. __is >> __p;
  3185. __x.param(bernoulli_distribution::param_type(__p));
  3186. return __is;
  3187. }
  3188. /**
  3189. * @brief A discrete binomial random number distribution.
  3190. *
  3191. * The formula for the binomial probability density function is
  3192. * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
  3193. * and @f$p@f$ are the parameters of the distribution.
  3194. */
  3195. template<typename _IntType = int>
  3196. class binomial_distribution
  3197. {
  3198. static_assert(std::is_integral<_IntType>::value,
  3199. "result_type must be an integral type");
  3200. public:
  3201. /** The type of the range of the distribution. */
  3202. typedef _IntType result_type;
  3203. /** Parameter type. */
  3204. struct param_type
  3205. {
  3206. typedef binomial_distribution<_IntType> distribution_type;
  3207. friend class binomial_distribution<_IntType>;
  3208. explicit
  3209. param_type(_IntType __t = _IntType(1), double __p = 0.5)
  3210. : _M_t(__t), _M_p(__p)
  3211. {
  3212. __glibcxx_assert((_M_t >= _IntType(0))
  3213. && (_M_p >= 0.0)
  3214. && (_M_p <= 1.0));
  3215. _M_initialize();
  3216. }
  3217. _IntType
  3218. t() const
  3219. { return _M_t; }
  3220. double
  3221. p() const
  3222. { return _M_p; }
  3223. friend bool
  3224. operator==(const param_type& __p1, const param_type& __p2)
  3225. { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; }
  3226. friend bool
  3227. operator!=(const param_type& __p1, const param_type& __p2)
  3228. { return !(__p1 == __p2); }
  3229. private:
  3230. void
  3231. _M_initialize();
  3232. _IntType _M_t;
  3233. double _M_p;
  3234. double _M_q;
  3235. #if _GLIBCXX_USE_C99_MATH_TR1
  3236. double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
  3237. _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
  3238. #endif
  3239. bool _M_easy;
  3240. };
  3241. // constructors and member function
  3242. explicit
  3243. binomial_distribution(_IntType __t = _IntType(1),
  3244. double __p = 0.5)
  3245. : _M_param(__t, __p), _M_nd()
  3246. { }
  3247. explicit
  3248. binomial_distribution(const param_type& __p)
  3249. : _M_param(__p), _M_nd()
  3250. { }
  3251. /**
  3252. * @brief Resets the distribution state.
  3253. */
  3254. void
  3255. reset()
  3256. { _M_nd.reset(); }
  3257. /**
  3258. * @brief Returns the distribution @p t parameter.
  3259. */
  3260. _IntType
  3261. t() const
  3262. { return _M_param.t(); }
  3263. /**
  3264. * @brief Returns the distribution @p p parameter.
  3265. */
  3266. double
  3267. p() const
  3268. { return _M_param.p(); }
  3269. /**
  3270. * @brief Returns the parameter set of the distribution.
  3271. */
  3272. param_type
  3273. param() const
  3274. { return _M_param; }
  3275. /**
  3276. * @brief Sets the parameter set of the distribution.
  3277. * @param __param The new parameter set of the distribution.
  3278. */
  3279. void
  3280. param(const param_type& __param)
  3281. { _M_param = __param; }
  3282. /**
  3283. * @brief Returns the greatest lower bound value of the distribution.
  3284. */
  3285. result_type
  3286. min() const
  3287. { return 0; }
  3288. /**
  3289. * @brief Returns the least upper bound value of the distribution.
  3290. */
  3291. result_type
  3292. max() const
  3293. { return _M_param.t(); }
  3294. /**
  3295. * @brief Generating functions.
  3296. */
  3297. template<typename _UniformRandomNumberGenerator>
  3298. result_type
  3299. operator()(_UniformRandomNumberGenerator& __urng)
  3300. { return this->operator()(__urng, _M_param); }
  3301. template<typename _UniformRandomNumberGenerator>
  3302. result_type
  3303. operator()(_UniformRandomNumberGenerator& __urng,
  3304. const param_type& __p);
  3305. template<typename _ForwardIterator,
  3306. typename _UniformRandomNumberGenerator>
  3307. void
  3308. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3309. _UniformRandomNumberGenerator& __urng)
  3310. { this->__generate(__f, __t, __urng, _M_param); }
  3311. template<typename _ForwardIterator,
  3312. typename _UniformRandomNumberGenerator>
  3313. void
  3314. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3315. _UniformRandomNumberGenerator& __urng,
  3316. const param_type& __p)
  3317. { this->__generate_impl(__f, __t, __urng, __p); }
  3318. template<typename _UniformRandomNumberGenerator>
  3319. void
  3320. __generate(result_type* __f, result_type* __t,
  3321. _UniformRandomNumberGenerator& __urng,
  3322. const param_type& __p)
  3323. { this->__generate_impl(__f, __t, __urng, __p); }
  3324. /**
  3325. * @brief Return true if two binomial distributions have
  3326. * the same parameters and the sequences that would
  3327. * be generated are equal.
  3328. */
  3329. friend bool
  3330. operator==(const binomial_distribution& __d1,
  3331. const binomial_distribution& __d2)
  3332. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  3333. { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; }
  3334. #else
  3335. { return __d1._M_param == __d2._M_param; }
  3336. #endif
  3337. /**
  3338. * @brief Inserts a %binomial_distribution random number distribution
  3339. * @p __x into the output stream @p __os.
  3340. *
  3341. * @param __os An output stream.
  3342. * @param __x A %binomial_distribution random number distribution.
  3343. *
  3344. * @returns The output stream with the state of @p __x inserted or in
  3345. * an error state.
  3346. */
  3347. template<typename _IntType1,
  3348. typename _CharT, typename _Traits>
  3349. friend std::basic_ostream<_CharT, _Traits>&
  3350. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3351. const std::binomial_distribution<_IntType1>& __x);
  3352. /**
  3353. * @brief Extracts a %binomial_distribution random number distribution
  3354. * @p __x from the input stream @p __is.
  3355. *
  3356. * @param __is An input stream.
  3357. * @param __x A %binomial_distribution random number generator engine.
  3358. *
  3359. * @returns The input stream with @p __x extracted or in an error
  3360. * state.
  3361. */
  3362. template<typename _IntType1,
  3363. typename _CharT, typename _Traits>
  3364. friend std::basic_istream<_CharT, _Traits>&
  3365. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3366. std::binomial_distribution<_IntType1>& __x);
  3367. private:
  3368. template<typename _ForwardIterator,
  3369. typename _UniformRandomNumberGenerator>
  3370. void
  3371. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3372. _UniformRandomNumberGenerator& __urng,
  3373. const param_type& __p);
  3374. template<typename _UniformRandomNumberGenerator>
  3375. result_type
  3376. _M_waiting(_UniformRandomNumberGenerator& __urng,
  3377. _IntType __t, double __q);
  3378. param_type _M_param;
  3379. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  3380. std::normal_distribution<double> _M_nd;
  3381. };
  3382. /**
  3383. * @brief Return true if two binomial distributions are different.
  3384. */
  3385. template<typename _IntType>
  3386. inline bool
  3387. operator!=(const std::binomial_distribution<_IntType>& __d1,
  3388. const std::binomial_distribution<_IntType>& __d2)
  3389. { return !(__d1 == __d2); }
  3390. /**
  3391. * @brief A discrete geometric random number distribution.
  3392. *
  3393. * The formula for the geometric probability density function is
  3394. * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
  3395. * distribution.
  3396. */
  3397. template<typename _IntType = int>
  3398. class geometric_distribution
  3399. {
  3400. static_assert(std::is_integral<_IntType>::value,
  3401. "result_type must be an integral type");
  3402. public:
  3403. /** The type of the range of the distribution. */
  3404. typedef _IntType result_type;
  3405. /** Parameter type. */
  3406. struct param_type
  3407. {
  3408. typedef geometric_distribution<_IntType> distribution_type;
  3409. friend class geometric_distribution<_IntType>;
  3410. explicit
  3411. param_type(double __p = 0.5)
  3412. : _M_p(__p)
  3413. {
  3414. __glibcxx_assert((_M_p > 0.0) && (_M_p < 1.0));
  3415. _M_initialize();
  3416. }
  3417. double
  3418. p() const
  3419. { return _M_p; }
  3420. friend bool
  3421. operator==(const param_type& __p1, const param_type& __p2)
  3422. { return __p1._M_p == __p2._M_p; }
  3423. friend bool
  3424. operator!=(const param_type& __p1, const param_type& __p2)
  3425. { return !(__p1 == __p2); }
  3426. private:
  3427. void
  3428. _M_initialize()
  3429. { _M_log_1_p = std::log(1.0 - _M_p); }
  3430. double _M_p;
  3431. double _M_log_1_p;
  3432. };
  3433. // constructors and member function
  3434. explicit
  3435. geometric_distribution(double __p = 0.5)
  3436. : _M_param(__p)
  3437. { }
  3438. explicit
  3439. geometric_distribution(const param_type& __p)
  3440. : _M_param(__p)
  3441. { }
  3442. /**
  3443. * @brief Resets the distribution state.
  3444. *
  3445. * Does nothing for the geometric distribution.
  3446. */
  3447. void
  3448. reset() { }
  3449. /**
  3450. * @brief Returns the distribution parameter @p p.
  3451. */
  3452. double
  3453. p() const
  3454. { return _M_param.p(); }
  3455. /**
  3456. * @brief Returns the parameter set of the distribution.
  3457. */
  3458. param_type
  3459. param() const
  3460. { return _M_param; }
  3461. /**
  3462. * @brief Sets the parameter set of the distribution.
  3463. * @param __param The new parameter set of the distribution.
  3464. */
  3465. void
  3466. param(const param_type& __param)
  3467. { _M_param = __param; }
  3468. /**
  3469. * @brief Returns the greatest lower bound value of the distribution.
  3470. */
  3471. result_type
  3472. min() const
  3473. { return 0; }
  3474. /**
  3475. * @brief Returns the least upper bound value of the distribution.
  3476. */
  3477. result_type
  3478. max() const
  3479. { return std::numeric_limits<result_type>::max(); }
  3480. /**
  3481. * @brief Generating functions.
  3482. */
  3483. template<typename _UniformRandomNumberGenerator>
  3484. result_type
  3485. operator()(_UniformRandomNumberGenerator& __urng)
  3486. { return this->operator()(__urng, _M_param); }
  3487. template<typename _UniformRandomNumberGenerator>
  3488. result_type
  3489. operator()(_UniformRandomNumberGenerator& __urng,
  3490. const param_type& __p);
  3491. template<typename _ForwardIterator,
  3492. typename _UniformRandomNumberGenerator>
  3493. void
  3494. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3495. _UniformRandomNumberGenerator& __urng)
  3496. { this->__generate(__f, __t, __urng, _M_param); }
  3497. template<typename _ForwardIterator,
  3498. typename _UniformRandomNumberGenerator>
  3499. void
  3500. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3501. _UniformRandomNumberGenerator& __urng,
  3502. const param_type& __p)
  3503. { this->__generate_impl(__f, __t, __urng, __p); }
  3504. template<typename _UniformRandomNumberGenerator>
  3505. void
  3506. __generate(result_type* __f, result_type* __t,
  3507. _UniformRandomNumberGenerator& __urng,
  3508. const param_type& __p)
  3509. { this->__generate_impl(__f, __t, __urng, __p); }
  3510. /**
  3511. * @brief Return true if two geometric distributions have
  3512. * the same parameters.
  3513. */
  3514. friend bool
  3515. operator==(const geometric_distribution& __d1,
  3516. const geometric_distribution& __d2)
  3517. { return __d1._M_param == __d2._M_param; }
  3518. private:
  3519. template<typename _ForwardIterator,
  3520. typename _UniformRandomNumberGenerator>
  3521. void
  3522. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3523. _UniformRandomNumberGenerator& __urng,
  3524. const param_type& __p);
  3525. param_type _M_param;
  3526. };
  3527. /**
  3528. * @brief Return true if two geometric distributions have
  3529. * different parameters.
  3530. */
  3531. template<typename _IntType>
  3532. inline bool
  3533. operator!=(const std::geometric_distribution<_IntType>& __d1,
  3534. const std::geometric_distribution<_IntType>& __d2)
  3535. { return !(__d1 == __d2); }
  3536. /**
  3537. * @brief Inserts a %geometric_distribution random number distribution
  3538. * @p __x into the output stream @p __os.
  3539. *
  3540. * @param __os An output stream.
  3541. * @param __x A %geometric_distribution random number distribution.
  3542. *
  3543. * @returns The output stream with the state of @p __x inserted or in
  3544. * an error state.
  3545. */
  3546. template<typename _IntType,
  3547. typename _CharT, typename _Traits>
  3548. std::basic_ostream<_CharT, _Traits>&
  3549. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3550. const std::geometric_distribution<_IntType>& __x);
  3551. /**
  3552. * @brief Extracts a %geometric_distribution random number distribution
  3553. * @p __x from the input stream @p __is.
  3554. *
  3555. * @param __is An input stream.
  3556. * @param __x A %geometric_distribution random number generator engine.
  3557. *
  3558. * @returns The input stream with @p __x extracted or in an error state.
  3559. */
  3560. template<typename _IntType,
  3561. typename _CharT, typename _Traits>
  3562. std::basic_istream<_CharT, _Traits>&
  3563. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3564. std::geometric_distribution<_IntType>& __x);
  3565. /**
  3566. * @brief A negative_binomial_distribution random number distribution.
  3567. *
  3568. * The formula for the negative binomial probability mass function is
  3569. * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
  3570. * and @f$p@f$ are the parameters of the distribution.
  3571. */
  3572. template<typename _IntType = int>
  3573. class negative_binomial_distribution
  3574. {
  3575. static_assert(std::is_integral<_IntType>::value,
  3576. "result_type must be an integral type");
  3577. public:
  3578. /** The type of the range of the distribution. */
  3579. typedef _IntType result_type;
  3580. /** Parameter type. */
  3581. struct param_type
  3582. {
  3583. typedef negative_binomial_distribution<_IntType> distribution_type;
  3584. explicit
  3585. param_type(_IntType __k = 1, double __p = 0.5)
  3586. : _M_k(__k), _M_p(__p)
  3587. {
  3588. __glibcxx_assert((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0));
  3589. }
  3590. _IntType
  3591. k() const
  3592. { return _M_k; }
  3593. double
  3594. p() const
  3595. { return _M_p; }
  3596. friend bool
  3597. operator==(const param_type& __p1, const param_type& __p2)
  3598. { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; }
  3599. friend bool
  3600. operator!=(const param_type& __p1, const param_type& __p2)
  3601. { return !(__p1 == __p2); }
  3602. private:
  3603. _IntType _M_k;
  3604. double _M_p;
  3605. };
  3606. explicit
  3607. negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
  3608. : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p)
  3609. { }
  3610. explicit
  3611. negative_binomial_distribution(const param_type& __p)
  3612. : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p())
  3613. { }
  3614. /**
  3615. * @brief Resets the distribution state.
  3616. */
  3617. void
  3618. reset()
  3619. { _M_gd.reset(); }
  3620. /**
  3621. * @brief Return the @f$k@f$ parameter of the distribution.
  3622. */
  3623. _IntType
  3624. k() const
  3625. { return _M_param.k(); }
  3626. /**
  3627. * @brief Return the @f$p@f$ parameter of the distribution.
  3628. */
  3629. double
  3630. p() const
  3631. { return _M_param.p(); }
  3632. /**
  3633. * @brief Returns the parameter set of the distribution.
  3634. */
  3635. param_type
  3636. param() const
  3637. { return _M_param; }
  3638. /**
  3639. * @brief Sets the parameter set of the distribution.
  3640. * @param __param The new parameter set of the distribution.
  3641. */
  3642. void
  3643. param(const param_type& __param)
  3644. { _M_param = __param; }
  3645. /**
  3646. * @brief Returns the greatest lower bound value of the distribution.
  3647. */
  3648. result_type
  3649. min() const
  3650. { return result_type(0); }
  3651. /**
  3652. * @brief Returns the least upper bound value of the distribution.
  3653. */
  3654. result_type
  3655. max() const
  3656. { return std::numeric_limits<result_type>::max(); }
  3657. /**
  3658. * @brief Generating functions.
  3659. */
  3660. template<typename _UniformRandomNumberGenerator>
  3661. result_type
  3662. operator()(_UniformRandomNumberGenerator& __urng);
  3663. template<typename _UniformRandomNumberGenerator>
  3664. result_type
  3665. operator()(_UniformRandomNumberGenerator& __urng,
  3666. const param_type& __p);
  3667. template<typename _ForwardIterator,
  3668. typename _UniformRandomNumberGenerator>
  3669. void
  3670. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3671. _UniformRandomNumberGenerator& __urng)
  3672. { this->__generate_impl(__f, __t, __urng); }
  3673. template<typename _ForwardIterator,
  3674. typename _UniformRandomNumberGenerator>
  3675. void
  3676. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3677. _UniformRandomNumberGenerator& __urng,
  3678. const param_type& __p)
  3679. { this->__generate_impl(__f, __t, __urng, __p); }
  3680. template<typename _UniformRandomNumberGenerator>
  3681. void
  3682. __generate(result_type* __f, result_type* __t,
  3683. _UniformRandomNumberGenerator& __urng)
  3684. { this->__generate_impl(__f, __t, __urng); }
  3685. template<typename _UniformRandomNumberGenerator>
  3686. void
  3687. __generate(result_type* __f, result_type* __t,
  3688. _UniformRandomNumberGenerator& __urng,
  3689. const param_type& __p)
  3690. { this->__generate_impl(__f, __t, __urng, __p); }
  3691. /**
  3692. * @brief Return true if two negative binomial distributions have
  3693. * the same parameters and the sequences that would be
  3694. * generated are equal.
  3695. */
  3696. friend bool
  3697. operator==(const negative_binomial_distribution& __d1,
  3698. const negative_binomial_distribution& __d2)
  3699. { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; }
  3700. /**
  3701. * @brief Inserts a %negative_binomial_distribution random
  3702. * number distribution @p __x into the output stream @p __os.
  3703. *
  3704. * @param __os An output stream.
  3705. * @param __x A %negative_binomial_distribution random number
  3706. * distribution.
  3707. *
  3708. * @returns The output stream with the state of @p __x inserted or in
  3709. * an error state.
  3710. */
  3711. template<typename _IntType1, typename _CharT, typename _Traits>
  3712. friend std::basic_ostream<_CharT, _Traits>&
  3713. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3714. const std::negative_binomial_distribution<_IntType1>& __x);
  3715. /**
  3716. * @brief Extracts a %negative_binomial_distribution random number
  3717. * distribution @p __x from the input stream @p __is.
  3718. *
  3719. * @param __is An input stream.
  3720. * @param __x A %negative_binomial_distribution random number
  3721. * generator engine.
  3722. *
  3723. * @returns The input stream with @p __x extracted or in an error state.
  3724. */
  3725. template<typename _IntType1, typename _CharT, typename _Traits>
  3726. friend std::basic_istream<_CharT, _Traits>&
  3727. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3728. std::negative_binomial_distribution<_IntType1>& __x);
  3729. private:
  3730. template<typename _ForwardIterator,
  3731. typename _UniformRandomNumberGenerator>
  3732. void
  3733. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3734. _UniformRandomNumberGenerator& __urng);
  3735. template<typename _ForwardIterator,
  3736. typename _UniformRandomNumberGenerator>
  3737. void
  3738. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3739. _UniformRandomNumberGenerator& __urng,
  3740. const param_type& __p);
  3741. param_type _M_param;
  3742. std::gamma_distribution<double> _M_gd;
  3743. };
  3744. /**
  3745. * @brief Return true if two negative binomial distributions are different.
  3746. */
  3747. template<typename _IntType>
  3748. inline bool
  3749. operator!=(const std::negative_binomial_distribution<_IntType>& __d1,
  3750. const std::negative_binomial_distribution<_IntType>& __d2)
  3751. { return !(__d1 == __d2); }
  3752. /* @} */ // group random_distributions_bernoulli
  3753. /**
  3754. * @addtogroup random_distributions_poisson Poisson Distributions
  3755. * @ingroup random_distributions
  3756. * @{
  3757. */
  3758. /**
  3759. * @brief A discrete Poisson random number distribution.
  3760. *
  3761. * The formula for the Poisson probability density function is
  3762. * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
  3763. * parameter of the distribution.
  3764. */
  3765. template<typename _IntType = int>
  3766. class poisson_distribution
  3767. {
  3768. static_assert(std::is_integral<_IntType>::value,
  3769. "result_type must be an integral type");
  3770. public:
  3771. /** The type of the range of the distribution. */
  3772. typedef _IntType result_type;
  3773. /** Parameter type. */
  3774. struct param_type
  3775. {
  3776. typedef poisson_distribution<_IntType> distribution_type;
  3777. friend class poisson_distribution<_IntType>;
  3778. explicit
  3779. param_type(double __mean = 1.0)
  3780. : _M_mean(__mean)
  3781. {
  3782. __glibcxx_assert(_M_mean > 0.0);
  3783. _M_initialize();
  3784. }
  3785. double
  3786. mean() const
  3787. { return _M_mean; }
  3788. friend bool
  3789. operator==(const param_type& __p1, const param_type& __p2)
  3790. { return __p1._M_mean == __p2._M_mean; }
  3791. friend bool
  3792. operator!=(const param_type& __p1, const param_type& __p2)
  3793. { return !(__p1 == __p2); }
  3794. private:
  3795. // Hosts either log(mean) or the threshold of the simple method.
  3796. void
  3797. _M_initialize();
  3798. double _M_mean;
  3799. double _M_lm_thr;
  3800. #if _GLIBCXX_USE_C99_MATH_TR1
  3801. double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
  3802. #endif
  3803. };
  3804. // constructors and member function
  3805. explicit
  3806. poisson_distribution(double __mean = 1.0)
  3807. : _M_param(__mean), _M_nd()
  3808. { }
  3809. explicit
  3810. poisson_distribution(const param_type& __p)
  3811. : _M_param(__p), _M_nd()
  3812. { }
  3813. /**
  3814. * @brief Resets the distribution state.
  3815. */
  3816. void
  3817. reset()
  3818. { _M_nd.reset(); }
  3819. /**
  3820. * @brief Returns the distribution parameter @p mean.
  3821. */
  3822. double
  3823. mean() const
  3824. { return _M_param.mean(); }
  3825. /**
  3826. * @brief Returns the parameter set of the distribution.
  3827. */
  3828. param_type
  3829. param() const
  3830. { return _M_param; }
  3831. /**
  3832. * @brief Sets the parameter set of the distribution.
  3833. * @param __param The new parameter set of the distribution.
  3834. */
  3835. void
  3836. param(const param_type& __param)
  3837. { _M_param = __param; }
  3838. /**
  3839. * @brief Returns the greatest lower bound value of the distribution.
  3840. */
  3841. result_type
  3842. min() const
  3843. { return 0; }
  3844. /**
  3845. * @brief Returns the least upper bound value of the distribution.
  3846. */
  3847. result_type
  3848. max() const
  3849. { return std::numeric_limits<result_type>::max(); }
  3850. /**
  3851. * @brief Generating functions.
  3852. */
  3853. template<typename _UniformRandomNumberGenerator>
  3854. result_type
  3855. operator()(_UniformRandomNumberGenerator& __urng)
  3856. { return this->operator()(__urng, _M_param); }
  3857. template<typename _UniformRandomNumberGenerator>
  3858. result_type
  3859. operator()(_UniformRandomNumberGenerator& __urng,
  3860. const param_type& __p);
  3861. template<typename _ForwardIterator,
  3862. typename _UniformRandomNumberGenerator>
  3863. void
  3864. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3865. _UniformRandomNumberGenerator& __urng)
  3866. { this->__generate(__f, __t, __urng, _M_param); }
  3867. template<typename _ForwardIterator,
  3868. typename _UniformRandomNumberGenerator>
  3869. void
  3870. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3871. _UniformRandomNumberGenerator& __urng,
  3872. const param_type& __p)
  3873. { this->__generate_impl(__f, __t, __urng, __p); }
  3874. template<typename _UniformRandomNumberGenerator>
  3875. void
  3876. __generate(result_type* __f, result_type* __t,
  3877. _UniformRandomNumberGenerator& __urng,
  3878. const param_type& __p)
  3879. { this->__generate_impl(__f, __t, __urng, __p); }
  3880. /**
  3881. * @brief Return true if two Poisson distributions have the same
  3882. * parameters and the sequences that would be generated
  3883. * are equal.
  3884. */
  3885. friend bool
  3886. operator==(const poisson_distribution& __d1,
  3887. const poisson_distribution& __d2)
  3888. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  3889. { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; }
  3890. #else
  3891. { return __d1._M_param == __d2._M_param; }
  3892. #endif
  3893. /**
  3894. * @brief Inserts a %poisson_distribution random number distribution
  3895. * @p __x into the output stream @p __os.
  3896. *
  3897. * @param __os An output stream.
  3898. * @param __x A %poisson_distribution random number distribution.
  3899. *
  3900. * @returns The output stream with the state of @p __x inserted or in
  3901. * an error state.
  3902. */
  3903. template<typename _IntType1, typename _CharT, typename _Traits>
  3904. friend std::basic_ostream<_CharT, _Traits>&
  3905. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3906. const std::poisson_distribution<_IntType1>& __x);
  3907. /**
  3908. * @brief Extracts a %poisson_distribution random number distribution
  3909. * @p __x from the input stream @p __is.
  3910. *
  3911. * @param __is An input stream.
  3912. * @param __x A %poisson_distribution random number generator engine.
  3913. *
  3914. * @returns The input stream with @p __x extracted or in an error
  3915. * state.
  3916. */
  3917. template<typename _IntType1, typename _CharT, typename _Traits>
  3918. friend std::basic_istream<_CharT, _Traits>&
  3919. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3920. std::poisson_distribution<_IntType1>& __x);
  3921. private:
  3922. template<typename _ForwardIterator,
  3923. typename _UniformRandomNumberGenerator>
  3924. void
  3925. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3926. _UniformRandomNumberGenerator& __urng,
  3927. const param_type& __p);
  3928. param_type _M_param;
  3929. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  3930. std::normal_distribution<double> _M_nd;
  3931. };
  3932. /**
  3933. * @brief Return true if two Poisson distributions are different.
  3934. */
  3935. template<typename _IntType>
  3936. inline bool
  3937. operator!=(const std::poisson_distribution<_IntType>& __d1,
  3938. const std::poisson_distribution<_IntType>& __d2)
  3939. { return !(__d1 == __d2); }
  3940. /**
  3941. * @brief An exponential continuous distribution for random numbers.
  3942. *
  3943. * The formula for the exponential probability density function is
  3944. * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
  3945. *
  3946. * <table border=1 cellpadding=10 cellspacing=0>
  3947. * <caption align=top>Distribution Statistics</caption>
  3948. * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
  3949. * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
  3950. * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
  3951. * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
  3952. * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
  3953. * </table>
  3954. */
  3955. template<typename _RealType = double>
  3956. class exponential_distribution
  3957. {
  3958. static_assert(std::is_floating_point<_RealType>::value,
  3959. "result_type must be a floating point type");
  3960. public:
  3961. /** The type of the range of the distribution. */
  3962. typedef _RealType result_type;
  3963. /** Parameter type. */
  3964. struct param_type
  3965. {
  3966. typedef exponential_distribution<_RealType> distribution_type;
  3967. explicit
  3968. param_type(_RealType __lambda = _RealType(1))
  3969. : _M_lambda(__lambda)
  3970. {
  3971. __glibcxx_assert(_M_lambda > _RealType(0));
  3972. }
  3973. _RealType
  3974. lambda() const
  3975. { return _M_lambda; }
  3976. friend bool
  3977. operator==(const param_type& __p1, const param_type& __p2)
  3978. { return __p1._M_lambda == __p2._M_lambda; }
  3979. friend bool
  3980. operator!=(const param_type& __p1, const param_type& __p2)
  3981. { return !(__p1 == __p2); }
  3982. private:
  3983. _RealType _M_lambda;
  3984. };
  3985. public:
  3986. /**
  3987. * @brief Constructs an exponential distribution with inverse scale
  3988. * parameter @f$\lambda@f$.
  3989. */
  3990. explicit
  3991. exponential_distribution(const result_type& __lambda = result_type(1))
  3992. : _M_param(__lambda)
  3993. { }
  3994. explicit
  3995. exponential_distribution(const param_type& __p)
  3996. : _M_param(__p)
  3997. { }
  3998. /**
  3999. * @brief Resets the distribution state.
  4000. *
  4001. * Has no effect on exponential distributions.
  4002. */
  4003. void
  4004. reset() { }
  4005. /**
  4006. * @brief Returns the inverse scale parameter of the distribution.
  4007. */
  4008. _RealType
  4009. lambda() const
  4010. { return _M_param.lambda(); }
  4011. /**
  4012. * @brief Returns the parameter set of the distribution.
  4013. */
  4014. param_type
  4015. param() const
  4016. { return _M_param; }
  4017. /**
  4018. * @brief Sets the parameter set of the distribution.
  4019. * @param __param The new parameter set of the distribution.
  4020. */
  4021. void
  4022. param(const param_type& __param)
  4023. { _M_param = __param; }
  4024. /**
  4025. * @brief Returns the greatest lower bound value of the distribution.
  4026. */
  4027. result_type
  4028. min() const
  4029. { return result_type(0); }
  4030. /**
  4031. * @brief Returns the least upper bound value of the distribution.
  4032. */
  4033. result_type
  4034. max() const
  4035. { return std::numeric_limits<result_type>::max(); }
  4036. /**
  4037. * @brief Generating functions.
  4038. */
  4039. template<typename _UniformRandomNumberGenerator>
  4040. result_type
  4041. operator()(_UniformRandomNumberGenerator& __urng)
  4042. { return this->operator()(__urng, _M_param); }
  4043. template<typename _UniformRandomNumberGenerator>
  4044. result_type
  4045. operator()(_UniformRandomNumberGenerator& __urng,
  4046. const param_type& __p)
  4047. {
  4048. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  4049. __aurng(__urng);
  4050. return -std::log(result_type(1) - __aurng()) / __p.lambda();
  4051. }
  4052. template<typename _ForwardIterator,
  4053. typename _UniformRandomNumberGenerator>
  4054. void
  4055. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4056. _UniformRandomNumberGenerator& __urng)
  4057. { this->__generate(__f, __t, __urng, _M_param); }
  4058. template<typename _ForwardIterator,
  4059. typename _UniformRandomNumberGenerator>
  4060. void
  4061. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4062. _UniformRandomNumberGenerator& __urng,
  4063. const param_type& __p)
  4064. { this->__generate_impl(__f, __t, __urng, __p); }
  4065. template<typename _UniformRandomNumberGenerator>
  4066. void
  4067. __generate(result_type* __f, result_type* __t,
  4068. _UniformRandomNumberGenerator& __urng,
  4069. const param_type& __p)
  4070. { this->__generate_impl(__f, __t, __urng, __p); }
  4071. /**
  4072. * @brief Return true if two exponential distributions have the same
  4073. * parameters.
  4074. */
  4075. friend bool
  4076. operator==(const exponential_distribution& __d1,
  4077. const exponential_distribution& __d2)
  4078. { return __d1._M_param == __d2._M_param; }
  4079. private:
  4080. template<typename _ForwardIterator,
  4081. typename _UniformRandomNumberGenerator>
  4082. void
  4083. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4084. _UniformRandomNumberGenerator& __urng,
  4085. const param_type& __p);
  4086. param_type _M_param;
  4087. };
  4088. /**
  4089. * @brief Return true if two exponential distributions have different
  4090. * parameters.
  4091. */
  4092. template<typename _RealType>
  4093. inline bool
  4094. operator!=(const std::exponential_distribution<_RealType>& __d1,
  4095. const std::exponential_distribution<_RealType>& __d2)
  4096. { return !(__d1 == __d2); }
  4097. /**
  4098. * @brief Inserts a %exponential_distribution random number distribution
  4099. * @p __x into the output stream @p __os.
  4100. *
  4101. * @param __os An output stream.
  4102. * @param __x A %exponential_distribution random number distribution.
  4103. *
  4104. * @returns The output stream with the state of @p __x inserted or in
  4105. * an error state.
  4106. */
  4107. template<typename _RealType, typename _CharT, typename _Traits>
  4108. std::basic_ostream<_CharT, _Traits>&
  4109. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4110. const std::exponential_distribution<_RealType>& __x);
  4111. /**
  4112. * @brief Extracts a %exponential_distribution random number distribution
  4113. * @p __x from the input stream @p __is.
  4114. *
  4115. * @param __is An input stream.
  4116. * @param __x A %exponential_distribution random number
  4117. * generator engine.
  4118. *
  4119. * @returns The input stream with @p __x extracted or in an error state.
  4120. */
  4121. template<typename _RealType, typename _CharT, typename _Traits>
  4122. std::basic_istream<_CharT, _Traits>&
  4123. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4124. std::exponential_distribution<_RealType>& __x);
  4125. /**
  4126. * @brief A weibull_distribution random number distribution.
  4127. *
  4128. * The formula for the normal probability density function is:
  4129. * @f[
  4130. * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
  4131. * \exp{(-(\frac{x}{\beta})^\alpha)}
  4132. * @f]
  4133. */
  4134. template<typename _RealType = double>
  4135. class weibull_distribution
  4136. {
  4137. static_assert(std::is_floating_point<_RealType>::value,
  4138. "result_type must be a floating point type");
  4139. public:
  4140. /** The type of the range of the distribution. */
  4141. typedef _RealType result_type;
  4142. /** Parameter type. */
  4143. struct param_type
  4144. {
  4145. typedef weibull_distribution<_RealType> distribution_type;
  4146. explicit
  4147. param_type(_RealType __a = _RealType(1),
  4148. _RealType __b = _RealType(1))
  4149. : _M_a(__a), _M_b(__b)
  4150. { }
  4151. _RealType
  4152. a() const
  4153. { return _M_a; }
  4154. _RealType
  4155. b() const
  4156. { return _M_b; }
  4157. friend bool
  4158. operator==(const param_type& __p1, const param_type& __p2)
  4159. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  4160. friend bool
  4161. operator!=(const param_type& __p1, const param_type& __p2)
  4162. { return !(__p1 == __p2); }
  4163. private:
  4164. _RealType _M_a;
  4165. _RealType _M_b;
  4166. };
  4167. explicit
  4168. weibull_distribution(_RealType __a = _RealType(1),
  4169. _RealType __b = _RealType(1))
  4170. : _M_param(__a, __b)
  4171. { }
  4172. explicit
  4173. weibull_distribution(const param_type& __p)
  4174. : _M_param(__p)
  4175. { }
  4176. /**
  4177. * @brief Resets the distribution state.
  4178. */
  4179. void
  4180. reset()
  4181. { }
  4182. /**
  4183. * @brief Return the @f$a@f$ parameter of the distribution.
  4184. */
  4185. _RealType
  4186. a() const
  4187. { return _M_param.a(); }
  4188. /**
  4189. * @brief Return the @f$b@f$ parameter of the distribution.
  4190. */
  4191. _RealType
  4192. b() const
  4193. { return _M_param.b(); }
  4194. /**
  4195. * @brief Returns the parameter set of the distribution.
  4196. */
  4197. param_type
  4198. param() const
  4199. { return _M_param; }
  4200. /**
  4201. * @brief Sets the parameter set of the distribution.
  4202. * @param __param The new parameter set of the distribution.
  4203. */
  4204. void
  4205. param(const param_type& __param)
  4206. { _M_param = __param; }
  4207. /**
  4208. * @brief Returns the greatest lower bound value of the distribution.
  4209. */
  4210. result_type
  4211. min() const
  4212. { return result_type(0); }
  4213. /**
  4214. * @brief Returns the least upper bound value of the distribution.
  4215. */
  4216. result_type
  4217. max() const
  4218. { return std::numeric_limits<result_type>::max(); }
  4219. /**
  4220. * @brief Generating functions.
  4221. */
  4222. template<typename _UniformRandomNumberGenerator>
  4223. result_type
  4224. operator()(_UniformRandomNumberGenerator& __urng)
  4225. { return this->operator()(__urng, _M_param); }
  4226. template<typename _UniformRandomNumberGenerator>
  4227. result_type
  4228. operator()(_UniformRandomNumberGenerator& __urng,
  4229. const param_type& __p);
  4230. template<typename _ForwardIterator,
  4231. typename _UniformRandomNumberGenerator>
  4232. void
  4233. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4234. _UniformRandomNumberGenerator& __urng)
  4235. { this->__generate(__f, __t, __urng, _M_param); }
  4236. template<typename _ForwardIterator,
  4237. typename _UniformRandomNumberGenerator>
  4238. void
  4239. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4240. _UniformRandomNumberGenerator& __urng,
  4241. const param_type& __p)
  4242. { this->__generate_impl(__f, __t, __urng, __p); }
  4243. template<typename _UniformRandomNumberGenerator>
  4244. void
  4245. __generate(result_type* __f, result_type* __t,
  4246. _UniformRandomNumberGenerator& __urng,
  4247. const param_type& __p)
  4248. { this->__generate_impl(__f, __t, __urng, __p); }
  4249. /**
  4250. * @brief Return true if two Weibull distributions have the same
  4251. * parameters.
  4252. */
  4253. friend bool
  4254. operator==(const weibull_distribution& __d1,
  4255. const weibull_distribution& __d2)
  4256. { return __d1._M_param == __d2._M_param; }
  4257. private:
  4258. template<typename _ForwardIterator,
  4259. typename _UniformRandomNumberGenerator>
  4260. void
  4261. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4262. _UniformRandomNumberGenerator& __urng,
  4263. const param_type& __p);
  4264. param_type _M_param;
  4265. };
  4266. /**
  4267. * @brief Return true if two Weibull distributions have different
  4268. * parameters.
  4269. */
  4270. template<typename _RealType>
  4271. inline bool
  4272. operator!=(const std::weibull_distribution<_RealType>& __d1,
  4273. const std::weibull_distribution<_RealType>& __d2)
  4274. { return !(__d1 == __d2); }
  4275. /**
  4276. * @brief Inserts a %weibull_distribution random number distribution
  4277. * @p __x into the output stream @p __os.
  4278. *
  4279. * @param __os An output stream.
  4280. * @param __x A %weibull_distribution random number distribution.
  4281. *
  4282. * @returns The output stream with the state of @p __x inserted or in
  4283. * an error state.
  4284. */
  4285. template<typename _RealType, typename _CharT, typename _Traits>
  4286. std::basic_ostream<_CharT, _Traits>&
  4287. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4288. const std::weibull_distribution<_RealType>& __x);
  4289. /**
  4290. * @brief Extracts a %weibull_distribution random number distribution
  4291. * @p __x from the input stream @p __is.
  4292. *
  4293. * @param __is An input stream.
  4294. * @param __x A %weibull_distribution random number
  4295. * generator engine.
  4296. *
  4297. * @returns The input stream with @p __x extracted or in an error state.
  4298. */
  4299. template<typename _RealType, typename _CharT, typename _Traits>
  4300. std::basic_istream<_CharT, _Traits>&
  4301. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4302. std::weibull_distribution<_RealType>& __x);
  4303. /**
  4304. * @brief A extreme_value_distribution random number distribution.
  4305. *
  4306. * The formula for the normal probability mass function is
  4307. * @f[
  4308. * p(x|a,b) = \frac{1}{b}
  4309. * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
  4310. * @f]
  4311. */
  4312. template<typename _RealType = double>
  4313. class extreme_value_distribution
  4314. {
  4315. static_assert(std::is_floating_point<_RealType>::value,
  4316. "result_type must be a floating point type");
  4317. public:
  4318. /** The type of the range of the distribution. */
  4319. typedef _RealType result_type;
  4320. /** Parameter type. */
  4321. struct param_type
  4322. {
  4323. typedef extreme_value_distribution<_RealType> distribution_type;
  4324. explicit
  4325. param_type(_RealType __a = _RealType(0),
  4326. _RealType __b = _RealType(1))
  4327. : _M_a(__a), _M_b(__b)
  4328. { }
  4329. _RealType
  4330. a() const
  4331. { return _M_a; }
  4332. _RealType
  4333. b() const
  4334. { return _M_b; }
  4335. friend bool
  4336. operator==(const param_type& __p1, const param_type& __p2)
  4337. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  4338. friend bool
  4339. operator!=(const param_type& __p1, const param_type& __p2)
  4340. { return !(__p1 == __p2); }
  4341. private:
  4342. _RealType _M_a;
  4343. _RealType _M_b;
  4344. };
  4345. explicit
  4346. extreme_value_distribution(_RealType __a = _RealType(0),
  4347. _RealType __b = _RealType(1))
  4348. : _M_param(__a, __b)
  4349. { }
  4350. explicit
  4351. extreme_value_distribution(const param_type& __p)
  4352. : _M_param(__p)
  4353. { }
  4354. /**
  4355. * @brief Resets the distribution state.
  4356. */
  4357. void
  4358. reset()
  4359. { }
  4360. /**
  4361. * @brief Return the @f$a@f$ parameter of the distribution.
  4362. */
  4363. _RealType
  4364. a() const
  4365. { return _M_param.a(); }
  4366. /**
  4367. * @brief Return the @f$b@f$ parameter of the distribution.
  4368. */
  4369. _RealType
  4370. b() const
  4371. { return _M_param.b(); }
  4372. /**
  4373. * @brief Returns the parameter set of the distribution.
  4374. */
  4375. param_type
  4376. param() const
  4377. { return _M_param; }
  4378. /**
  4379. * @brief Sets the parameter set of the distribution.
  4380. * @param __param The new parameter set of the distribution.
  4381. */
  4382. void
  4383. param(const param_type& __param)
  4384. { _M_param = __param; }
  4385. /**
  4386. * @brief Returns the greatest lower bound value of the distribution.
  4387. */
  4388. result_type
  4389. min() const
  4390. { return std::numeric_limits<result_type>::lowest(); }
  4391. /**
  4392. * @brief Returns the least upper bound value of the distribution.
  4393. */
  4394. result_type
  4395. max() const
  4396. { return std::numeric_limits<result_type>::max(); }
  4397. /**
  4398. * @brief Generating functions.
  4399. */
  4400. template<typename _UniformRandomNumberGenerator>
  4401. result_type
  4402. operator()(_UniformRandomNumberGenerator& __urng)
  4403. { return this->operator()(__urng, _M_param); }
  4404. template<typename _UniformRandomNumberGenerator>
  4405. result_type
  4406. operator()(_UniformRandomNumberGenerator& __urng,
  4407. const param_type& __p);
  4408. template<typename _ForwardIterator,
  4409. typename _UniformRandomNumberGenerator>
  4410. void
  4411. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4412. _UniformRandomNumberGenerator& __urng)
  4413. { this->__generate(__f, __t, __urng, _M_param); }
  4414. template<typename _ForwardIterator,
  4415. typename _UniformRandomNumberGenerator>
  4416. void
  4417. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4418. _UniformRandomNumberGenerator& __urng,
  4419. const param_type& __p)
  4420. { this->__generate_impl(__f, __t, __urng, __p); }
  4421. template<typename _UniformRandomNumberGenerator>
  4422. void
  4423. __generate(result_type* __f, result_type* __t,
  4424. _UniformRandomNumberGenerator& __urng,
  4425. const param_type& __p)
  4426. { this->__generate_impl(__f, __t, __urng, __p); }
  4427. /**
  4428. * @brief Return true if two extreme value distributions have the same
  4429. * parameters.
  4430. */
  4431. friend bool
  4432. operator==(const extreme_value_distribution& __d1,
  4433. const extreme_value_distribution& __d2)
  4434. { return __d1._M_param == __d2._M_param; }
  4435. private:
  4436. template<typename _ForwardIterator,
  4437. typename _UniformRandomNumberGenerator>
  4438. void
  4439. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4440. _UniformRandomNumberGenerator& __urng,
  4441. const param_type& __p);
  4442. param_type _M_param;
  4443. };
  4444. /**
  4445. * @brief Return true if two extreme value distributions have different
  4446. * parameters.
  4447. */
  4448. template<typename _RealType>
  4449. inline bool
  4450. operator!=(const std::extreme_value_distribution<_RealType>& __d1,
  4451. const std::extreme_value_distribution<_RealType>& __d2)
  4452. { return !(__d1 == __d2); }
  4453. /**
  4454. * @brief Inserts a %extreme_value_distribution random number distribution
  4455. * @p __x into the output stream @p __os.
  4456. *
  4457. * @param __os An output stream.
  4458. * @param __x A %extreme_value_distribution random number distribution.
  4459. *
  4460. * @returns The output stream with the state of @p __x inserted or in
  4461. * an error state.
  4462. */
  4463. template<typename _RealType, typename _CharT, typename _Traits>
  4464. std::basic_ostream<_CharT, _Traits>&
  4465. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4466. const std::extreme_value_distribution<_RealType>& __x);
  4467. /**
  4468. * @brief Extracts a %extreme_value_distribution random number
  4469. * distribution @p __x from the input stream @p __is.
  4470. *
  4471. * @param __is An input stream.
  4472. * @param __x A %extreme_value_distribution random number
  4473. * generator engine.
  4474. *
  4475. * @returns The input stream with @p __x extracted or in an error state.
  4476. */
  4477. template<typename _RealType, typename _CharT, typename _Traits>
  4478. std::basic_istream<_CharT, _Traits>&
  4479. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4480. std::extreme_value_distribution<_RealType>& __x);
  4481. /**
  4482. * @brief A discrete_distribution random number distribution.
  4483. *
  4484. * The formula for the discrete probability mass function is
  4485. *
  4486. */
  4487. template<typename _IntType = int>
  4488. class discrete_distribution
  4489. {
  4490. static_assert(std::is_integral<_IntType>::value,
  4491. "result_type must be an integral type");
  4492. public:
  4493. /** The type of the range of the distribution. */
  4494. typedef _IntType result_type;
  4495. /** Parameter type. */
  4496. struct param_type
  4497. {
  4498. typedef discrete_distribution<_IntType> distribution_type;
  4499. friend class discrete_distribution<_IntType>;
  4500. param_type()
  4501. : _M_prob(), _M_cp()
  4502. { }
  4503. template<typename _InputIterator>
  4504. param_type(_InputIterator __wbegin,
  4505. _InputIterator __wend)
  4506. : _M_prob(__wbegin, __wend), _M_cp()
  4507. { _M_initialize(); }
  4508. param_type(initializer_list<double> __wil)
  4509. : _M_prob(__wil.begin(), __wil.end()), _M_cp()
  4510. { _M_initialize(); }
  4511. template<typename _Func>
  4512. param_type(size_t __nw, double __xmin, double __xmax,
  4513. _Func __fw);
  4514. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  4515. param_type(const param_type&) = default;
  4516. param_type& operator=(const param_type&) = default;
  4517. std::vector<double>
  4518. probabilities() const
  4519. { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; }
  4520. friend bool
  4521. operator==(const param_type& __p1, const param_type& __p2)
  4522. { return __p1._M_prob == __p2._M_prob; }
  4523. friend bool
  4524. operator!=(const param_type& __p1, const param_type& __p2)
  4525. { return !(__p1 == __p2); }
  4526. private:
  4527. void
  4528. _M_initialize();
  4529. std::vector<double> _M_prob;
  4530. std::vector<double> _M_cp;
  4531. };
  4532. discrete_distribution()
  4533. : _M_param()
  4534. { }
  4535. template<typename _InputIterator>
  4536. discrete_distribution(_InputIterator __wbegin,
  4537. _InputIterator __wend)
  4538. : _M_param(__wbegin, __wend)
  4539. { }
  4540. discrete_distribution(initializer_list<double> __wl)
  4541. : _M_param(__wl)
  4542. { }
  4543. template<typename _Func>
  4544. discrete_distribution(size_t __nw, double __xmin, double __xmax,
  4545. _Func __fw)
  4546. : _M_param(__nw, __xmin, __xmax, __fw)
  4547. { }
  4548. explicit
  4549. discrete_distribution(const param_type& __p)
  4550. : _M_param(__p)
  4551. { }
  4552. /**
  4553. * @brief Resets the distribution state.
  4554. */
  4555. void
  4556. reset()
  4557. { }
  4558. /**
  4559. * @brief Returns the probabilities of the distribution.
  4560. */
  4561. std::vector<double>
  4562. probabilities() const
  4563. {
  4564. return _M_param._M_prob.empty()
  4565. ? std::vector<double>(1, 1.0) : _M_param._M_prob;
  4566. }
  4567. /**
  4568. * @brief Returns the parameter set of the distribution.
  4569. */
  4570. param_type
  4571. param() const
  4572. { return _M_param; }
  4573. /**
  4574. * @brief Sets the parameter set of the distribution.
  4575. * @param __param The new parameter set of the distribution.
  4576. */
  4577. void
  4578. param(const param_type& __param)
  4579. { _M_param = __param; }
  4580. /**
  4581. * @brief Returns the greatest lower bound value of the distribution.
  4582. */
  4583. result_type
  4584. min() const
  4585. { return result_type(0); }
  4586. /**
  4587. * @brief Returns the least upper bound value of the distribution.
  4588. */
  4589. result_type
  4590. max() const
  4591. {
  4592. return _M_param._M_prob.empty()
  4593. ? result_type(0) : result_type(_M_param._M_prob.size() - 1);
  4594. }
  4595. /**
  4596. * @brief Generating functions.
  4597. */
  4598. template<typename _UniformRandomNumberGenerator>
  4599. result_type
  4600. operator()(_UniformRandomNumberGenerator& __urng)
  4601. { return this->operator()(__urng, _M_param); }
  4602. template<typename _UniformRandomNumberGenerator>
  4603. result_type
  4604. operator()(_UniformRandomNumberGenerator& __urng,
  4605. const param_type& __p);
  4606. template<typename _ForwardIterator,
  4607. typename _UniformRandomNumberGenerator>
  4608. void
  4609. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4610. _UniformRandomNumberGenerator& __urng)
  4611. { this->__generate(__f, __t, __urng, _M_param); }
  4612. template<typename _ForwardIterator,
  4613. typename _UniformRandomNumberGenerator>
  4614. void
  4615. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4616. _UniformRandomNumberGenerator& __urng,
  4617. const param_type& __p)
  4618. { this->__generate_impl(__f, __t, __urng, __p); }
  4619. template<typename _UniformRandomNumberGenerator>
  4620. void
  4621. __generate(result_type* __f, result_type* __t,
  4622. _UniformRandomNumberGenerator& __urng,
  4623. const param_type& __p)
  4624. { this->__generate_impl(__f, __t, __urng, __p); }
  4625. /**
  4626. * @brief Return true if two discrete distributions have the same
  4627. * parameters.
  4628. */
  4629. friend bool
  4630. operator==(const discrete_distribution& __d1,
  4631. const discrete_distribution& __d2)
  4632. { return __d1._M_param == __d2._M_param; }
  4633. /**
  4634. * @brief Inserts a %discrete_distribution random number distribution
  4635. * @p __x into the output stream @p __os.
  4636. *
  4637. * @param __os An output stream.
  4638. * @param __x A %discrete_distribution random number distribution.
  4639. *
  4640. * @returns The output stream with the state of @p __x inserted or in
  4641. * an error state.
  4642. */
  4643. template<typename _IntType1, typename _CharT, typename _Traits>
  4644. friend std::basic_ostream<_CharT, _Traits>&
  4645. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4646. const std::discrete_distribution<_IntType1>& __x);
  4647. /**
  4648. * @brief Extracts a %discrete_distribution random number distribution
  4649. * @p __x from the input stream @p __is.
  4650. *
  4651. * @param __is An input stream.
  4652. * @param __x A %discrete_distribution random number
  4653. * generator engine.
  4654. *
  4655. * @returns The input stream with @p __x extracted or in an error
  4656. * state.
  4657. */
  4658. template<typename _IntType1, typename _CharT, typename _Traits>
  4659. friend std::basic_istream<_CharT, _Traits>&
  4660. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4661. std::discrete_distribution<_IntType1>& __x);
  4662. private:
  4663. template<typename _ForwardIterator,
  4664. typename _UniformRandomNumberGenerator>
  4665. void
  4666. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4667. _UniformRandomNumberGenerator& __urng,
  4668. const param_type& __p);
  4669. param_type _M_param;
  4670. };
  4671. /**
  4672. * @brief Return true if two discrete distributions have different
  4673. * parameters.
  4674. */
  4675. template<typename _IntType>
  4676. inline bool
  4677. operator!=(const std::discrete_distribution<_IntType>& __d1,
  4678. const std::discrete_distribution<_IntType>& __d2)
  4679. { return !(__d1 == __d2); }
  4680. /**
  4681. * @brief A piecewise_constant_distribution random number distribution.
  4682. *
  4683. * The formula for the piecewise constant probability mass function is
  4684. *
  4685. */
  4686. template<typename _RealType = double>
  4687. class piecewise_constant_distribution
  4688. {
  4689. static_assert(std::is_floating_point<_RealType>::value,
  4690. "result_type must be a floating point type");
  4691. public:
  4692. /** The type of the range of the distribution. */
  4693. typedef _RealType result_type;
  4694. /** Parameter type. */
  4695. struct param_type
  4696. {
  4697. typedef piecewise_constant_distribution<_RealType> distribution_type;
  4698. friend class piecewise_constant_distribution<_RealType>;
  4699. param_type()
  4700. : _M_int(), _M_den(), _M_cp()
  4701. { }
  4702. template<typename _InputIteratorB, typename _InputIteratorW>
  4703. param_type(_InputIteratorB __bfirst,
  4704. _InputIteratorB __bend,
  4705. _InputIteratorW __wbegin);
  4706. template<typename _Func>
  4707. param_type(initializer_list<_RealType> __bi, _Func __fw);
  4708. template<typename _Func>
  4709. param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
  4710. _Func __fw);
  4711. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  4712. param_type(const param_type&) = default;
  4713. param_type& operator=(const param_type&) = default;
  4714. std::vector<_RealType>
  4715. intervals() const
  4716. {
  4717. if (_M_int.empty())
  4718. {
  4719. std::vector<_RealType> __tmp(2);
  4720. __tmp[1] = _RealType(1);
  4721. return __tmp;
  4722. }
  4723. else
  4724. return _M_int;
  4725. }
  4726. std::vector<double>
  4727. densities() const
  4728. { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }
  4729. friend bool
  4730. operator==(const param_type& __p1, const param_type& __p2)
  4731. { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
  4732. friend bool
  4733. operator!=(const param_type& __p1, const param_type& __p2)
  4734. { return !(__p1 == __p2); }
  4735. private:
  4736. void
  4737. _M_initialize();
  4738. std::vector<_RealType> _M_int;
  4739. std::vector<double> _M_den;
  4740. std::vector<double> _M_cp;
  4741. };
  4742. explicit
  4743. piecewise_constant_distribution()
  4744. : _M_param()
  4745. { }
  4746. template<typename _InputIteratorB, typename _InputIteratorW>
  4747. piecewise_constant_distribution(_InputIteratorB __bfirst,
  4748. _InputIteratorB __bend,
  4749. _InputIteratorW __wbegin)
  4750. : _M_param(__bfirst, __bend, __wbegin)
  4751. { }
  4752. template<typename _Func>
  4753. piecewise_constant_distribution(initializer_list<_RealType> __bl,
  4754. _Func __fw)
  4755. : _M_param(__bl, __fw)
  4756. { }
  4757. template<typename _Func>
  4758. piecewise_constant_distribution(size_t __nw,
  4759. _RealType __xmin, _RealType __xmax,
  4760. _Func __fw)
  4761. : _M_param(__nw, __xmin, __xmax, __fw)
  4762. { }
  4763. explicit
  4764. piecewise_constant_distribution(const param_type& __p)
  4765. : _M_param(__p)
  4766. { }
  4767. /**
  4768. * @brief Resets the distribution state.
  4769. */
  4770. void
  4771. reset()
  4772. { }
  4773. /**
  4774. * @brief Returns a vector of the intervals.
  4775. */
  4776. std::vector<_RealType>
  4777. intervals() const
  4778. {
  4779. if (_M_param._M_int.empty())
  4780. {
  4781. std::vector<_RealType> __tmp(2);
  4782. __tmp[1] = _RealType(1);
  4783. return __tmp;
  4784. }
  4785. else
  4786. return _M_param._M_int;
  4787. }
  4788. /**
  4789. * @brief Returns a vector of the probability densities.
  4790. */
  4791. std::vector<double>
  4792. densities() const
  4793. {
  4794. return _M_param._M_den.empty()
  4795. ? std::vector<double>(1, 1.0) : _M_param._M_den;
  4796. }
  4797. /**
  4798. * @brief Returns the parameter set of the distribution.
  4799. */
  4800. param_type
  4801. param() const
  4802. { return _M_param; }
  4803. /**
  4804. * @brief Sets the parameter set of the distribution.
  4805. * @param __param The new parameter set of the distribution.
  4806. */
  4807. void
  4808. param(const param_type& __param)
  4809. { _M_param = __param; }
  4810. /**
  4811. * @brief Returns the greatest lower bound value of the distribution.
  4812. */
  4813. result_type
  4814. min() const
  4815. {
  4816. return _M_param._M_int.empty()
  4817. ? result_type(0) : _M_param._M_int.front();
  4818. }
  4819. /**
  4820. * @brief Returns the least upper bound value of the distribution.
  4821. */
  4822. result_type
  4823. max() const
  4824. {
  4825. return _M_param._M_int.empty()
  4826. ? result_type(1) : _M_param._M_int.back();
  4827. }
  4828. /**
  4829. * @brief Generating functions.
  4830. */
  4831. template<typename _UniformRandomNumberGenerator>
  4832. result_type
  4833. operator()(_UniformRandomNumberGenerator& __urng)
  4834. { return this->operator()(__urng, _M_param); }
  4835. template<typename _UniformRandomNumberGenerator>
  4836. result_type
  4837. operator()(_UniformRandomNumberGenerator& __urng,
  4838. const param_type& __p);
  4839. template<typename _ForwardIterator,
  4840. typename _UniformRandomNumberGenerator>
  4841. void
  4842. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4843. _UniformRandomNumberGenerator& __urng)
  4844. { this->__generate(__f, __t, __urng, _M_param); }
  4845. template<typename _ForwardIterator,
  4846. typename _UniformRandomNumberGenerator>
  4847. void
  4848. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4849. _UniformRandomNumberGenerator& __urng,
  4850. const param_type& __p)
  4851. { this->__generate_impl(__f, __t, __urng, __p); }
  4852. template<typename _UniformRandomNumberGenerator>
  4853. void
  4854. __generate(result_type* __f, result_type* __t,
  4855. _UniformRandomNumberGenerator& __urng,
  4856. const param_type& __p)
  4857. { this->__generate_impl(__f, __t, __urng, __p); }
  4858. /**
  4859. * @brief Return true if two piecewise constant distributions have the
  4860. * same parameters.
  4861. */
  4862. friend bool
  4863. operator==(const piecewise_constant_distribution& __d1,
  4864. const piecewise_constant_distribution& __d2)
  4865. { return __d1._M_param == __d2._M_param; }
  4866. /**
  4867. * @brief Inserts a %piecewise_constant_distribution random
  4868. * number distribution @p __x into the output stream @p __os.
  4869. *
  4870. * @param __os An output stream.
  4871. * @param __x A %piecewise_constant_distribution random number
  4872. * distribution.
  4873. *
  4874. * @returns The output stream with the state of @p __x inserted or in
  4875. * an error state.
  4876. */
  4877. template<typename _RealType1, typename _CharT, typename _Traits>
  4878. friend std::basic_ostream<_CharT, _Traits>&
  4879. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4880. const std::piecewise_constant_distribution<_RealType1>& __x);
  4881. /**
  4882. * @brief Extracts a %piecewise_constant_distribution random
  4883. * number distribution @p __x from the input stream @p __is.
  4884. *
  4885. * @param __is An input stream.
  4886. * @param __x A %piecewise_constant_distribution random number
  4887. * generator engine.
  4888. *
  4889. * @returns The input stream with @p __x extracted or in an error
  4890. * state.
  4891. */
  4892. template<typename _RealType1, typename _CharT, typename _Traits>
  4893. friend std::basic_istream<_CharT, _Traits>&
  4894. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4895. std::piecewise_constant_distribution<_RealType1>& __x);
  4896. private:
  4897. template<typename _ForwardIterator,
  4898. typename _UniformRandomNumberGenerator>
  4899. void
  4900. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4901. _UniformRandomNumberGenerator& __urng,
  4902. const param_type& __p);
  4903. param_type _M_param;
  4904. };
  4905. /**
  4906. * @brief Return true if two piecewise constant distributions have
  4907. * different parameters.
  4908. */
  4909. template<typename _RealType>
  4910. inline bool
  4911. operator!=(const std::piecewise_constant_distribution<_RealType>& __d1,
  4912. const std::piecewise_constant_distribution<_RealType>& __d2)
  4913. { return !(__d1 == __d2); }
  4914. /**
  4915. * @brief A piecewise_linear_distribution random number distribution.
  4916. *
  4917. * The formula for the piecewise linear probability mass function is
  4918. *
  4919. */
  4920. template<typename _RealType = double>
  4921. class piecewise_linear_distribution
  4922. {
  4923. static_assert(std::is_floating_point<_RealType>::value,
  4924. "result_type must be a floating point type");
  4925. public:
  4926. /** The type of the range of the distribution. */
  4927. typedef _RealType result_type;
  4928. /** Parameter type. */
  4929. struct param_type
  4930. {
  4931. typedef piecewise_linear_distribution<_RealType> distribution_type;
  4932. friend class piecewise_linear_distribution<_RealType>;
  4933. param_type()
  4934. : _M_int(), _M_den(), _M_cp(), _M_m()
  4935. { }
  4936. template<typename _InputIteratorB, typename _InputIteratorW>
  4937. param_type(_InputIteratorB __bfirst,
  4938. _InputIteratorB __bend,
  4939. _InputIteratorW __wbegin);
  4940. template<typename _Func>
  4941. param_type(initializer_list<_RealType> __bl, _Func __fw);
  4942. template<typename _Func>
  4943. param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
  4944. _Func __fw);
  4945. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  4946. param_type(const param_type&) = default;
  4947. param_type& operator=(const param_type&) = default;
  4948. std::vector<_RealType>
  4949. intervals() const
  4950. {
  4951. if (_M_int.empty())
  4952. {
  4953. std::vector<_RealType> __tmp(2);
  4954. __tmp[1] = _RealType(1);
  4955. return __tmp;
  4956. }
  4957. else
  4958. return _M_int;
  4959. }
  4960. std::vector<double>
  4961. densities() const
  4962. { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; }
  4963. friend bool
  4964. operator==(const param_type& __p1, const param_type& __p2)
  4965. { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
  4966. friend bool
  4967. operator!=(const param_type& __p1, const param_type& __p2)
  4968. { return !(__p1 == __p2); }
  4969. private:
  4970. void
  4971. _M_initialize();
  4972. std::vector<_RealType> _M_int;
  4973. std::vector<double> _M_den;
  4974. std::vector<double> _M_cp;
  4975. std::vector<double> _M_m;
  4976. };
  4977. explicit
  4978. piecewise_linear_distribution()
  4979. : _M_param()
  4980. { }
  4981. template<typename _InputIteratorB, typename _InputIteratorW>
  4982. piecewise_linear_distribution(_InputIteratorB __bfirst,
  4983. _InputIteratorB __bend,
  4984. _InputIteratorW __wbegin)
  4985. : _M_param(__bfirst, __bend, __wbegin)
  4986. { }
  4987. template<typename _Func>
  4988. piecewise_linear_distribution(initializer_list<_RealType> __bl,
  4989. _Func __fw)
  4990. : _M_param(__bl, __fw)
  4991. { }
  4992. template<typename _Func>
  4993. piecewise_linear_distribution(size_t __nw,
  4994. _RealType __xmin, _RealType __xmax,
  4995. _Func __fw)
  4996. : _M_param(__nw, __xmin, __xmax, __fw)
  4997. { }
  4998. explicit
  4999. piecewise_linear_distribution(const param_type& __p)
  5000. : _M_param(__p)
  5001. { }
  5002. /**
  5003. * Resets the distribution state.
  5004. */
  5005. void
  5006. reset()
  5007. { }
  5008. /**
  5009. * @brief Return the intervals of the distribution.
  5010. */
  5011. std::vector<_RealType>
  5012. intervals() const
  5013. {
  5014. if (_M_param._M_int.empty())
  5015. {
  5016. std::vector<_RealType> __tmp(2);
  5017. __tmp[1] = _RealType(1);
  5018. return __tmp;
  5019. }
  5020. else
  5021. return _M_param._M_int;
  5022. }
  5023. /**
  5024. * @brief Return a vector of the probability densities of the
  5025. * distribution.
  5026. */
  5027. std::vector<double>
  5028. densities() const
  5029. {
  5030. return _M_param._M_den.empty()
  5031. ? std::vector<double>(2, 1.0) : _M_param._M_den;
  5032. }
  5033. /**
  5034. * @brief Returns the parameter set of the distribution.
  5035. */
  5036. param_type
  5037. param() const
  5038. { return _M_param; }
  5039. /**
  5040. * @brief Sets the parameter set of the distribution.
  5041. * @param __param The new parameter set of the distribution.
  5042. */
  5043. void
  5044. param(const param_type& __param)
  5045. { _M_param = __param; }
  5046. /**
  5047. * @brief Returns the greatest lower bound value of the distribution.
  5048. */
  5049. result_type
  5050. min() const
  5051. {
  5052. return _M_param._M_int.empty()
  5053. ? result_type(0) : _M_param._M_int.front();
  5054. }
  5055. /**
  5056. * @brief Returns the least upper bound value of the distribution.
  5057. */
  5058. result_type
  5059. max() const
  5060. {
  5061. return _M_param._M_int.empty()
  5062. ? result_type(1) : _M_param._M_int.back();
  5063. }
  5064. /**
  5065. * @brief Generating functions.
  5066. */
  5067. template<typename _UniformRandomNumberGenerator>
  5068. result_type
  5069. operator()(_UniformRandomNumberGenerator& __urng)
  5070. { return this->operator()(__urng, _M_param); }
  5071. template<typename _UniformRandomNumberGenerator>
  5072. result_type
  5073. operator()(_UniformRandomNumberGenerator& __urng,
  5074. const param_type& __p);
  5075. template<typename _ForwardIterator,
  5076. typename _UniformRandomNumberGenerator>
  5077. void
  5078. __generate(_ForwardIterator __f, _ForwardIterator __t,
  5079. _UniformRandomNumberGenerator& __urng)
  5080. { this->__generate(__f, __t, __urng, _M_param); }
  5081. template<typename _ForwardIterator,
  5082. typename _UniformRandomNumberGenerator>
  5083. void
  5084. __generate(_ForwardIterator __f, _ForwardIterator __t,
  5085. _UniformRandomNumberGenerator& __urng,
  5086. const param_type& __p)
  5087. { this->__generate_impl(__f, __t, __urng, __p); }
  5088. template<typename _UniformRandomNumberGenerator>
  5089. void
  5090. __generate(result_type* __f, result_type* __t,
  5091. _UniformRandomNumberGenerator& __urng,
  5092. const param_type& __p)
  5093. { this->__generate_impl(__f, __t, __urng, __p); }
  5094. /**
  5095. * @brief Return true if two piecewise linear distributions have the
  5096. * same parameters.
  5097. */
  5098. friend bool
  5099. operator==(const piecewise_linear_distribution& __d1,
  5100. const piecewise_linear_distribution& __d2)
  5101. { return __d1._M_param == __d2._M_param; }
  5102. /**
  5103. * @brief Inserts a %piecewise_linear_distribution random number
  5104. * distribution @p __x into the output stream @p __os.
  5105. *
  5106. * @param __os An output stream.
  5107. * @param __x A %piecewise_linear_distribution random number
  5108. * distribution.
  5109. *
  5110. * @returns The output stream with the state of @p __x inserted or in
  5111. * an error state.
  5112. */
  5113. template<typename _RealType1, typename _CharT, typename _Traits>
  5114. friend std::basic_ostream<_CharT, _Traits>&
  5115. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  5116. const std::piecewise_linear_distribution<_RealType1>& __x);
  5117. /**
  5118. * @brief Extracts a %piecewise_linear_distribution random number
  5119. * distribution @p __x from the input stream @p __is.
  5120. *
  5121. * @param __is An input stream.
  5122. * @param __x A %piecewise_linear_distribution random number
  5123. * generator engine.
  5124. *
  5125. * @returns The input stream with @p __x extracted or in an error
  5126. * state.
  5127. */
  5128. template<typename _RealType1, typename _CharT, typename _Traits>
  5129. friend std::basic_istream<_CharT, _Traits>&
  5130. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  5131. std::piecewise_linear_distribution<_RealType1>& __x);
  5132. private:
  5133. template<typename _ForwardIterator,
  5134. typename _UniformRandomNumberGenerator>
  5135. void
  5136. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  5137. _UniformRandomNumberGenerator& __urng,
  5138. const param_type& __p);
  5139. param_type _M_param;
  5140. };
  5141. /**
  5142. * @brief Return true if two piecewise linear distributions have
  5143. * different parameters.
  5144. */
  5145. template<typename _RealType>
  5146. inline bool
  5147. operator!=(const std::piecewise_linear_distribution<_RealType>& __d1,
  5148. const std::piecewise_linear_distribution<_RealType>& __d2)
  5149. { return !(__d1 == __d2); }
  5150. /* @} */ // group random_distributions_poisson
  5151. /* @} */ // group random_distributions
  5152. /**
  5153. * @addtogroup random_utilities Random Number Utilities
  5154. * @ingroup random
  5155. * @{
  5156. */
  5157. /**
  5158. * @brief The seed_seq class generates sequences of seeds for random
  5159. * number generators.
  5160. */
  5161. class seed_seq
  5162. {
  5163. public:
  5164. /** The type of the seed vales. */
  5165. typedef uint_least32_t result_type;
  5166. /** Default constructor. */
  5167. seed_seq() noexcept
  5168. : _M_v()
  5169. { }
  5170. template<typename _IntType>
  5171. seed_seq(std::initializer_list<_IntType> il);
  5172. template<typename _InputIterator>
  5173. seed_seq(_InputIterator __begin, _InputIterator __end);
  5174. // generating functions
  5175. template<typename _RandomAccessIterator>
  5176. void
  5177. generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
  5178. // property functions
  5179. size_t size() const noexcept
  5180. { return _M_v.size(); }
  5181. template<typename _OutputIterator>
  5182. void
  5183. param(_OutputIterator __dest) const
  5184. { std::copy(_M_v.begin(), _M_v.end(), __dest); }
  5185. // no copy functions
  5186. seed_seq(const seed_seq&) = delete;
  5187. seed_seq& operator=(const seed_seq&) = delete;
  5188. private:
  5189. std::vector<result_type> _M_v;
  5190. };
  5191. /* @} */ // group random_utilities
  5192. /* @} */ // group random
  5193. _GLIBCXX_END_NAMESPACE_VERSION
  5194. } // namespace std
  5195. #endif

my random.h archive