inversive_congruential.hpp 9.3 KB

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