annotate DEPENDENCIES/generic/include/boost/random/inversive_congruential.hpp @ 125:34e428693f5d vext

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