Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r68814 - in trunk: boost boost/random libs/random/src libs/random/test
From: steven_at_[hidden]
Date: 2011-02-12 13:41:43


Author: steven_watanabe
Date: 2011-02-12 13:41:28 EST (Sat, 12 Feb 2011)
New Revision: 68814
URL: http://svn.boost.org/trac/boost/changeset/68814

Log:
Update random_device.
Added:
   trunk/boost/random/random_device.hpp
      - copied, changed from r68770, /trunk/boost/nondet_random.hpp
Text files modified:
   trunk/boost/nondet_random.hpp | 122 ---------------------------------------
   trunk/boost/random/random_device.hpp | 105 ++++++++++++++++-----------------
   trunk/libs/random/src/random_device.cpp | 39 +++++++-----
   trunk/libs/random/test/test_random_device.cpp | 5 +
   4 files changed, 78 insertions(+), 193 deletions(-)

Modified: trunk/boost/nondet_random.hpp
==============================================================================
--- trunk/boost/nondet_random.hpp (original)
+++ trunk/boost/nondet_random.hpp 2011-02-12 13:41:28 EST (Sat, 12 Feb 2011)
@@ -17,126 +17,6 @@
 #ifndef BOOST_NONDET_RANDOM_HPP
 #define BOOST_NONDET_RANDOM_HPP
 
-#include <string> // std::abs
-#include <algorithm> // std::min
-#include <boost/config/no_tr1/cmath.hpp>
-#include <boost/config.hpp>
-#include <boost/utility.hpp> // noncopyable
-#include <boost/integer_traits.hpp> // compile-time integral limits
-#include <boost/random/detail/auto_link.hpp>
-
-namespace boost {
-
-/**
- * Class \random_device models a \nondeterministic_random_number_generator.
- * It uses one or more implementation-defined stochastic processes to
- * generate a sequence of uniformly distributed non-deterministic random
- * numbers. For those environments where a non-deterministic random number
- * generator is not available, class random_device must not be implemented. See
- *
- * @blockquote
- * "Randomness Recommendations for Security", D. Eastlake, S. Crocker,
- * J. Schiller, Network Working Group, RFC 1750, December 1994
- * @endblockquote
- *
- * for further discussions.
- *
- * @xmlnote
- * Some operating systems abstract the computer hardware enough
- * to make it difficult to non-intrusively monitor stochastic processes.
- * However, several do provide a special device for exactly this purpose.
- * It seems to be impossible to emulate the functionality using Standard
- * C++ only, so users should be aware that this class may not be available
- * on all platforms.
- * @endxmlnote
- *
- * <b>Implementation Note for Linux</b>
- *
- * On the Linux operating system, token is interpreted as a filesystem
- * path. It is assumed that this path denotes an operating system
- * pseudo-device which generates a stream of non-deterministic random
- * numbers. The pseudo-device should never signal an error or end-of-file.
- * Otherwise, @c std::ios_base::failure is thrown. By default,
- * \random_device uses the /dev/urandom pseudo-device to retrieve
- * the random numbers. Another option would be to specify the /dev/random
- * pseudo-device, which blocks on reads if the entropy pool has no more
- * random bits available.
- *
- * <b>Implementation Note for Windows</b>
- *
- * On the Windows operating system, token is interpreted as the name
- * of a cryptographic service provider. By default \random_device uses
- * MS_DEF_PROV.
- *
- * <b>Performance</b>
- *
- * The test program <a href="\boost/libs/random/performance/nondet_random_speed.cpp">
- * nondet_random_speed.cpp</a> measures the execution times of the
- * nondet_random.hpp implementation of the above algorithms in a tight
- * loop. The performance has been evaluated on a Pentium Pro 200 MHz
- * with gcc 2.95.2, Linux 2.2.13, glibc 2.1.2.
- *
- * <table cols="2">
- * <tr><th>class</th><th>time per invocation [usec]</th></tr>
- * <tr><td> @xmlonly <classname alt="boost::random_device">random_device</classname> @endxmlonly </td><td>92.0</td></tr>
- * </table>
- *
- * The measurement error is estimated at +/- 1 usec.
- */
-class random_device : private noncopyable
-{
-public:
- typedef unsigned int result_type;
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = true);
- BOOST_STATIC_CONSTANT(result_type, min_value = integer_traits<result_type>::const_min);
- BOOST_STATIC_CONSTANT(result_type, max_value = integer_traits<result_type>::const_max);
-
- /**
- * Returns: The smallest value that the \random_device can produce.
- */
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return min_value; }
- /**
- * Returns: The largest value that the \random_device can produce.
- */
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return max_value; }
- /**
- * Constructs a @c random_device, optionally using the given token as an
- * access specification (for example, a URL) to some implementation-defined
- * service for monitoring a stochastic process.
- */
- BOOST_RANDOM_DECL explicit random_device(const std::string& token = default_token);
- BOOST_RANDOM_DECL ~random_device();
- /**
- * Returns: An entropy estimate for the random numbers returned by
- * operator(), in the range min() to log2( max()+1). A deterministic
- * random number generator (e.g. a pseudo-random number engine)
- * has entropy 0.
- *
- * Throws: Nothing.
- */
- BOOST_RANDOM_DECL double entropy() const;
- /**
- * Returns: A random value in the range [min, max]
- */
- BOOST_RANDOM_DECL unsigned int operator()();
-
-private:
- BOOST_RANDOM_DECL static const char * const default_token;
-
- /*
- * std:5.3.5/5 [expr.delete]: "If the object being deleted has incomplete
- * class type at the point of deletion and the complete class has a
- * non-trivial destructor [...], the behavior is undefined".
- * This disallows the use of scoped_ptr<> with pimpl-like classes
- * having a non-trivial destructor.
- */
- class impl;
- impl * pimpl;
-};
-
-
-// TODO: put Schneier's Yarrow-160 algorithm here.
-
-} // namespace boost
+#include <boost/random/random_device.hpp>
 
 #endif /* BOOST_NONDET_RANDOM_HPP */

Copied: trunk/boost/random/random_device.hpp (from r68770, /trunk/boost/nondet_random.hpp)
==============================================================================
--- /trunk/boost/nondet_random.hpp (original)
+++ trunk/boost/random/random_device.hpp 2011-02-12 13:41:28 EST (Sat, 12 Feb 2011)
@@ -1,6 +1,7 @@
-/* boost nondet_random.hpp header file
+/* boost random/random_device.hpp header file
  *
  * Copyright Jens Maurer 2000
+ * Copyright Steven Watanabe 2010-2011
  * Distributed under the Boost Software License, Version 1.0. (See
  * accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -14,18 +15,16 @@
 // See http://www.boost.org/libs/random for documentation.
 
 
-#ifndef BOOST_NONDET_RANDOM_HPP
-#define BOOST_NONDET_RANDOM_HPP
+#ifndef BOOST_RANDOM_RANDOM_DEVICE_HPP
+#define BOOST_RANDOM_RANDOM_DEVICE_HPP
 
-#include <string> // std::abs
-#include <algorithm> // std::min
-#include <boost/config/no_tr1/cmath.hpp>
+#include <string>
 #include <boost/config.hpp>
-#include <boost/utility.hpp> // noncopyable
-#include <boost/integer_traits.hpp> // compile-time integral limits
+#include <boost/noncopyable.hpp>
 #include <boost/random/detail/auto_link.hpp>
 
 namespace boost {
+namespace random {
 
 /**
  * Class \random_device models a \nondeterministic_random_number_generator.
@@ -86,57 +85,55 @@
 class random_device : private noncopyable
 {
 public:
- typedef unsigned int result_type;
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = true);
- BOOST_STATIC_CONSTANT(result_type, min_value = integer_traits<result_type>::const_min);
- BOOST_STATIC_CONSTANT(result_type, max_value = integer_traits<result_type>::const_max);
-
- /**
- * Returns: The smallest value that the \random_device can produce.
- */
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return min_value; }
- /**
- * Returns: The largest value that the \random_device can produce.
- */
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return max_value; }
- /**
- * Constructs a @c random_device, optionally using the given token as an
- * access specification (for example, a URL) to some implementation-defined
- * service for monitoring a stochastic process.
- */
- BOOST_RANDOM_DECL explicit random_device(const std::string& token = default_token);
- BOOST_RANDOM_DECL ~random_device();
- /**
- * Returns: An entropy estimate for the random numbers returned by
- * operator(), in the range min() to log2( max()+1). A deterministic
- * random number generator (e.g. a pseudo-random number engine)
- * has entropy 0.
- *
- * Throws: Nothing.
- */
- BOOST_RANDOM_DECL double entropy() const;
- /**
- * Returns: A random value in the range [min, max]
- */
- BOOST_RANDOM_DECL unsigned int operator()();
+ typedef unsigned int result_type;
+ BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
 
-private:
- BOOST_RANDOM_DECL static const char * const default_token;
+ /** Returns the smallest value that the \random_device can produce. */
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
+ /** Returns the largest value that the \random_device can produce. */
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return ~0u; }
+
+ /** Constructs a @c random_device, optionally using the default device. */
+ BOOST_RANDOM_DECL random_device();
+ /**
+ * Constructs a @c random_device, optionally using the given token as an
+ * access specification (for example, a URL) to some implementation-defined
+ * service for monitoring a stochastic process.
+ */
+ BOOST_RANDOM_DECL explicit random_device(const std::string& token);
+
+ BOOST_RANDOM_DECL ~random_device();
+
+ /**
+ * Returns: An entropy estimate for the random numbers returned by
+ * operator(), in the range min() to log2( max()+1). A deterministic
+ * random number generator (e.g. a pseudo-random number engine)
+ * has entropy 0.
+ *
+ * Throws: Nothing.
+ */
+ BOOST_RANDOM_DECL double entropy() const;
+ /** Returns a random value in the range [min, max]. */
+ BOOST_RANDOM_DECL unsigned int operator()();
+
+ /** Fills a range with random 32-bit values. */
+ template<class Iter>
+ void generate(Iter begin, Iter end)
+ {
+ for(; begin != end; ++begin) {
+ *begin = (*this)();
+ }
+ }
 
- /*
- * std:5.3.5/5 [expr.delete]: "If the object being deleted has incomplete
- * class type at the point of deletion and the complete class has a
- * non-trivial destructor [...], the behavior is undefined".
- * This disallows the use of scoped_ptr<> with pimpl-like classes
- * having a non-trivial destructor.
- */
- class impl;
- impl * pimpl;
+private:
+ class impl;
+ impl * pimpl;
 };
 
+} // namespace random
 
-// TODO: put Schneier's Yarrow-160 algorithm here.
+using random::random_device;
 
 } // namespace boost
 
-#endif /* BOOST_NONDET_RANDOM_HPP */
+#endif /* BOOST_RANDOM_RANDOM_DEVICE_HPP */

Modified: trunk/libs/random/src/random_device.cpp
==============================================================================
--- trunk/libs/random/src/random_device.cpp (original)
+++ trunk/libs/random/src/random_device.cpp 2011-02-12 13:41:28 EST (Sat, 12 Feb 2011)
@@ -1,7 +1,7 @@
 /* boost random_device.cpp implementation
  *
  * Copyright Jens Maurer 2000
- * Copyright Steven Watanabe 2010
+ * Copyright Steven Watanabe 2010-2011
  * Distributed under the Boost Software License, Version 1.0. (See
  * accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -12,16 +12,15 @@
 
 #define BOOST_RANDOM_SOURCE
 
-#include <boost/nondet_random.hpp>
+#include <boost/random/random_device.hpp>
+#include <boost/config.hpp>
+#include <boost/detail/workaround.hpp>
 #include <string>
 #include <cassert>
 
-
-#if defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) && !BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
+#if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) && !BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
 // A definition is required even for integral static constants
-const bool boost::random_device::has_fixed_range;
-const boost::random_device::result_type boost::random_device::min_value;
-const boost::random_device::result_type boost::random_device::max_value;
+const bool boost::random::random_device::has_fixed_range;
 #endif
 
 
@@ -56,9 +55,13 @@
 
 #endif
 
-BOOST_RANDOM_DECL const char * const boost::random_device::default_token = MS_DEF_PROV_A;
+namespace {
+
+const char * const default_token = MS_DEF_PROV_A;
+
+}
 
-class boost::random_device::impl
+class boost::random::random_device::impl
 {
 public:
   impl(const std::string & token) : provider(token) {
@@ -120,9 +123,11 @@
 
 #else
 
+namespace {
 // the default is the unlimited capacity device, using some secure hash
 // try "/dev/random" for blocking when the entropy pool has drained
-const char * const boost::random_device::default_token = "/dev/urandom";
+const char * const default_token = "/dev/urandom";
+}
 
 /*
  * This uses the POSIX interface for unbuffered reading.
@@ -153,7 +158,7 @@
 #include <stdexcept> // std::invalid_argument
 
 
-class boost::random_device::impl
+class boost::random::random_device::impl
 {
 public:
   impl(const std::string & token) : path(token) {
@@ -188,16 +193,16 @@
 
 #endif // BOOST_WINDOWS
 
-BOOST_RANDOM_DECL boost::random_device::random_device(const std::string& token)
+BOOST_RANDOM_DECL boost::random::random_device::random_device()
+ : pimpl(new impl(default_token))
+{}
+
+BOOST_RANDOM_DECL boost::random::random_device::random_device(const std::string& token)
   : pimpl(new impl(token))
-{
- assert((std::numeric_limits<result_type>::max)() == max_value);
-}
+{}
 
 BOOST_RANDOM_DECL boost::random_device::~random_device()
 {
- // the complete class impl is now visible, so we're safe
- // (see comment in random.hpp)
   delete pimpl;
 }
 

Modified: trunk/libs/random/test/test_random_device.cpp
==============================================================================
--- trunk/libs/random/test/test_random_device.cpp (original)
+++ trunk/libs/random/test/test_random_device.cpp 2011-02-12 13:41:28 EST (Sat, 12 Feb 2011)
@@ -8,7 +8,7 @@
  * $Id$
  */
 
-#include <boost/nondet_random.hpp>
+#include <boost/random/random_device.hpp>
 
 #include <boost/test/test_tools.hpp>
 #include <boost/test/included/test_exec_monitor.hpp>
@@ -22,5 +22,8 @@
                 BOOST_CHECK_GE(val, (rng.min)());
                 BOOST_CHECK_LE(val, (rng.max)());
         }
+
+ boost::uint32_t a[10];
+ rng.generate(a, a + 10);
         return 0;
 }


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