- assert(_x >= (min)());
- assert(_x <= (max)());
- }
-
- /**
- * seeds a @c linear_congruential generator with values taken
- * from the itrator range [first, last) and adjusts @c first to
- * point to the element after the last one used. If there are
- * not enough elements, throws @c std::invalid_argument.
- *
- * @c first and @c last must be input iterators.
- */
- template<class It>
- void seed(It& first, It last)
- {
- if(first == last)
- throw std::invalid_argument("linear_congruential::seed");
- seed(*first++);
- }
-
- /**
- * Returns the smallest value that the @c linear_congruential generator
- * can produce.
- */
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
- /**
- * Returns the largest value that the @c linear_congruential generator
- * can produce.
- */
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
-
- /** Returns the next value of the @c linear_congruential generator. */
- IntType operator()()
- {
- _x = const_mod<IntType, m>::mult_add(a, _x, c);
- return _x;
- }
-
- static bool validation(IntType x) { return val == x; }
-
-#ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
-
- // Use a member function; Streamable concept not supported.
- bool operator==(const linear_congruential& rhs) const
- { return _x == rhs._x; }
- bool operator!=(const linear_congruential& rhs) const
- { return !(*this == rhs); }
-
-#else
- friend bool operator==(const linear_congruential& x,
- const linear_congruential& y)
- { return x._x == y._x; }
- friend bool operator!=(const linear_congruential& x,
- const linear_congruential& y)
- { return !(x == y); }
+
+ /** Fills a range with random values */
+ template<class Iter>
+ void generate(Iter first, Iter last)
+ { detail::generate_from_int(*this, first, last); }
+
+ /** Advances the state of the generator by @c z. */
+ void discard(boost::uintmax_t z)
+ {
+ typedef const_mod<IntType, m> mod_type;
+ IntType b_inv = mod_type::invert(a-1);
+ IntType b_gcd = mod_type::mult(a-1, b_inv);
+ if(b_gcd == 1) {
+ IntType a_z = mod_type::pow(a, z);
+ _x = mod_type::mult_add(a_z, _x,
+ mod_type::mult(mod_type::mult(c, b_inv), a_z - 1));
+ } else {
+ // compute (a^z - 1)*c % (b_gcd * m) / (b / b_gcd) * inv(b / b_gcd)
+ // we're storing the intermediate result / b_gcd
+ IntType a_zm1_over_gcd = 0;
+ IntType a_km1_over_gcd = (a - 1) / b_gcd;
+ boost::uintmax_t exponent = z;
+ while(exponent != 0) {
+ if(exponent % 2 == 1) {
+ a_zm1_over_gcd =
+ mod_type::mult_add(
+ b_gcd,
+ mod_type::mult(a_zm1_over_gcd, a_km1_over_gcd),
+ mod_type::add(a_zm1_over_gcd, a_km1_over_gcd));
+ }
+ a_km1_over_gcd = mod_type::mult_add(
+ b_gcd,
+ mod_type::mult(a_km1_over_gcd, a_km1_over_gcd),
+ mod_type::add(a_km1_over_gcd, a_km1_over_gcd));
+ exponent /= 2;
+ }
+
+ IntType a_z = mod_type::mult_add(b_gcd, a_zm1_over_gcd, 1);
+ IntType num = mod_type::mult(c, a_zm1_over_gcd);
+ b_inv = mod_type::invert((a-1)/b_gcd);
+ _x = mod_type::mult_add(a_z, _x, mod_type::mult(b_inv, num));
+ }
+ }
+
+ friend bool operator==(const linear_congruential_engine& x,
+ const linear_congruential_engine& y)
+ { return x._x == y._x; }
+ friend bool operator!=(const linear_congruential_engine& x,
+ const linear_congruential_engine& y)
+ { return !(x == y); }