Chris@16: /* boost random/shuffle_order.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2010 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: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_SHUFFLE_ORDER_HPP Chris@16: #define BOOST_RANDOM_SHUFFLE_ORDER_HPP Chris@16: Chris@16: #include Chris@16: #include // std::copy 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: * Instatiations of class template @c shuffle_order_engine model a Chris@16: * \pseudo_random_number_generator. It mixes the output Chris@16: * of some (usually \linear_congruential_engine) Chris@16: * \uniform_random_number_generator to get better statistical properties. Chris@16: * The algorithm is described in Chris@16: * Chris@16: * @blockquote Chris@16: * "Improving a poor random number generator", Carter Bays Chris@16: * and S.D. Durham, ACM Transactions on Mathematical Software, Chris@16: * Vol 2, No. 1, March 1976, pp. 59-64. Chris@16: * http://doi.acm.org/10.1145/355666.355670 Chris@16: * @endblockquote Chris@16: * Chris@16: * The output of the base generator is buffered in an array of Chris@16: * length k. Every output X(n) has a second role: It gives an Chris@16: * index into the array where X(n+1) will be retrieved. Used Chris@16: * array elements are replaced with fresh output from the base Chris@16: * generator. Chris@16: * Chris@16: * Template parameters are the base generator and the array Chris@16: * length k, which should be around 100. Chris@16: */ Chris@16: template Chris@16: class shuffle_order_engine Chris@16: { Chris@16: public: Chris@16: typedef UniformRandomNumberGenerator base_type; Chris@16: typedef typename base_type::result_type result_type; Chris@16: Chris@16: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); Chris@16: BOOST_STATIC_CONSTANT(std::size_t, buffer_size = k); Chris@16: BOOST_STATIC_CONSTANT(std::size_t, table_size = k); Chris@16: Chris@16: BOOST_STATIC_ASSERT(std::numeric_limits::is_integer); Chris@16: BOOST_STATIC_ASSERT(k > 0); Chris@16: Chris@16: /** Chris@16: * Constructs a @c shuffle_order_engine by invoking the Chris@16: * default constructor of the base generator. Chris@16: * Chris@16: * Complexity: Exactly k+1 invocations of the base generator. Chris@16: */ Chris@16: shuffle_order_engine() : _rng() { init(); } Chris@16: /** Chris@16: * Constructs a @c shuffle_output_engine by invoking the one-argument Chris@16: * constructor of the base generator with the parameter seed. Chris@16: * Chris@16: * Complexity: Exactly k+1 invocations of the base generator. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(shuffle_order_engine, Chris@16: result_type, s) Chris@16: { _rng.seed(s); init(); } Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(shuffle_order_engine, SeedSeq, seq) Chris@16: { _rng.seed(seq); init(); } Chris@16: /** Chris@16: * Constructs a @c shuffle_output_engine by using a copy Chris@16: * of the provided generator. Chris@16: * Chris@16: * Precondition: The template argument UniformRandomNumberGenerator Chris@16: * shall denote a CopyConstructible type. Chris@16: * Chris@16: * Complexity: Exactly k+1 invocations of the base generator. Chris@16: */ Chris@16: explicit shuffle_order_engine(const base_type & rng) : _rng(rng) { init(); } Chris@16: Chris@101: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: explicit shuffle_order_engine(base_type&& rng) : _rng(rng) { init(); } Chris@16: #endif Chris@16: Chris@16: template shuffle_order_engine(It& first, It last) Chris@16: : _rng(first, last) { init(); } Chris@16: void seed() { _rng.seed(); init(); } Chris@16: /** Chris@16: * Invokes the one-argument seed method of the base generator Chris@16: * with the parameter seed and re-initializes the internal buffer array. Chris@16: * Chris@16: * Complexity: Exactly k+1 invocations of the base generator. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(shuffle_order_engine, Chris@16: result_type, seed_arg) Chris@16: { _rng.seed(seed_arg); init(); } Chris@16: /** Chris@16: * Invokes the one-argument seed method of the base generator Chris@16: * with the parameter seq and re-initializes the internal buffer array. Chris@16: * Chris@16: * Complexity: Exactly k+1 invocations of the base generator. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(shuffle_order_engine, SeedSeq, seq) Chris@16: { _rng.seed(seq); init(); } Chris@16: template void seed(It& first, It last) Chris@16: { _rng.seed(first, last); init(); } Chris@16: Chris@16: const base_type& base() const { return _rng; } Chris@16: Chris@16: result_type operator()() { Chris@16: // calculating the range every time may seem wasteful. However, this Chris@16: // makes the information locally available for the optimizer. Chris@16: typedef typename make_unsigned::type base_unsigned; Chris@16: const base_unsigned brange = Chris@16: detail::subtract()((max)(), (min)()); Chris@16: const base_unsigned off = Chris@16: detail::subtract()(y, (min)()); Chris@16: Chris@16: base_unsigned j; Chris@16: if(k == 1) { Chris@16: j = 0; Chris@16: } else if(brange < (std::numeric_limits::max)() / k) { Chris@16: // try to do it in the native type if we know that it won't Chris@16: // overflow Chris@16: j = k * off / (brange + 1); Chris@16: } else if(brange < (std::numeric_limits::max)() / k) { Chris@16: // Otherwise try to use uint64_t Chris@16: j = static_cast( Chris@16: static_cast(off) * k / Chris@16: (static_cast(brange) + 1)); Chris@16: } else { Chris@16: boost::uintmax_t divisor = Chris@16: static_cast(brange) + 1; Chris@16: j = static_cast(detail::muldiv(off, k, divisor)); Chris@16: } Chris@16: // assert(0 <= j && j < k); Chris@16: y = v[j]; Chris@16: v[j] = _rng(); Chris@16: return y; Chris@16: } Chris@16: Chris@16: /** Advances the generator by z steps. */ Chris@16: void discard(boost::uintmax_t z) Chris@16: { Chris@16: for(boost::uintmax_t j = 0; j < z; ++j) { Chris@16: (*this)(); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Fills a range with pseudo-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: /** Returns the smallest value that the generator can produce. */ Chris@16: static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { return (base_type::min)(); } Chris@16: /** Returns the largest value that the generator can produce. */ Chris@16: static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () Chris@16: { return (base_type::max)(); } Chris@16: Chris@16: /** Writes a @c shuffle_order_engine to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, shuffle_order_engine, s) Chris@16: { Chris@16: os << s._rng; Chris@16: for(std::size_t i = 0; i < k; ++i) Chris@16: os << ' ' << s.v[i]; Chris@16: os << ' ' << s.y; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads a @c shuffle_order_engine from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, shuffle_order_engine, s) Chris@16: { Chris@16: is >> s._rng; Chris@16: for(std::size_t i = 0; i < k; ++i) Chris@16: is >> std::ws >> s.v[i]; Chris@16: is >> std::ws >> s.y; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two generators will produce identical sequences. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(shuffle_order_engine, x, y) Chris@16: { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); } Chris@16: /** Returns true if the two generators will produce different sequences. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(shuffle_order_engine) Chris@16: Chris@16: private: Chris@16: Chris@16: /// \cond show_private Chris@16: Chris@16: void init() Chris@16: { Chris@16: // we cannot use std::generate, because it uses pass-by-value for _rng Chris@16: for(result_type * p = v; p != v+k; ++p) Chris@16: *p = _rng(); Chris@16: y = _rng(); Chris@16: } Chris@16: Chris@16: /// \endcond Chris@16: Chris@16: base_type _rng; Chris@16: result_type v[k]; Chris@16: result_type y; 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 shuffle_order_engine::has_fixed_range; Chris@16: template Chris@16: const std::size_t shuffle_order_engine::table_size; Chris@16: template Chris@16: const std::size_t shuffle_order_engine::buffer_size; Chris@16: #endif Chris@16: Chris@16: /** Chris@16: * According to Harry Erwin (private e-mail), the specialization Chris@16: * @c kreutzer1986 was suggested in: Chris@16: * Chris@16: * @blockquote Chris@16: * "System Simulation: Programming Styles and Languages (International Chris@16: * Computer Science Series)", Wolfgang Kreutzer, Addison-Wesley, December 1986. Chris@16: * @endblockquote Chris@16: */ Chris@16: typedef shuffle_order_engine< Chris@16: linear_congruential_engine, Chris@16: 97> kreutzer1986; Chris@16: Chris@16: /** Chris@16: * The specialization @c knuth_b is specified by the C++ standard. Chris@16: * It is described in Chris@16: * Chris@16: * @blockquote Chris@16: * "The Art of Computer Programming, Second Edition, Volume 2, Chris@16: * Seminumerical Algorithms", Donald Knuth, Addison-Wesley, 1981. Chris@16: * @endblockquote Chris@16: */ Chris@16: typedef shuffle_order_engine knuth_b; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::kreutzer1986; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP