You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6101 lines
174KB

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