random_number.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/random_number.h
  21. * @brief Random number generator based on the Mersenne twister.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
  26. #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
  27. #include <parallel/types.h>
  28. #include <tr1/random>
  29. #include <limits>
  30. namespace __gnu_parallel
  31. {
  32. /** @brief Random number generator, based on the Mersenne twister. */
  33. class _RandomNumber
  34. {
  35. private:
  36. std::tr1::mt19937 _M_mt;
  37. uint64_t _M_supremum;
  38. uint64_t _M_rand_sup;
  39. double _M_supremum_reciprocal;
  40. double _M_rand_sup_reciprocal;
  41. // Assumed to be twice as long as the usual random number.
  42. uint64_t __cache;
  43. // Bit results.
  44. int __bits_left;
  45. static uint32_t
  46. __scale_down(uint64_t __x,
  47. #if _GLIBCXX_SCALE_DOWN_FPU
  48. uint64_t /*_M_supremum*/, double _M_supremum_reciprocal)
  49. #else
  50. uint64_t _M_supremum, double /*_M_supremum_reciprocal*/)
  51. #endif
  52. {
  53. #if _GLIBCXX_SCALE_DOWN_FPU
  54. return uint32_t(__x * _M_supremum_reciprocal);
  55. #else
  56. return static_cast<uint32_t>(__x % _M_supremum);
  57. #endif
  58. }
  59. public:
  60. /** @brief Default constructor. Seed with 0. */
  61. _RandomNumber()
  62. : _M_mt(0), _M_supremum(0x100000000ULL),
  63. _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
  64. _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
  65. _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
  66. __cache(0), __bits_left(0) { }
  67. /** @brief Constructor.
  68. * @param __seed Random __seed.
  69. * @param _M_supremum Generate integer random numbers in the
  70. * interval @c [0,_M_supremum). */
  71. _RandomNumber(uint32_t __seed, uint64_t _M_supremum = 0x100000000ULL)
  72. : _M_mt(__seed), _M_supremum(_M_supremum),
  73. _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
  74. _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
  75. _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
  76. __cache(0), __bits_left(0) { }
  77. /** @brief Generate unsigned random 32-bit integer. */
  78. uint32_t
  79. operator()()
  80. { return __scale_down(_M_mt(), _M_supremum, _M_supremum_reciprocal); }
  81. /** @brief Generate unsigned random 32-bit integer in the
  82. interval @c [0,local_supremum). */
  83. uint32_t
  84. operator()(uint64_t local_supremum)
  85. {
  86. return __scale_down(_M_mt(), local_supremum,
  87. double(local_supremum * _M_rand_sup_reciprocal));
  88. }
  89. /** @brief Generate a number of random bits, run-time parameter.
  90. * @param __bits Number of bits to generate. */
  91. unsigned long
  92. __genrand_bits(int __bits)
  93. {
  94. unsigned long __res = __cache & ((1 << __bits) - 1);
  95. __cache = __cache >> __bits;
  96. __bits_left -= __bits;
  97. if (__bits_left < 32)
  98. {
  99. __cache |= ((uint64_t(_M_mt())) << __bits_left);
  100. __bits_left += 32;
  101. }
  102. return __res;
  103. }
  104. };
  105. } // namespace __gnu_parallel
  106. #endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */