independent_bits.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* boost random/independent_bits.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2011
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id: independent_bits.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
  11. *
  12. */
  13. #ifndef BOOST_RANDOM_INDEPENDENT_BITS_HPP
  14. #define BOOST_RANDOM_INDEPENDENT_BITS_HPP
  15. #include <istream>
  16. #include <iosfwd>
  17. #include <boost/assert.hpp>
  18. #include <boost/limits.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/integer/integer_mask.hpp>
  22. #include <boost/type_traits/make_unsigned.hpp>
  23. #include <boost/random/detail/config.hpp>
  24. #include <boost/random/detail/integer_log2.hpp>
  25. #include <boost/random/detail/operators.hpp>
  26. #include <boost/random/detail/seed.hpp>
  27. #include <boost/random/detail/seed_impl.hpp>
  28. #include <boost/random/detail/signed_unsigned_tools.hpp>
  29. namespace boost {
  30. namespace random {
  31. /**
  32. * An instantiation of class template @c independent_bits_engine
  33. * model a \pseudo_random_number_generator. It generates random
  34. * numbers distributed between [0, 2^w) by combining one or
  35. * more invocations of the base engine.
  36. *
  37. * Requires: 0 < w <= std::numeric_limits<UIntType>::digits
  38. */
  39. template<class Engine, std::size_t w, class UIntType>
  40. class independent_bits_engine
  41. {
  42. public:
  43. typedef Engine base_type;
  44. typedef UIntType result_type;
  45. // Required by old Boost.Random concept
  46. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  47. /** Returns the smallest value that the generator can produce. */
  48. static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  49. { return 0; }
  50. /** Returns the largest value that the generator can produce. */
  51. static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  52. { return boost::low_bits_mask_t<w>::sig_bits; }
  53. /**
  54. * Constructs an @c independent_bits_engine using the
  55. * default constructor of the base generator.
  56. */
  57. independent_bits_engine() { }
  58. /**
  59. * Constructs an @c independent_bits_engine, using seed as
  60. * the constructor argument for both base generators.
  61. */
  62. BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(independent_bits_engine,
  63. result_type, seed_arg)
  64. {
  65. _base.seed(seed_arg);
  66. }
  67. /**
  68. * Constructs an @c independent_bits_engine, using seq as
  69. * the constructor argument for the base generator.
  70. */
  71. BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(independent_bits_engine,
  72. SeedSeq, seq)
  73. { _base.seed(seq); }
  74. /** Constructs an @c independent_bits_engine by copying @c base. */
  75. independent_bits_engine(const base_type& base_arg) : _base(base_arg) {}
  76. /**
  77. * Contructs an @c independent_bits_engine with
  78. * values from the range defined by the input iterators first
  79. * and last. first will be modified to point to the element
  80. * after the last one used.
  81. *
  82. * Throws: @c std::invalid_argument if the input range is too small.
  83. *
  84. * Exception Safety: Basic
  85. */
  86. template<class It>
  87. independent_bits_engine(It& first, It last) : _base(first, last) { }
  88. /**
  89. * Seeds an @c independent_bits_engine using the default
  90. * seed of the base generator.
  91. */
  92. void seed() { _base.seed(); }
  93. /**
  94. * Seeds an @c independent_bits_engine, using @c seed as the
  95. * seed for the base generator.
  96. */
  97. BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(independent_bits_engine,
  98. result_type, seed_arg)
  99. { _base.seed(seed_arg); }
  100. /**
  101. * Seeds an @c independent_bits_engine, using @c seq to
  102. * seed the base generator.
  103. */
  104. BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(independent_bits_engine,
  105. SeedSeq, seq)
  106. { _base.seed(seq); }
  107. /**
  108. * Seeds an @c independent_bits_engine with
  109. * values from the range defined by the input iterators first
  110. * and last. first will be modified to point to the element
  111. * after the last one used.
  112. *
  113. * Throws: @c std::invalid_argument if the input range is too small.
  114. *
  115. * Exception Safety: Basic
  116. */
  117. template<class It> void seed(It& first, It last)
  118. { _base.seed(first, last); }
  119. /** Returns the next value of the generator. */
  120. result_type operator()()
  121. {
  122. // While it may seem wasteful to recalculate this
  123. // every time, both msvc and gcc can propagate
  124. // constants, resolving this at compile time.
  125. base_unsigned range =
  126. detail::subtract<base_result>()((_base.max)(), (_base.min)());
  127. std::size_t m =
  128. (range == (std::numeric_limits<base_unsigned>::max)()) ?
  129. std::numeric_limits<base_unsigned>::digits :
  130. detail::integer_log2(range + 1);
  131. std::size_t n = (w + m - 1) / m;
  132. std::size_t w0, n0;
  133. base_unsigned y0, y1;
  134. base_unsigned y0_mask, y1_mask;
  135. calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
  136. if(base_unsigned(range - y0 + 1) > y0 / n) {
  137. // increment n and try again.
  138. ++n;
  139. calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
  140. }
  141. BOOST_ASSERT(n0*w0 + (n - n0)*(w0 + 1) == w);
  142. result_type S = 0;
  143. for(std::size_t k = 0; k < n0; ++k) {
  144. base_unsigned u;
  145. do {
  146. u = detail::subtract<base_result>()(_base(), (_base.min)());
  147. } while(u > base_unsigned(y0 - 1));
  148. S = (S << w0) + (u & y0_mask);
  149. }
  150. for(std::size_t k = 0; k < (n - n0); ++k) {
  151. base_unsigned u;
  152. do {
  153. u = detail::subtract<base_result>()(_base(), (_base.min)());
  154. } while(u > base_unsigned(y1 - 1));
  155. S = (S << (w0 + 1)) + (u & y1_mask);
  156. }
  157. return S;
  158. }
  159. /** Fills a range with random values */
  160. template<class Iter>
  161. void generate(Iter first, Iter last)
  162. { detail::generate_from_int(*this, first, last); }
  163. /** Advances the state of the generator by @c z. */
  164. void discard(boost::uintmax_t z)
  165. {
  166. for(boost::uintmax_t i = 0; i < z; ++i) {
  167. (*this)();
  168. }
  169. }
  170. const base_type& base() const { return _base; }
  171. /**
  172. * Writes the textual representation if the generator to a @c std::ostream.
  173. * The textual representation of the engine is the textual representation
  174. * of the base engine.
  175. */
  176. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, independent_bits_engine, r)
  177. {
  178. os << r._base;
  179. return os;
  180. }
  181. /**
  182. * Reads the state of an @c independent_bits_engine from a
  183. * @c std::istream.
  184. */
  185. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, independent_bits_engine, r)
  186. {
  187. is >> r._base;
  188. return is;
  189. }
  190. /**
  191. * Returns: true iff the two @c independent_bits_engines will
  192. * produce the same sequence of values.
  193. */
  194. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(independent_bits_engine, x, y)
  195. { return x._base == y._base; }
  196. /**
  197. * Returns: true iff the two @c independent_bits_engines will
  198. * produce different sequences of values.
  199. */
  200. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(independent_bits_engine)
  201. private:
  202. /// \cond show_private
  203. typedef typename base_type::result_type base_result;
  204. typedef typename make_unsigned<base_result>::type base_unsigned;
  205. void calc_params(
  206. std::size_t n, base_unsigned range,
  207. std::size_t& w0, std::size_t& n0,
  208. base_unsigned& y0, base_unsigned& y1,
  209. base_unsigned& y0_mask, base_unsigned& y1_mask)
  210. {
  211. BOOST_ASSERT(w >= n);
  212. w0 = w/n;
  213. n0 = n - w % n;
  214. y0_mask = (base_unsigned(2) << (w0 - 1)) - 1;
  215. y1_mask = (y0_mask << 1) | 1;
  216. y0 = (range + 1) & ~y0_mask;
  217. y1 = (range + 1) & ~y1_mask;
  218. BOOST_ASSERT(y0 != 0 || base_unsigned(range + 1) == 0);
  219. }
  220. /// \endcond
  221. Engine _base;
  222. };
  223. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  224. template<class Engine, std::size_t w, class UIntType>
  225. const bool independent_bits_engine<Engine, w, UIntType>::has_fixed_range;
  226. #endif
  227. } // namespace random
  228. } // namespace boost
  229. #endif // BOOST_RANDOM_INDEPENDENT_BITS_HPP