shuffle_order.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* boost random/shuffle_order.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Copyright Steven Watanabe 2010
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. */
  14. #ifndef BOOST_RANDOM_SHUFFLE_ORDER_HPP
  15. #define BOOST_RANDOM_SHUFFLE_ORDER_HPP
  16. #include <iostream>
  17. #include <algorithm> // std::copy
  18. #include <cassert>
  19. #include <boost/config.hpp>
  20. #include <boost/limits.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/cstdint.hpp>
  23. #include <boost/random/detail/operators.hpp>
  24. #include <boost/random/detail/seed.hpp>
  25. #include <boost/random/detail/signed_unsigned_tools.hpp>
  26. #include <boost/random/linear_congruential.hpp>
  27. #include <boost/random/detail/disable_warnings.hpp>
  28. namespace boost {
  29. namespace random {
  30. /**
  31. * Instatiations of class template @c shuffle_order_engine model a
  32. * \pseudo_random_number_generator. It mixes the output
  33. * of some (usually \linear_congruential_engine)
  34. * \uniform_random_number_generator to get better statistical properties.
  35. * The algorithm is described in
  36. *
  37. * @blockquote
  38. * "Improving a poor random number generator", Carter Bays
  39. * and S.D. Durham, ACM Transactions on Mathematical Software,
  40. * Vol 2, No. 1, March 1976, pp. 59-64.
  41. * http://doi.acm.org/10.1145/355666.355670
  42. * @endblockquote
  43. *
  44. * The output of the base generator is buffered in an array of
  45. * length k. Every output X(n) has a second role: It gives an
  46. * index into the array where X(n+1) will be retrieved. Used
  47. * array elements are replaced with fresh output from the base
  48. * generator.
  49. *
  50. * Template parameters are the base generator and the array
  51. * length k, which should be around 100.
  52. */
  53. template<class UniformRandomNumberGenerator, std::size_t k>
  54. class shuffle_order_engine
  55. {
  56. public:
  57. typedef UniformRandomNumberGenerator base_type;
  58. typedef typename base_type::result_type result_type;
  59. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  60. BOOST_STATIC_CONSTANT(std::size_t, buffer_size = k);
  61. BOOST_STATIC_CONSTANT(std::size_t, table_size = k);
  62. BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
  63. BOOST_STATIC_ASSERT(k > 0);
  64. /**
  65. * Constructs a @c shuffle_order_engine by invoking the
  66. * default constructor of the base generator.
  67. *
  68. * Complexity: Exactly k+1 invocations of the base generator.
  69. */
  70. shuffle_order_engine() : _rng() { init(); }
  71. /**
  72. * Constructs a @c shuffle_output_engine by invoking the one-argument
  73. * constructor of the base generator with the parameter seed.
  74. *
  75. * Complexity: Exactly k+1 invocations of the base generator.
  76. */
  77. BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(shuffle_order_engine,
  78. result_type, s)
  79. { _rng.seed(s); init(); }
  80. BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(shuffle_order_engine, SeedSeq, seq)
  81. { _rng.seed(seq); init(); }
  82. /**
  83. * Constructs a @c shuffle_output_engine by using a copy
  84. * of the provided generator.
  85. *
  86. * Precondition: The template argument UniformRandomNumberGenerator
  87. * shall denote a CopyConstructible type.
  88. *
  89. * Complexity: Exactly k+1 invocations of the base generator.
  90. */
  91. explicit shuffle_order_engine(const base_type & rng) : _rng(rng) { init(); }
  92. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  93. explicit shuffle_order_engine(base_type&& rng) : _rng(rng) { init(); }
  94. #endif
  95. template<class It> shuffle_order_engine(It& first, It last)
  96. : _rng(first, last) { init(); }
  97. void seed() { _rng.seed(); init(); }
  98. /**
  99. * Invokes the one-argument seed method of the base generator
  100. * with the parameter seed and re-initializes the internal buffer array.
  101. *
  102. * Complexity: Exactly k+1 invocations of the base generator.
  103. */
  104. BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(shuffle_order_engine,
  105. result_type, seed_arg)
  106. { _rng.seed(seed_arg); init(); }
  107. /**
  108. * Invokes the one-argument seed method of the base generator
  109. * with the parameter seq and re-initializes the internal buffer array.
  110. *
  111. * Complexity: Exactly k+1 invocations of the base generator.
  112. */
  113. BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(shuffle_order_engine, SeedSeq, seq)
  114. { _rng.seed(seq); init(); }
  115. template<class It> void seed(It& first, It last)
  116. { _rng.seed(first, last); init(); }
  117. const base_type& base() const { return _rng; }
  118. result_type operator()() {
  119. // calculating the range every time may seem wasteful. However, this
  120. // makes the information locally available for the optimizer.
  121. typedef typename boost::random::traits::make_unsigned<result_type>::type base_unsigned;
  122. const base_unsigned brange =
  123. detail::subtract<result_type>()((max)(), (min)());
  124. const base_unsigned off =
  125. detail::subtract<result_type>()(y, (min)());
  126. base_unsigned j;
  127. if(k == 1) {
  128. j = 0;
  129. } else if(brange < (std::numeric_limits<base_unsigned>::max)() / k) {
  130. // try to do it in the native type if we know that it won't
  131. // overflow
  132. j = k * off / (brange + 1);
  133. } else if(brange < (std::numeric_limits<uintmax_t>::max)() / k) {
  134. // Otherwise try to use uint64_t
  135. j = static_cast<base_unsigned>(
  136. static_cast<uintmax_t>(off) * k /
  137. (static_cast<uintmax_t>(brange) + 1));
  138. } else {
  139. boost::uintmax_t divisor =
  140. static_cast<boost::uintmax_t>(brange) + 1;
  141. j = static_cast<base_unsigned>(detail::muldiv(off, k, divisor));
  142. }
  143. // assert(0 <= j && j < k);
  144. y = v[j];
  145. v[j] = _rng();
  146. return y;
  147. }
  148. /** Advances the generator by z steps. */
  149. void discard(boost::uintmax_t z)
  150. {
  151. for(boost::uintmax_t j = 0; j < z; ++j) {
  152. (*this)();
  153. }
  154. }
  155. /** Fills a range with pseudo-random values. */
  156. template<class Iter>
  157. void generate(Iter first, Iter last)
  158. { detail::generate_from_int(*this, first, last); }
  159. /** Returns the smallest value that the generator can produce. */
  160. static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  161. { return (base_type::min)(); }
  162. /** Returns the largest value that the generator can produce. */
  163. static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  164. { return (base_type::max)(); }
  165. /** Writes a @c shuffle_order_engine to a @c std::ostream. */
  166. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, shuffle_order_engine, s)
  167. {
  168. os << s._rng;
  169. for(std::size_t i = 0; i < k; ++i)
  170. os << ' ' << s.v[i];
  171. os << ' ' << s.y;
  172. return os;
  173. }
  174. /** Reads a @c shuffle_order_engine from a @c std::istream. */
  175. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, shuffle_order_engine, s)
  176. {
  177. is >> s._rng;
  178. for(std::size_t i = 0; i < k; ++i)
  179. is >> std::ws >> s.v[i];
  180. is >> std::ws >> s.y;
  181. return is;
  182. }
  183. /** Returns true if the two generators will produce identical sequences. */
  184. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(shuffle_order_engine, x, y)
  185. { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
  186. /** Returns true if the two generators will produce different sequences. */
  187. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(shuffle_order_engine)
  188. private:
  189. /// \cond show_private
  190. void init()
  191. {
  192. // we cannot use std::generate, because it uses pass-by-value for _rng
  193. for(result_type * p = v; p != v+k; ++p)
  194. *p = _rng();
  195. y = _rng();
  196. }
  197. /// \endcond
  198. base_type _rng;
  199. result_type v[k];
  200. result_type y;
  201. };
  202. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  203. // A definition is required even for integral static constants
  204. template<class URNG, std::size_t k>
  205. const bool shuffle_order_engine<URNG, k>::has_fixed_range;
  206. template<class URNG, std::size_t k>
  207. const std::size_t shuffle_order_engine<URNG, k>::table_size;
  208. template<class URNG, std::size_t k>
  209. const std::size_t shuffle_order_engine<URNG, k>::buffer_size;
  210. #endif
  211. /**
  212. * According to Harry Erwin (private e-mail), the specialization
  213. * @c kreutzer1986 was suggested in:
  214. *
  215. * @blockquote
  216. * "System Simulation: Programming Styles and Languages (International
  217. * Computer Science Series)", Wolfgang Kreutzer, Addison-Wesley, December 1986.
  218. * @endblockquote
  219. */
  220. typedef shuffle_order_engine<
  221. linear_congruential_engine<uint32_t, 1366, 150889, 714025>,
  222. 97> kreutzer1986;
  223. /**
  224. * The specialization @c knuth_b is specified by the C++ standard.
  225. * It is described in
  226. *
  227. * @blockquote
  228. * "The Art of Computer Programming, Second Edition, Volume 2,
  229. * Seminumerical Algorithms", Donald Knuth, Addison-Wesley, 1981.
  230. * @endblockquote
  231. */
  232. typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
  233. } // namespace random
  234. using random::kreutzer1986;
  235. } // namespace boost
  236. #include <boost/random/detail/enable_warnings.hpp>
  237. #endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP