123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- #ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
- #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
- #include <iostream>
- #include <stdexcept>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/limits.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/type_traits/is_arithmetic.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/const_mod.hpp>
- #include <boost/random/detail/seed.hpp>
- #include <boost/random/detail/seed_impl.hpp>
- #include <boost/detail/workaround.hpp>
- #include <boost/random/detail/disable_warnings.hpp>
- namespace boost {
- namespace random {
- template<class IntType, IntType a, IntType c, IntType m>
- class linear_congruential_engine
- {
- public:
- typedef IntType result_type;
-
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
- BOOST_STATIC_CONSTANT(IntType, multiplier = a);
- BOOST_STATIC_CONSTANT(IntType, increment = c);
- BOOST_STATIC_CONSTANT(IntType, modulus = m);
- BOOST_STATIC_CONSTANT(IntType, default_seed = 1);
-
- BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
- BOOST_STATIC_ASSERT(m == 0 || a < m);
- BOOST_STATIC_ASSERT(m == 0 || c < m);
-
-
- linear_congruential_engine() { seed(); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_congruential_engine,
- IntType, x0)
- { seed(x0); }
-
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_congruential_engine,
- SeedSeq, seq)
- { seed(seq); }
-
- template<class It>
- linear_congruential_engine(It& first, It last)
- {
- seed(first, last);
- }
-
-
- void seed() { seed(default_seed); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_congruential_engine, IntType, x0_)
- {
-
-
-
- IntType x0 = x0_;
-
- if(modulus == 0) {
- _x = x0;
- } else {
- _x = x0 % modulus;
- }
-
- if(_x < 0) {
- _x += modulus;
- }
-
- if(increment == 0 && _x == 0) {
- _x = 1;
- }
- BOOST_ASSERT(_x >= (min)());
- BOOST_ASSERT(_x <= (max)());
- }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_congruential_engine, SeedSeq, seq)
- { seed(detail::seed_one_int<IntType, m>(seq)); }
-
- template<class It>
- void seed(It& first, It last)
- { seed(detail::get_one_int<IntType, m>(first, last)); }
-
- static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return c == 0 ? 1 : 0; }
-
- static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return modulus-1; }
-
- IntType operator()()
- {
- _x = const_mod<IntType, m>::mult_add(a, _x, c);
- return _x;
- }
-
-
- template<class Iter>
- void generate(Iter first, Iter last)
- { detail::generate_from_int(*this, first, last); }
-
- 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 {
-
-
- 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); }
-
- #if !defined(BOOST_RANDOM_NO_STREAM_OPERATORS)
-
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os,
- const linear_congruential_engine& lcg)
- {
- return os << lcg._x;
- }
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is,
- linear_congruential_engine& lcg)
- {
- lcg.read(is);
- return is;
- }
- #endif
- private:
-
- template<class CharT, class Traits>
- void read(std::basic_istream<CharT, Traits>& is) {
- IntType x;
- if(is >> x) {
- if(x >= (min)() && x <= (max)()) {
- _x = x;
- } else {
- is.setstate(std::ios_base::failbit);
- }
- }
- }
-
- IntType _x;
- };
- #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- template<class IntType, IntType a, IntType c, IntType m>
- const bool linear_congruential_engine<IntType, a, c, m>::has_fixed_range;
- template<class IntType, IntType a, IntType c, IntType m>
- const IntType linear_congruential_engine<IntType,a,c,m>::multiplier;
- template<class IntType, IntType a, IntType c, IntType m>
- const IntType linear_congruential_engine<IntType,a,c,m>::increment;
- template<class IntType, IntType a, IntType c, IntType m>
- const IntType linear_congruential_engine<IntType,a,c,m>::modulus;
- template<class IntType, IntType a, IntType c, IntType m>
- const IntType linear_congruential_engine<IntType,a,c,m>::default_seed;
- #endif
- template<class IntType, IntType a, IntType c, IntType m, IntType val = 0>
- class linear_congruential : public linear_congruential_engine<IntType, a, c, m>
- {
- typedef linear_congruential_engine<IntType, a, c, m> base_type;
- public:
- linear_congruential(IntType x0 = 1) : base_type(x0) {}
- template<class It>
- linear_congruential(It& first, It last) : base_type(first, last) {}
- };
- typedef linear_congruential_engine<uint32_t, 16807, 0, 2147483647> minstd_rand0;
- typedef linear_congruential_engine<uint32_t, 48271, 0, 2147483647> minstd_rand;
- #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
- class rand48
- {
- public:
- typedef boost::uint32_t result_type;
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
-
- static BOOST_CONSTEXPR uint32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
-
- static BOOST_CONSTEXPR uint32_t max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return 0x7FFFFFFF; }
-
-
- rand48() : lcf(cnv(static_cast<uint32_t>(1))) {}
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(rand48, result_type, x0)
- { seed(x0); }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(rand48, SeedSeq, seq)
- { seed(seq); }
-
- template<class It> rand48(It& first, It last) : lcf(first, last) { }
-
-
- void seed() { seed(static_cast<uint32_t>(1)); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(rand48, result_type, x0)
- { lcf.seed(cnv(x0)); }
-
- template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(rand48, SeedSeq, seq)
- { lcf.seed(seq); }
-
- uint32_t operator()() { return static_cast<uint32_t>(lcf() >> 17); }
-
-
- void discard(boost::uintmax_t z) { lcf.discard(z); }
-
-
- template<class Iter>
- void generate(Iter first, Iter last)
- {
- for(; first != last; ++first) {
- *first = (*this)();
- }
- }
- #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
-
- template<class CharT,class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
- { os << r.lcf; return os; }
-
- template<class CharT,class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
- { is >> r.lcf; return is; }
- #endif
-
- friend bool operator==(const rand48& x, const rand48& y)
- { return x.lcf == y.lcf; }
-
- friend bool operator!=(const rand48& x, const rand48& y)
- { return !(x == y); }
- private:
-
- typedef random::linear_congruential_engine<uint64_t,
-
- uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32),
- 0xB, uint64_t(1)<<48> lcf_t;
- lcf_t lcf;
- static boost::uint64_t cnv(boost::uint32_t x)
- { return (static_cast<uint64_t>(x) << 16) | 0x330e; }
-
- };
- #endif
- }
- using random::minstd_rand0;
- using random::minstd_rand;
- using random::rand48;
- }
- #include <boost/random/detail/enable_warnings.hpp>
- #endif
|