|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r63270 - in trunk: boost/random libs/random/doc
From: steven_at_[hidden]
Date: 2010-06-23 16:55:44
Author: steven_watanabe
Date: 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
New Revision: 63270
URL: http://svn.boost.org/trac/boost/changeset/63270
Log:
Sync linear_congruential with C++0x.
Text files modified:
trunk/boost/random/additive_combine.hpp | 18
trunk/boost/random/lagged_fibonacci.hpp | 2
trunk/boost/random/linear_congruential.hpp | 645 +++++++++++++++++++++------------------
trunk/boost/random/shuffle_output.hpp | 6
trunk/libs/random/doc/Jamfile.v2 | 12
trunk/libs/random/doc/concepts.qbk | 3
trunk/libs/random/doc/random.qbk | 6
7 files changed, 370 insertions(+), 322 deletions(-)
Modified: trunk/boost/random/additive_combine.hpp
==============================================================================
--- trunk/boost/random/additive_combine.hpp (original)
+++ trunk/boost/random/additive_combine.hpp 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -29,7 +29,7 @@
/**
* An instantiation of class template \additive_combine model a
* \pseudo_random_number_generator. It combines two multiplicative
- * \linear_congruential number generators, i.e. those with @c c = 0.
+ * \linear_congruential_engine number generators, i.e. those with @c c = 0.
* It is described in
*
* @blockquote
@@ -38,7 +38,7 @@
* @endblockquote
*
* The template parameters MLCG1 and MLCG2 shall denote two different
- * \linear_congruential number generators, each with c = 0. Each invocation
+ * \linear_congruential_engine number generators, each with c = 0. Each invocation
* returns a random number X(n) := (MLCG1(n) - MLCG2(n)) mod (m1 - 1), where
* m1 denotes the modulus of MLCG1.
*
@@ -57,13 +57,9 @@
typedef MLCG1 first_base;
typedef MLCG2 second_base;
typedef typename MLCG1::result_type result_type;
-#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- static const bool has_fixed_range = true;
- static const result_type min_value = 1;
- static const result_type max_value = MLCG1::max_value-1;
-#else
- enum { has_fixed_range = false };
-#endif
+
+ // Required by old Boost.Random concept
+ BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
/**
* Returns: The smallest value that the generator can produce
*/
@@ -225,8 +221,8 @@
* @endblockquote
*/
typedef random::additive_combine<
- random::linear_congruential<int32_t, 40014, 0, 2147483563, 0>,
- random::linear_congruential<int32_t, 40692, 0, 2147483399, 0>,
+ random::linear_congruential_engine<int32_t, 40014, 0, 2147483563>,
+ random::linear_congruential_engine<int32_t, 40692, 0, 2147483399>,
2060321752> ecuyer1988;
} // namespace boost
Modified: trunk/boost/random/lagged_fibonacci.hpp
==============================================================================
--- trunk/boost/random/lagged_fibonacci.hpp (original)
+++ trunk/boost/random/lagged_fibonacci.hpp 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -363,7 +363,7 @@
* value and calls seed with it. Distinct seeds in the range
* [1, 2147483647) will produce generators with different states. Other
* seeds will be equivalent to some seed within this range. See
- * \linear_congruential for details.
+ * \linear_congruential_engine for details.
*/
BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(lagged_fibonacci_01, uint32_t, value)
{
Modified: trunk/boost/random/linear_congruential.hpp
==============================================================================
--- trunk/boost/random/linear_congruential.hpp (original)
+++ trunk/boost/random/linear_congruential.hpp 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -22,8 +22,10 @@
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/static_assert.hpp>
+#include <boost/integer/static_log2.hpp>
#include <boost/random/detail/config.hpp>
#include <boost/random/detail/const_mod.hpp>
+#include <boost/random/detail/seed.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/random/detail/disable_warnings.hpp>
@@ -32,13 +34,15 @@
namespace random {
/**
- * Instantiations of class template linear_congruential model a
+ * Instantiations of class template linear_congruential_engine model a
* \pseudo_random_number_generator. Linear congruential pseudo-random
* number generators are described in:
*
+ * @blockquote
* "Mathematical methods in large-scale computing units", D. H. Lehmer,
* Proc. 2nd Symposium on Large-Scale Digital Calculating Machines,
* Harvard University Press, 1951, pp. 141-146
+ * @endblockquote
*
* Let x(n) denote the sequence of numbers returned by some pseudo-random
* number generator. Then for the linear congruential generator,
@@ -51,230 +55,243 @@
* the parameters. User code should use one of the sensibly parameterized
* generators such as minstd_rand instead.
*/
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-class linear_congruential
+template<class IntType, IntType a, IntType c, IntType m>
+class linear_congruential_engine
{
public:
- typedef IntType result_type;
-#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- static const bool has_fixed_range = true;
- static const result_type min_value = ( c == 0 ? 1 : 0 );
- static const result_type max_value = m-1;
-#else
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
-#endif
- BOOST_STATIC_CONSTANT(IntType, multiplier = a);
- BOOST_STATIC_CONSTANT(IntType, increment = c);
- BOOST_STATIC_CONSTANT(IntType, modulus = m);
-
- // MSVC 6 and possibly others crash when encountering complicated integral
- // constant expressions. Avoid the check for now.
- // BOOST_STATIC_ASSERT(m == 0 || a < m);
- // BOOST_STATIC_ASSERT(m == 0 || c < m);
-
- /**
- * Constructs a linear_congruential generator, seeding it with @c x0.
- */
- explicit linear_congruential(IntType x0 = 1)
- {
- seed(x0);
+ typedef IntType result_type;
- // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
-#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
+ // Required for old Boost.Random concept
+ 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);
-#endif
- }
+ BOOST_STATIC_ASSERT(m == 0 || a < m);
+ BOOST_STATIC_ASSERT(m == 0 || c < m);
+
+ /**
+ * Constructs a @c linear_congruential_engine, using the default seed
+ */
+ linear_congruential_engine() { seed(); }
+
+ /**
+ * Constructs a @c linear_congruential_engine, seeding it with @c x0.
+ */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_congruential_engine,
+ IntType, x0)
+ { seed(x0); }
+
+ /**
+ * Constructs a @c linear_congruential_engine, seeding it with values
+ * produced by a call to @c seq.generate().
+ */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_congruential_engine,
+ SeedSeq, seq)
+ { seed(seq); }
+
+ /**
+ * Constructs a @c linear_congruential_engine and seeds it
+ * with values taken from the itrator range [first, last)
+ * and adjusts first to point to the element after the last one
+ * used. If there are not enough elements, throws @c std::invalid_argument.
+ *
+ * first and last must be input iterators.
+ */
+ template<class It>
+ linear_congruential_engine(It& first, It last)
+ {
+ seed(first, last);
+ }
+
+ // compiler-generated copy constructor and assignment operator are fine
+
+ /**
+ * Calls seed(default_seed)
+ */
+ void seed() { seed(default_seed); }
+
+ /**
+ * If c mod m is zero and x0 mod m is zero, changes the current value of
+ * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
+ * distinct seeds in the range [1,m) will leave the generator in distinct
+ * states. If c is not zero, the range is [0,m).
+ */
+ BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_congruential_engine, IntType, x0)
+ {
+ // wrap _x if it doesn't fit in the destination
+ if(modulus == 0) {
+ _x = x0;
+ } else {
+ _x = x0 % modulus;
+ }
+ // handle negative seeds
+ if(_x <= 0 && _x != 0) {
+ _x += modulus;
+ }
+ // adjust to the correct range
+ if(increment == 0 && _x == 0) {
+ _x = 1;
+ }
+ assert(_x >= (min)());
+ assert(_x <= (max)());
+ }
+
+ /**
+ * Seeds a @c linear_congruential_engine using values from a SeedSeq.
+ */
+ BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_congruential_engine, SeedSeq, seq)
+ {
+ static const int log = ::boost::static_log2<m>::value;
+ static const int k =
+ (log + ((~(static_cast<IntType>(1) << log) & m)? 32 : 31)) / 32;
+ boost::uint32_t a[k + 3];
+ seq.generate(&a[0], &a[0] + k + 3);
+ IntType s = 0;
+ IntType mul = 1;
+ for(int j = 0; j < k; ++j) {
+ s = const_mod<IntType, m>::mult_add(a[j + 3], mul, s);
+ mul <<= 32;
+ }
+ seed(s);
+ }
- /**
- * Constructs a @c linear_congruential generator and seeds it
- * with values taken from the itrator range [first, last)
- * and adjusts first to point to the element after the last one
- * used. If there are not enough elements, throws @c std::invalid_argument.
- *
- * first and last must be input iterators.
- */
- template<class It>
- linear_congruential(It& first, It last)
- {
- seed(first, last);
- }
-
- // compiler-generated copy constructor and assignment operator are fine
-
- /**
- * If c mod m is zero and x0 mod m is zero, changes the current value of
- * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
- * distinct seeds in the range [1,m) will leave the generator in distinct
- * states. If c is not zero, the range is [0,m).
- */
- void seed(IntType x0 = 1)
- {
- // wrap _x if it doesn't fit in the destination
- if(modulus == 0) {
- _x = x0;
- } else {
- _x = x0 % modulus;
- }
- // handle negative seeds
- if(_x <= 0 && _x != 0) {
- _x += modulus;
- }
- // adjust to the correct range
- if(increment == 0 && _x == 0) {
- _x = 1;
- }
- 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;
- }
+ /**
+ * seeds a @c linear_congruential_engine 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_engine
+ * can produce.
+ */
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return c == 0 ? 1 : 0; }
+ /**
+ * Returns the largest value that the @c linear_congruential_engine
+ * can produce.
+ */
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return modulus-1; }
+
+ /** Returns the next value of the @c linear_congruential_engine. */
+ IntType operator()()
+ {
+ _x = const_mod<IntType, m>::mult_add(a, _x, c);
+ return _x;
+ }
- /** Fills a range with random values */
- template<class Iter>
- void generate(Iter first, Iter last)
- {
- for(; first != last; ++first) {
- *first = (*this)();
+ /** Fills a range with random values */
+ template<class Iter>
+ void generate(Iter first, Iter last)
+ {
+ for(; first != last; ++first) {
+ *first = (*this)();
+ }
}
- }
- static bool validation(IntType x) { return val == x; }
+#ifndef BOOST_NO_LONG_LONG
+ /** Advances the state of the generator by @c z. */
+ void discard(boost::ulong_long_type z)
+ {
+ for(boost::ulong_long_type j = 0; j < z; ++j) {
+ (*this)();
+ }
+ }
+#endif
-#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); }
+ 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) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os,
- const linear_congruential& 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& lcg)
- {
- lcg.read(is);
- return is;
- }
-
-private:
-#endif
-#endif
+#if !defined(BOOST_RANDOM_NO_STREAM_OPERATORS)
+ /** Writes a @c linear_congruential_engine to a @c std::ostream. */
+ 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>
- 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);
- }
+ /** Reads a @c linear_congruential_engine from a @c std::istream. */
+ 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
- IntType _x;
-};
+private:
-// probably needs the "no native streams" caveat for STLPort
-#if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-std::ostream&
-operator<<(std::ostream& os,
- const linear_congruential<IntType,a,c,m,val>& lcg)
-{
- return os << lcg._x;
-}
+ /// \cond
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-std::istream&
-operator>>(std::istream& is,
- linear_congruential<IntType,a,c,m,val>& lcg)
-{
- return is >> lcg._x;
-}
-#elif defined(BOOST_RANDOM_NO_STREAM_OPERATORS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
-template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
-std::basic_ostream<CharT,Traits>&
-operator<<(std::basic_ostream<CharT,Traits>& os,
- const linear_congruential<IntType,a,c,m,val>& lcg)
-{
- return os << lcg._x;
-}
+ 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);
+ }
+ }
+ }
-template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
-std::basic_istream<CharT,Traits>&
-operator>>(std::basic_istream<CharT,Traits>& is,
- linear_congruential<IntType,a,c,m,val>& lcg)
-{
- return is >> lcg._x;
-}
-#endif
+ /// \endcond
+
+ IntType _x;
+};
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
// A definition is required even for integral static constants
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
-template<class IntType, IntType a, IntType c, IntType m, IntType val>
-const IntType linear_congruential<IntType,a,c,m,val>::modulus;
-#endif
+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
+
+/// \cond
+
+// provided for backwards compatibility
+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) {}
+};
-} // namespace random
+/// \endcond
-// validation values from the publications
/**
* The specialization \minstd_rand0 was originally suggested in
*
@@ -292,8 +309,7 @@
* the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
* @endblockquote
*/
-typedef random::linear_congruential<int32_t, 16807, 0, 2147483647,
- 1043618065> minstd_rand0;
+typedef linear_congruential_engine<int32_t, 16807, 0, 2147483647> minstd_rand0;
/** The specialization \minstd_rand was suggested in
*
@@ -303,12 +319,12 @@
* the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
* @endblockquote
*/
-typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
- 399268537> minstd_rand;
+typedef linear_congruential_engine<int32_t, 48271, 0, 2147483647> minstd_rand;
#if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
-/** Class @c rand48 models a \pseudo_random_number_generator. It uses
+/**
+ * Class @c rand48 models a \pseudo_random_number_generator. It uses
* the linear congruential algorithm with the parameters a = 0x5DEECE66D,
* c = 0xB, m = 2**48. It delivers identical results to the @c lrand48()
* function available on some systems (assuming lcong48 has not been called).
@@ -320,104 +336,137 @@
class rand48
{
public:
- typedef int32_t result_type;
-#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- static const bool has_fixed_range = true;
- static const int32_t min_value = 0;
- static const int32_t max_value = integer_traits<int32_t>::const_max;
-#else
- enum { has_fixed_range = false };
-#endif
- /**
- * Returns the smallest value that the generator can produce
- */
- int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
- /**
- * Returns the largest value that the generator can produce
- */
- int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
-
-#ifdef BOOST_RANDOM_DOXYGEN
- /**
- * If T is an integral type smaller than int46_t, constructs
- * a \rand48 generator with x(0) := (x0 << 16) | 0x330e. Otherwise
- * constructs a \rand48 generator with x(0) = x0.
- */
- template<class T> explicit rand48(T x0 = 1);
-#else
- rand48() : lcf(cnv(static_cast<int32_t>(1))) {}
- template<class T> explicit rand48(T x0) : lcf(cnv(x0)) { }
-#endif
- template<class It> rand48(It& first, It last) : lcf(first, last) { }
+ typedef int32_t result_type;
- // compiler-generated copy ctor and assignment operator are fine
+ BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+ /**
+ * Returns the smallest value that the generator can produce
+ */
+ static int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
+ /**
+ * Returns the largest value that the generator can produce
+ */
+ static int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return (std::numeric_limits<int32_t>::max)(); }
+
+ /** Seeds the generator with the default seed. */
+ rand48() : lcf(cnv(static_cast<int32_t>(1))) {}
+ /**
+ * If T is an integral type smaller than int64_t, constructs
+ * a \rand48 generator with x(0) := (x0 << 16) | 0x330e. Otherwise
+ * constructs a \rand48 generator with x(0) = x0.
+ */
+ template<class T> explicit rand48(const T& x0) : lcf(cnv(x0)) { }
+ /**
+ * Seeds the generator with values produced by @c seq.generate().
+ */
+ template<class SeedSeq> explicit rand48(SeedSeq& seq) : lcf(cnv(seq)) { }
+ /**
+ * Seeds the generator using values from an iterator range,
+ * and updates first to point one past the last value consumed.
+ */
+ template<class It> rand48(It& first, It last) : lcf(first, last) { }
+
+ // compiler-generated copy ctor and assignment operator are fine
+
+ /** Seeds the generator with the default seed. */
+ void seed() { seed(static_cast<int32_t>(1)); }
+ /**
+ * If T is an integral type smaller than int64_t, changes
+ * the current value x(n) of the generator to (x0 << 16) | 0x330e.
+ * Otherwise changes the current value x(n) to x0.
+ */
+ template<class T> void seed(const T& x0) { lcf.seed(cnv(x0)); }
+ /**
+ * Seeds the generator using values from an iterator range,
+ * and updates first to point one past the last value consumed.
+ */
+ template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
+ /**
+ * Seeds the generator with values produced by @c seq.generate().
+ */
+ template<class SeedSeq> void seed(SeedSeq& seq) { lcf.seed(cnv(seq)); }
-#ifdef BOOST_RANDOM_DOXYGEN
- /**
- * If T is an integral type smaller than int46_t, changes
- * the current value x(n) of the generator to (x0 << 16) | 0x330e.
- * Otherwise changes the current value x(n) to x0.
- */
- template<class T> void seed(T x0 = 1);
-#else
- void seed() { seed(static_cast<int32_t>(1)); }
- template<class T> void seed(T x0) { lcf.seed(cnv(x0)); }
+ /** Returns the next value of the generator. */
+ int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
+
+#ifndef BOOST_NO_LONG_LONG
+ /** Advances the state of the generator by @c z. */
+ void discard(boost::ulong_long_type z) { lcf.discard(z); }
#endif
- template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
-
- /**
- * Returns the next value of the generator.
- */
- int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
- // by experiment from lrand48()
- static bool validation(int32_t x) { return x == 1993516219; }
-
-#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
+
+ /** Fills a range with random values */
+ 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); }
-#else
- // Use a member function; Streamable concept not supported.
- bool operator==(const rand48& rhs) const
- { return lcf == rhs.lcf; }
- bool operator!=(const rand48& rhs) const
- { return !(*this == rhs); }
-#endif
+ /** Writes a @c rand48 to a @c std::ostream. */
+ 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; }
+
+ /** Reads a @c rand48 from a @c std::istream. */
+ 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
+
+ /**
+ * Returns true if the two generators will produce identical
+ * sequences of values.
+ */
+ friend bool operator==(const rand48& x, const rand48& y)
+ { return x.lcf == y.lcf; }
+ /**
+ * Returns true if the two generators will produce different
+ * sequences of values.
+ */
+ friend bool operator!=(const rand48& x, const rand48& y)
+ { return !(x == y); }
private:
- /// \cond hide_private_members
- random::linear_congruential<uint64_t,
- uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
- 0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
- template<class T>
- static uint64_t cnv(T x)
- {
- if(sizeof(T) < sizeof(uint64_t)) {
- return (static_cast<uint64_t>(x) << 16) | 0x330e;
- } else {
- return(static_cast<uint64_t>(x));
- }
- }
- static uint64_t cnv(float x) { return(static_cast<uint64_t>(x)); }
- static uint64_t cnv(double x) { return(static_cast<uint64_t>(x)); }
- static uint64_t cnv(long double x) { return(static_cast<uint64_t>(x)); }
- /// \endcond
+ /// \cond
+ typedef random::linear_congruential_engine<uint64_t,
+ // xxxxULL is not portable
+ uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32),
+ 0xB, uint64_t(1)<<48> lcf_t;
+ lcf_t lcf;
+ template<class T>
+ static uint64_t cnv(T x,
+ typename boost::enable_if<boost::is_arithmetic<T> >::type* = 0)
+ {
+ if(sizeof(T) < sizeof(uint64_t)) {
+ return (static_cast<uint64_t>(x) << 16) | 0x330e;
+ } else {
+ return(static_cast<uint64_t>(x));
+ }
+ }
+ template<class SeedSeq>
+ static SeedSeq& cnv(SeedSeq& seq,
+ typename boost::disable_if<boost::is_arithmetic<SeedSeq> >::type* = 0)
+ {
+ return seq;
+ }
+ static lcf_t& cnv(rand48& x) { return x.lcf; }
+ static uint64_t cnv(float x) { return(static_cast<uint64_t>(x)); }
+ static uint64_t cnv(double x) { return(static_cast<uint64_t>(x)); }
+ static uint64_t cnv(long double x) { return(static_cast<uint64_t>(x)); }
+ /// \endcond
};
#endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
+} // namespace random
+
+using random::minstd_rand0;
+using random::minstd_rand;
+using random::rand48;
+
} // namespace boost
#include <boost/random/detail/enable_warnings.hpp>
Modified: trunk/boost/random/shuffle_output.hpp
==============================================================================
--- trunk/boost/random/shuffle_output.hpp (original)
+++ trunk/boost/random/shuffle_output.hpp 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -31,8 +31,8 @@
/**
* Instatiations of class template shuffle_output model a
* \pseudo_random_number_generator. It mixes the output
- * of some (usually \linear_congruential) \uniform_random_number_generator
- * to get better statistical properties.
+ * of some (usually \linear_congruential_engine)
+ * \uniform_random_number_generator to get better statistical properties.
* The algorithm is described in
*
* @blockquote
@@ -225,7 +225,7 @@
* @endblockquote
*/
typedef random::shuffle_output<
- random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
+ random::linear_congruential_engine<uint32_t, 1366, 150889, 714025>,
97, 139726> kreutzer1986;
Modified: trunk/libs/random/doc/Jamfile.v2
==============================================================================
--- trunk/libs/random/doc/Jamfile.v2 (original)
+++ trunk/libs/random/doc/Jamfile.v2 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -79,10 +79,10 @@
additive_combine=\"@xmlonly <classname alt=\\\"boost::random::additive_combine\\\">additive_combine</classname> @endxmlonly\" \\
discard_block=\"@xmlonly <classname alt=\\\"boost::random::discard_block\\\">discard_block</classname> @endxmlonly\" \\
lagged_fibonacci=\"@xmlonly<classname alt=\\\"boost::random::lagged_fibonacci\\\">lagged_fibonacci</classname>@endxmlonly\" \\
- linear_congruential=\"@xmlonly<classname alt=\\\"boost::random::linear_congruential\\\">linear_congruential</classname>@endxmlonly\" \\
- minstd_rand=\"@xmlonly <classname alt=\\\"boost::minstd_rand\\\">minstd_rand</classname> @endxmlonly\" \\
- minstd_rand0=\"@xmlonly <classname alt=\\\"boost::minstd_rand0\\\">minstd_rand0</classname> @endxmlonly\" \\
- rand48=\"@xmlonly <classname alt=\\\"boost::rand48\\\">rand48</classname> @endxmlonly\" \\
+ linear_congruential_engine=\"@xmlonly<classname alt=\\\"boost::random::linear_congruential_engine\\\">linear_congruential_engine</classname>@endxmlonly\" \\
+ minstd_rand=\"@xmlonly <classname alt=\\\"boost::random::minstd_rand\\\">minstd_rand</classname> @endxmlonly\" \\
+ minstd_rand0=\"@xmlonly <classname alt=\\\"boost::random::minstd_rand0\\\">minstd_rand0</classname> @endxmlonly\" \\
+ rand48=\"@xmlonly <classname alt=\\\"boost::random::rand48\\\">rand48</classname> @endxmlonly\" \\
mt11213b=\"@xmlonly <classname alt=\\\"boost::random::mt11213b\\\">mt11213b</classname> @endxmlonly\" \\
mt19937=\"@xmlonly <classname alt=\\\"boost::random::mt19937\\\">mt19937</classname> @endxmlonly\" \\
ecuyer1988=\"@xmlonly <classname alt=\\\"boost::ecuyer1988\\\">ecuyer1988</classname> @endxmlonly\" \\
@@ -106,8 +106,10 @@
\"BOOST_STATIC_CONSTANT(type,value)=static const type value\" \\
\"BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(Self,T,t)=explicit Self(T t)\" \\
\"BOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(Self,T,t)=template<class T> explicit Self(T& t)\" \\
+ \"BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(Self,T,t)=template<class T> explicit Self(T& t)\" \\
\"BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(Self,T,t)=void seed(T t)\" \\
- \"BOOST_RANDOM_DETAIL_GENERATOR_SEED(Self,T,t)=template<class T> void seed(T& t)\""
+ \"BOOST_RANDOM_DETAIL_GENERATOR_SEED(Self,T,t)=template<class T> void seed(T& t)\" \\
+ \"BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(Self,T,t)=template<class T> void seed(T& t)\""
<reftitle>"Headers"
<doxygen:xml-imagedir>images/random
;
Modified: trunk/libs/random/doc/concepts.qbk
==============================================================================
--- trunk/libs/random/doc/concepts.qbk (original)
+++ trunk/libs/random/doc/concepts.qbk 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -158,7 +158,8 @@
A pseudo-random number generator is a __UniformRandomNumberGenerator which
provides a deterministic sequence of pseudo-random numbers, based on some
-algorithm and internal state. [classref boost::random::linear_congruential
+algorithm and internal state.
+[classref boost::random::linear_congruential_engine
Linear congruential] and [classref boost::random::inversive_congruential
inversive congruential] generators are examples of such [prng pseudo-random
number generators]. Often, these generators are very sensitive to their
Modified: trunk/libs/random/doc/random.qbk
==============================================================================
--- trunk/libs/random/doc/random.qbk (original)
+++ trunk/libs/random/doc/random.qbk 2010-06-23 16:55:43 EDT (Wed, 23 Jun 2010)
@@ -30,9 +30,9 @@
[def __random_number_generator [classref boost::random_number_generator random_number_generator]]
[def __variate_generator [classref boost::variate_generator variate_generator]]
-[def __minstd_rand0 [classref boost::minstd_rand0 minstd_rand0]]
-[def __minstd_rand [classref boost::minstd_rand minstd_rand]]
-[def __rand48 [classref boost::rand48 rand48]]
+[def __minstd_rand0 [classref boost::random::minstd_rand0 minstd_rand0]]
+[def __minstd_rand [classref boost::random::minstd_rand minstd_rand]]
+[def __rand48 [classref boost::random::rand48 rand48]]
[def __ecuyer1988 [classref boost::ecuyer1988 ecuyer1988]]
[def __kreutzer1986 [classref boost::kreutzer1986 kreutzer1986]]
[def __taus88 [classref boost::taus88 taus88]]
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk