Chris@16: /* boost random/linear_congruential.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-02-18 moved to individual header files Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP Chris@16: #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * Instantiations of class template linear_congruential_engine model a Chris@16: * \pseudo_random_number_generator. Linear congruential pseudo-random Chris@16: * number generators are described in: Chris@16: * Chris@16: * @blockquote Chris@16: * "Mathematical methods in large-scale computing units", D. H. Lehmer, Chris@16: * Proc. 2nd Symposium on Large-Scale Digital Calculating Machines, Chris@16: * Harvard University Press, 1951, pp. 141-146 Chris@16: * @endblockquote Chris@16: * Chris@16: * Let x(n) denote the sequence of numbers returned by some pseudo-random Chris@16: * number generator. Then for the linear congruential generator, Chris@16: * x(n+1) := (a * x(n) + c) mod m. Parameters for the generator are Chris@16: * x(0), a, c, m. The template parameter IntType shall denote an integral Chris@16: * type. It must be large enough to hold values a, c, and m. The template Chris@16: * parameters a and c must be smaller than m. Chris@16: * Chris@16: * Note: The quality of the generator crucially depends on the choice of Chris@16: * the parameters. User code should use one of the sensibly parameterized Chris@16: * generators such as minstd_rand instead. Chris@16: */ Chris@16: template Chris@16: class linear_congruential_engine Chris@16: { Chris@16: public: Chris@16: typedef IntType result_type; Chris@16: Chris@16: // Required for old Boost.Random concept Chris@16: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); Chris@16: Chris@16: BOOST_STATIC_CONSTANT(IntType, multiplier = a); Chris@16: BOOST_STATIC_CONSTANT(IntType, increment = c); Chris@16: BOOST_STATIC_CONSTANT(IntType, modulus = m); Chris@16: BOOST_STATIC_CONSTANT(IntType, default_seed = 1); Chris@16: Chris@16: BOOST_STATIC_ASSERT(std::numeric_limits::is_integer); Chris@16: BOOST_STATIC_ASSERT(m == 0 || a < m); Chris@16: BOOST_STATIC_ASSERT(m == 0 || c < m); Chris@16: Chris@16: /** Chris@16: * Constructs a @c linear_congruential_engine, using the default seed Chris@16: */ Chris@16: linear_congruential_engine() { seed(); } Chris@16: Chris@16: /** Chris@16: * Constructs a @c linear_congruential_engine, seeding it with @c x0. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_congruential_engine, Chris@16: IntType, x0) Chris@16: { seed(x0); } Chris@16: Chris@16: /** Chris@16: * Constructs a @c linear_congruential_engine, seeding it with values Chris@16: * produced by a call to @c seq.generate(). Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_congruential_engine, Chris@16: SeedSeq, seq) Chris@16: { seed(seq); } Chris@16: Chris@16: /** Chris@16: * Constructs a @c linear_congruential_engine and seeds it Chris@16: * with values taken from the itrator range [first, last) Chris@16: * and adjusts first to point to the element after the last one Chris@16: * used. If there are not enough elements, throws @c std::invalid_argument. Chris@16: * Chris@16: * first and last must be input iterators. Chris@16: */ Chris@16: template Chris@16: linear_congruential_engine(It& first, It last) Chris@16: { Chris@16: seed(first, last); Chris@16: } Chris@16: Chris@16: // compiler-generated copy constructor and assignment operator are fine Chris@16: Chris@16: /** Chris@16: * Calls seed(default_seed) Chris@16: */ Chris@16: void seed() { seed(default_seed); } Chris@16: Chris@16: /** Chris@16: * If c mod m is zero and x0 mod m is zero, changes the current value of Chris@16: * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero, Chris@16: * distinct seeds in the range [1,m) will leave the generator in distinct Chris@16: * states. If c is not zero, the range is [0,m). Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_congruential_engine, IntType, x0) Chris@16: { Chris@16: // wrap _x if it doesn't fit in the destination Chris@16: if(modulus == 0) { Chris@16: _x = x0; Chris@16: } else { Chris@16: _x = x0 % modulus; Chris@16: } Chris@16: // handle negative seeds Chris@16: if(_x <= 0 && _x != 0) { Chris@16: _x += modulus; Chris@16: } Chris@16: // adjust to the correct range Chris@16: if(increment == 0 && _x == 0) { Chris@16: _x = 1; Chris@16: } Chris@16: BOOST_ASSERT(_x >= (min)()); Chris@16: BOOST_ASSERT(_x <= (max)()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Seeds a @c linear_congruential_engine using values from a SeedSeq. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_congruential_engine, SeedSeq, seq) Chris@16: { seed(detail::seed_one_int(seq)); } Chris@16: Chris@16: /** Chris@16: * seeds a @c linear_congruential_engine with values taken Chris@16: * from the itrator range [first, last) and adjusts @c first to Chris@16: * point to the element after the last one used. If there are Chris@16: * not enough elements, throws @c std::invalid_argument. Chris@16: * Chris@16: * @c first and @c last must be input iterators. Chris@16: */ Chris@16: template Chris@16: void seed(It& first, It last) Chris@16: { seed(detail::get_one_int(first, last)); } Chris@16: Chris@16: /** Chris@16: * Returns the smallest value that the @c linear_congruential_engine Chris@16: * can produce. Chris@16: */ Chris@16: static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { return c == 0 ? 1 : 0; } Chris@16: /** Chris@16: * Returns the largest value that the @c linear_congruential_engine Chris@16: * can produce. Chris@16: */ Chris@16: static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { return modulus-1; } Chris@16: Chris@16: /** Returns the next value of the @c linear_congruential_engine. */ Chris@16: IntType operator()() Chris@16: { Chris@16: _x = const_mod::mult_add(a, _x, c); Chris@16: return _x; Chris@16: } Chris@16: Chris@16: /** Fills a range with random values */ Chris@16: template Chris@16: void generate(Iter first, Iter last) Chris@16: { detail::generate_from_int(*this, first, last); } Chris@16: Chris@16: /** Advances the state of the generator by @c z. */ Chris@16: void discard(boost::uintmax_t z) Chris@16: { Chris@16: typedef const_mod mod_type; Chris@16: IntType b_inv = mod_type::invert(a-1); Chris@16: IntType b_gcd = mod_type::mult(a-1, b_inv); Chris@16: if(b_gcd == 1) { Chris@16: IntType a_z = mod_type::pow(a, z); Chris@16: _x = mod_type::mult_add(a_z, _x, Chris@16: mod_type::mult(mod_type::mult(c, b_inv), a_z - 1)); Chris@16: } else { Chris@16: // compute (a^z - 1)*c % (b_gcd * m) / (b / b_gcd) * inv(b / b_gcd) Chris@16: // we're storing the intermediate result / b_gcd Chris@16: IntType a_zm1_over_gcd = 0; Chris@16: IntType a_km1_over_gcd = (a - 1) / b_gcd; Chris@16: boost::uintmax_t exponent = z; Chris@16: while(exponent != 0) { Chris@16: if(exponent % 2 == 1) { Chris@16: a_zm1_over_gcd = Chris@16: mod_type::mult_add( Chris@16: b_gcd, Chris@16: mod_type::mult(a_zm1_over_gcd, a_km1_over_gcd), Chris@16: mod_type::add(a_zm1_over_gcd, a_km1_over_gcd)); Chris@16: } Chris@16: a_km1_over_gcd = mod_type::mult_add( Chris@16: b_gcd, Chris@16: mod_type::mult(a_km1_over_gcd, a_km1_over_gcd), Chris@16: mod_type::add(a_km1_over_gcd, a_km1_over_gcd)); Chris@16: exponent /= 2; Chris@16: } Chris@16: Chris@16: IntType a_z = mod_type::mult_add(b_gcd, a_zm1_over_gcd, 1); Chris@16: IntType num = mod_type::mult(c, a_zm1_over_gcd); Chris@16: b_inv = mod_type::invert((a-1)/b_gcd); Chris@16: _x = mod_type::mult_add(a_z, _x, mod_type::mult(b_inv, num)); Chris@16: } Chris@16: } Chris@16: Chris@16: friend bool operator==(const linear_congruential_engine& x, Chris@16: const linear_congruential_engine& y) Chris@16: { return x._x == y._x; } Chris@16: friend bool operator!=(const linear_congruential_engine& x, Chris@16: const linear_congruential_engine& y) Chris@16: { return !(x == y); } Chris@16: Chris@16: #if !defined(BOOST_RANDOM_NO_STREAM_OPERATORS) Chris@16: /** Writes a @c linear_congruential_engine to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const linear_congruential_engine& lcg) Chris@16: { Chris@16: return os << lcg._x; Chris@16: } Chris@16: Chris@16: /** Reads a @c linear_congruential_engine from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, Chris@16: linear_congruential_engine& lcg) Chris@16: { Chris@16: lcg.read(is); Chris@16: return is; Chris@16: } Chris@16: #endif Chris@16: Chris@16: private: Chris@16: Chris@16: /// \cond show_private Chris@16: Chris@16: template Chris@16: void read(std::basic_istream& is) { Chris@16: IntType x; Chris@16: if(is >> x) { Chris@16: if(x >= (min)() && x <= (max)()) { Chris@16: _x = x; Chris@16: } else { Chris@16: is.setstate(std::ios_base::failbit); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: /// \endcond Chris@16: Chris@16: IntType _x; Chris@16: }; Chris@16: Chris@16: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION Chris@16: // A definition is required even for integral static constants Chris@16: template Chris@16: const bool linear_congruential_engine::has_fixed_range; Chris@16: template Chris@16: const IntType linear_congruential_engine::multiplier; Chris@16: template Chris@16: const IntType linear_congruential_engine::increment; Chris@16: template Chris@16: const IntType linear_congruential_engine::modulus; Chris@16: template Chris@16: const IntType linear_congruential_engine::default_seed; Chris@16: #endif Chris@16: Chris@16: /// \cond show_deprecated Chris@16: Chris@16: // provided for backwards compatibility Chris@16: template Chris@16: class linear_congruential : public linear_congruential_engine Chris@16: { Chris@16: typedef linear_congruential_engine base_type; Chris@16: public: Chris@16: linear_congruential(IntType x0 = 1) : base_type(x0) {} Chris@16: template Chris@16: linear_congruential(It& first, It last) : base_type(first, last) {} Chris@16: }; Chris@16: Chris@16: /// \endcond Chris@16: Chris@16: /** Chris@16: * The specialization \minstd_rand0 was originally suggested in Chris@16: * Chris@16: * @blockquote Chris@16: * A pseudo-random number generator for the System/360, P.A. Lewis, Chris@16: * A.S. Goodman, J.M. Miller, IBM Systems Journal, Vol. 8, No. 2, Chris@16: * 1969, pp. 136-146 Chris@16: * @endblockquote Chris@16: * Chris@16: * It is examined more closely together with \minstd_rand in Chris@16: * Chris@16: * @blockquote Chris@16: * "Random Number Generators: Good ones are hard to find", Chris@16: * Stephen K. Park and Keith W. Miller, Communications of Chris@16: * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201 Chris@16: * @endblockquote Chris@16: */ Chris@16: typedef linear_congruential_engine minstd_rand0; Chris@16: Chris@16: /** The specialization \minstd_rand was suggested in Chris@16: * Chris@16: * @blockquote Chris@16: * "Random Number Generators: Good ones are hard to find", Chris@16: * Stephen K. Park and Keith W. Miller, Communications of Chris@16: * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201 Chris@16: * @endblockquote Chris@16: */ Chris@16: typedef linear_congruential_engine minstd_rand; Chris@16: Chris@16: Chris@16: #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T) Chris@16: /** Chris@16: * Class @c rand48 models a \pseudo_random_number_generator. It uses Chris@16: * the linear congruential algorithm with the parameters a = 0x5DEECE66D, Chris@16: * c = 0xB, m = 2**48. It delivers identical results to the @c lrand48() Chris@16: * function available on some systems (assuming lcong48 has not been called). Chris@16: * Chris@16: * It is only available on systems where @c uint64_t is provided as an Chris@16: * integral type, so that for example static in-class constants and/or Chris@16: * enum definitions with large @c uint64_t numbers work. Chris@16: */ Chris@16: class rand48 Chris@16: { Chris@16: public: Chris@16: typedef boost::uint32_t result_type; Chris@16: Chris@16: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); Chris@16: /** Chris@16: * Returns the smallest value that the generator can produce Chris@16: */ Chris@16: static uint32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } Chris@16: /** Chris@16: * Returns the largest value that the generator can produce Chris@16: */ Chris@16: static uint32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { return 0x7FFFFFFF; } Chris@16: Chris@16: /** Seeds the generator with the default seed. */ Chris@16: rand48() : lcf(cnv(static_cast(1))) {} Chris@16: /** Chris@16: * Constructs a \rand48 generator with x(0) := (x0 << 16) | 0x330e. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(rand48, result_type, x0) Chris@16: { seed(x0); } Chris@16: /** Chris@16: * Seeds the generator with values produced by @c seq.generate(). Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(rand48, SeedSeq, seq) Chris@16: { seed(seq); } Chris@16: /** Chris@16: * Seeds the generator using values from an iterator range, Chris@16: * and updates first to point one past the last value consumed. Chris@16: */ Chris@16: template rand48(It& first, It last) : lcf(first, last) { } Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: /** Seeds the generator with the default seed. */ Chris@16: void seed() { seed(static_cast(1)); } Chris@16: /** Chris@16: * Changes the current value x(n) of the generator to (x0 << 16) | 0x330e. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(rand48, result_type, x0) Chris@16: { lcf.seed(cnv(x0)); } Chris@16: /** Chris@16: * Seeds the generator using values from an iterator range, Chris@16: * and updates first to point one past the last value consumed. Chris@16: */ Chris@16: template void seed(It& first, It last) { lcf.seed(first,last); } Chris@16: /** Chris@16: * Seeds the generator with values produced by @c seq.generate(). Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(rand48, SeedSeq, seq) Chris@16: { lcf.seed(seq); } Chris@16: Chris@16: /** Returns the next value of the generator. */ Chris@16: uint32_t operator()() { return static_cast(lcf() >> 17); } Chris@16: Chris@16: /** Advances the state of the generator by @c z. */ Chris@16: void discard(boost::uintmax_t z) { lcf.discard(z); } Chris@16: Chris@16: /** Fills a range with random values */ Chris@16: template Chris@16: void generate(Iter first, Iter last) Chris@16: { Chris@16: for(; first != last; ++first) { Chris@16: *first = (*this)(); Chris@16: } Chris@16: } Chris@16: Chris@16: #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS Chris@16: /** Writes a @c rand48 to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const rand48& r) Chris@16: { os << r.lcf; return os; } Chris@16: Chris@16: /** Reads a @c rand48 from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, rand48& r) Chris@16: { is >> r.lcf; return is; } Chris@16: #endif Chris@16: Chris@16: /** Chris@16: * Returns true if the two generators will produce identical Chris@16: * sequences of values. Chris@16: */ Chris@16: friend bool operator==(const rand48& x, const rand48& y) Chris@16: { return x.lcf == y.lcf; } Chris@16: /** Chris@16: * Returns true if the two generators will produce different Chris@16: * sequences of values. Chris@16: */ Chris@16: friend bool operator!=(const rand48& x, const rand48& y) Chris@16: { return !(x == y); } Chris@16: private: Chris@16: /// \cond show_private Chris@16: typedef random::linear_congruential_engine lcf_t; Chris@16: lcf_t lcf; Chris@16: Chris@16: static boost::uint64_t cnv(boost::uint32_t x) Chris@16: { return (static_cast(x) << 16) | 0x330e; } Chris@16: /// \endcond Chris@16: }; Chris@16: #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */ Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::minstd_rand0; Chris@16: using random::minstd_rand; Chris@16: using random::rand48; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP