inversive_congruential.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* boost random/inversive_congruential.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  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$
  11. *
  12. * Revision history
  13. * 2001-02-18 moved to individual header files
  14. */
  15. #ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
  16. #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
  17. #include <iosfwd>
  18. #include <stdexcept>
  19. #include <boost/assert.hpp>
  20. #include <boost/config.hpp>
  21. #include <boost/cstdint.hpp>
  22. #include <boost/integer/static_log2.hpp>
  23. #include <boost/random/detail/config.hpp>
  24. #include <boost/random/detail/const_mod.hpp>
  25. #include <boost/random/detail/seed.hpp>
  26. #include <boost/random/detail/operators.hpp>
  27. #include <boost/random/detail/seed_impl.hpp>
  28. #include <boost/random/detail/disable_warnings.hpp>
  29. namespace boost {
  30. namespace random {
  31. // Eichenauer and Lehn 1986
  32. /**
  33. * Instantiations of class template @c inversive_congruential_engine model a
  34. * \pseudo_random_number_generator. It uses the inversive congruential
  35. * algorithm (ICG) described in
  36. *
  37. * @blockquote
  38. * "Inversive pseudorandom number generators: concepts, results and links",
  39. * Peter Hellekalek, In: "Proceedings of the 1995 Winter Simulation
  40. * Conference", C. Alexopoulos, K. Kang, W.R. Lilegdon, and D. Goldsman
  41. * (editors), 1995, pp. 255-262. ftp://random.mat.sbg.ac.at/pub/data/wsc95.ps
  42. * @endblockquote
  43. *
  44. * The output sequence is defined by x(n+1) = (a*inv(x(n)) - b) (mod p),
  45. * where x(0), a, b, and the prime number p are parameters of the generator.
  46. * The expression inv(k) denotes the multiplicative inverse of k in the
  47. * field of integer numbers modulo p, with inv(0) := 0.
  48. *
  49. * The template parameter IntType shall denote a signed integral type large
  50. * enough to hold p; a, b, and p are the parameters of the generators. The
  51. * template parameter val is the validation value checked by validation.
  52. *
  53. * @xmlnote
  54. * The implementation currently uses the Euclidian Algorithm to compute
  55. * the multiplicative inverse. Therefore, the inversive generators are about
  56. * 10-20 times slower than the others (see section"performance"). However,
  57. * the paper talks of only 3x slowdown, so the Euclidian Algorithm is probably
  58. * not optimal for calculating the multiplicative inverse.
  59. * @endxmlnote
  60. */
  61. template<class IntType, IntType a, IntType b, IntType p>
  62. class inversive_congruential_engine
  63. {
  64. public:
  65. typedef IntType result_type;
  66. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  67. BOOST_STATIC_CONSTANT(result_type, multiplier = a);
  68. BOOST_STATIC_CONSTANT(result_type, increment = b);
  69. BOOST_STATIC_CONSTANT(result_type, modulus = p);
  70. BOOST_STATIC_CONSTANT(IntType, default_seed = 1);
  71. static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return b == 0 ? 1 : 0; }
  72. static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return p-1; }
  73. /**
  74. * Constructs an @c inversive_congruential_engine, seeding it with
  75. * the default seed.
  76. */
  77. inversive_congruential_engine() { seed(); }
  78. /**
  79. * Constructs an @c inversive_congruential_engine, seeding it with @c x0.
  80. */
  81. BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(inversive_congruential_engine,
  82. IntType, x0)
  83. { seed(x0); }
  84. /**
  85. * Constructs an @c inversive_congruential_engine, seeding it with values
  86. * produced by a call to @c seq.generate().
  87. */
  88. BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(inversive_congruential_engine,
  89. SeedSeq, seq)
  90. { seed(seq); }
  91. /**
  92. * Constructs an @c inversive_congruential_engine, seeds it
  93. * with values taken from the itrator range [first, last),
  94. * and adjusts first to point to the element after the last one
  95. * used. If there are not enough elements, throws @c std::invalid_argument.
  96. *
  97. * first and last must be input iterators.
  98. */
  99. template<class It> inversive_congruential_engine(It& first, It last)
  100. { seed(first, last); }
  101. /**
  102. * Calls seed(default_seed)
  103. */
  104. void seed() { seed(default_seed); }
  105. /**
  106. * If c mod m is zero and x0 mod m is zero, changes the current value of
  107. * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
  108. * distinct seeds in the range [1,m) will leave the generator in distinct
  109. * states. If c is not zero, the range is [0,m).
  110. */
  111. BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(inversive_congruential_engine, IntType, x0)
  112. {
  113. // wrap _x if it doesn't fit in the destination
  114. if(modulus == 0) {
  115. _value = x0;
  116. } else {
  117. _value = x0 % modulus;
  118. }
  119. // handle negative seeds
  120. if(_value <= 0 && _value != 0) {
  121. _value += modulus;
  122. }
  123. // adjust to the correct range
  124. if(increment == 0 && _value == 0) {
  125. _value = 1;
  126. }
  127. BOOST_ASSERT(_value >= (min)());
  128. BOOST_ASSERT(_value <= (max)());
  129. }
  130. /**
  131. * Seeds an @c inversive_congruential_engine using values from a SeedSeq.
  132. */
  133. BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(inversive_congruential_engine, SeedSeq, seq)
  134. { seed(detail::seed_one_int<IntType, modulus>(seq)); }
  135. /**
  136. * seeds an @c inversive_congruential_engine with values taken
  137. * from the itrator range [first, last) and adjusts @c first to
  138. * point to the element after the last one used. If there are
  139. * not enough elements, throws @c std::invalid_argument.
  140. *
  141. * @c first and @c last must be input iterators.
  142. */
  143. template<class It> void seed(It& first, It last)
  144. { seed(detail::get_one_int<IntType, modulus>(first, last)); }
  145. /** Returns the next output of the generator. */
  146. IntType operator()()
  147. {
  148. typedef const_mod<IntType, p> do_mod;
  149. _value = do_mod::mult_add(a, do_mod::invert(_value), b);
  150. return _value;
  151. }
  152. /** Fills a range with random values */
  153. template<class Iter>
  154. void generate(Iter first, Iter last)
  155. { detail::generate_from_int(*this, first, last); }
  156. /** Advances the state of the generator by @c z. */
  157. void discard(boost::uintmax_t z)
  158. {
  159. for(boost::uintmax_t j = 0; j < z; ++j) {
  160. (*this)();
  161. }
  162. }
  163. /**
  164. * Writes the textual representation of the generator to a @c std::ostream.
  165. */
  166. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, inversive_congruential_engine, x)
  167. {
  168. os << x._value;
  169. return os;
  170. }
  171. /**
  172. * Reads the textual representation of the generator from a @c std::istream.
  173. */
  174. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, inversive_congruential_engine, x)
  175. {
  176. is >> x._value;
  177. return is;
  178. }
  179. /**
  180. * Returns true if the two generators will produce identical
  181. * sequences of outputs.
  182. */
  183. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(inversive_congruential_engine, x, y)
  184. { return x._value == y._value; }
  185. /**
  186. * Returns true if the two generators will produce different
  187. * sequences of outputs.
  188. */
  189. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(inversive_congruential_engine)
  190. private:
  191. IntType _value;
  192. };
  193. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  194. // A definition is required even for integral static constants
  195. template<class IntType, IntType a, IntType b, IntType p>
  196. const bool inversive_congruential_engine<IntType, a, b, p>::has_fixed_range;
  197. template<class IntType, IntType a, IntType b, IntType p>
  198. const typename inversive_congruential_engine<IntType, a, b, p>::result_type inversive_congruential_engine<IntType, a, b, p>::multiplier;
  199. template<class IntType, IntType a, IntType b, IntType p>
  200. const typename inversive_congruential_engine<IntType, a, b, p>::result_type inversive_congruential_engine<IntType, a, b, p>::increment;
  201. template<class IntType, IntType a, IntType b, IntType p>
  202. const typename inversive_congruential_engine<IntType, a, b, p>::result_type inversive_congruential_engine<IntType, a, b, p>::modulus;
  203. template<class IntType, IntType a, IntType b, IntType p>
  204. const typename inversive_congruential_engine<IntType, a, b, p>::result_type inversive_congruential_engine<IntType, a, b, p>::default_seed;
  205. #endif
  206. /// \cond show_deprecated
  207. // provided for backwards compatibility
  208. template<class IntType, IntType a, IntType b, IntType p, IntType val = 0>
  209. class inversive_congruential : public inversive_congruential_engine<IntType, a, b, p>
  210. {
  211. typedef inversive_congruential_engine<IntType, a, b, p> base_type;
  212. public:
  213. inversive_congruential(IntType x0 = 1) : base_type(x0) {}
  214. template<class It>
  215. inversive_congruential(It& first, It last) : base_type(first, last) {}
  216. };
  217. /// \endcond
  218. /**
  219. * The specialization hellekalek1995 was suggested in
  220. *
  221. * @blockquote
  222. * "Inversive pseudorandom number generators: concepts, results and links",
  223. * Peter Hellekalek, In: "Proceedings of the 1995 Winter Simulation
  224. * Conference", C. Alexopoulos, K. Kang, W.R. Lilegdon, and D. Goldsman
  225. * (editors), 1995, pp. 255-262. ftp://random.mat.sbg.ac.at/pub/data/wsc95.ps
  226. * @endblockquote
  227. */
  228. typedef inversive_congruential_engine<uint32_t, 9102, 2147483647-36884165,
  229. 2147483647> hellekalek1995;
  230. } // namespace random
  231. using random::hellekalek1995;
  232. } // namespace boost
  233. #include <boost/random/detail/enable_warnings.hpp>
  234. #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP