|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r69978 - in trunk: boost/random libs/random/test
From: steven_at_[hidden]
Date: 2011-03-14 16:38:14
Author: steven_watanabe
Date: 2011-03-14 16:38:12 EDT (Mon, 14 Mar 2011)
New Revision: 69978
URL: http://svn.boost.org/trac/boost/changeset/69978
Log:
Move random_number_generator into namespace random, tweak the implementation a bit, and add a test case.
Added:
trunk/libs/random/test/test_random_number_generator.cpp (contents, props changed)
Text files modified:
trunk/boost/random/random_number_generator.hpp | 62 +++++++++++++++++++--------------------
trunk/libs/random/test/Jamfile.v2 | 1
2 files changed, 31 insertions(+), 32 deletions(-)
Modified: trunk/boost/random/random_number_generator.hpp
==============================================================================
--- trunk/boost/random/random_number_generator.hpp (original)
+++ trunk/boost/random/random_number_generator.hpp 2011-03-14 16:38:12 EDT (Mon, 14 Mar 2011)
@@ -16,13 +16,11 @@
#ifndef BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP
#define BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP
-#include <boost/config.hpp>
-#include <boost/limits.hpp>
-#include <boost/static_assert.hpp>
-#include <boost/random/uniform_int.hpp>
-#include <boost/random/variate_generator.hpp>
+#include <boost/assert.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
namespace boost {
+namespace random {
/**
* Instantiations of class template random_number_generator model a
@@ -32,40 +30,40 @@
*
* The template parameter IntType shall denote some integer-like value type.
*/
-template<class UniformRandomNumberGenerator, class IntType = long>
+template<class URNG, class IntType = long>
class random_number_generator
{
public:
- typedef UniformRandomNumberGenerator base_type;
- typedef IntType argument_type;
- typedef IntType result_type;
- /**
- * Constructs a random_number_generator functor with the given
- * \uniform_random_number_generator as the underlying source of
- * random numbers.
- */
- random_number_generator(base_type& rng) : _rng(rng)
- {
-#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
- BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
-#endif
- }
- // compiler-generated copy ctor is fine
- // assignment is disallowed because there is a reference member
-
- /**
- * Returns a value in the range [0, n)
- */
- result_type operator()(argument_type n)
- {
- typedef uniform_int<IntType> dist_type;
- return variate_generator<base_type&, dist_type>(_rng, dist_type(0, n-1))();
- }
+ typedef URNG base_type;
+ typedef IntType argument_type;
+ typedef IntType result_type;
+ /**
+ * Constructs a random_number_generator functor with the given
+ * \uniform_random_number_generator as the underlying source of
+ * random numbers.
+ */
+ random_number_generator(base_type& rng) : _rng(rng) {}
+
+ // compiler-generated copy ctor is fine
+ // assignment is disallowed because there is a reference member
+
+ /**
+ * Returns a value in the range [0, n)
+ */
+ result_type operator()(argument_type n)
+ {
+ BOOST_ASSERT(n > 0);
+ return uniform_int_distribution<IntType>(0, n-1)(_rng);
+ }
private:
- base_type& _rng;
+ base_type& _rng;
};
+} // namespace random
+
+using random::random_number_generator;
+
} // namespace boost
#endif // BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP
Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2011-03-14 16:38:12 EDT (Mon, 14 Mar 2011)
@@ -13,6 +13,7 @@
run random_test.cpp ;
run test_const_mod.cpp /boost//unit_test_framework ;
run test_generate_canonical.cpp /boost//unit_test_framework ;
+run test_random_number_generator.cpp /boost//unit_test_framework ;
run ../example/random_demo.cpp ;
run test_random_device.cpp /boost//random : : : <link>static : test_random_device ;
run test_random_device.cpp /boost//random : : : <link>shared : test_random_device_dll ;
Added: trunk/libs/random/test/test_random_number_generator.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_random_number_generator.cpp 2011-03-14 16:38:12 EDT (Mon, 14 Mar 2011)
@@ -0,0 +1,33 @@
+/* boost test_random_number_generator.cpp
+ *
+ * Copyright Jens Maurer 2000
+ * Copyright Steven Watanabe 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)
+ *
+ * $Id$
+ */
+
+#include <boost/random/random_number_generator.hpp>
+#include <boost/random/mersenne_twister.hpp>
+
+#include <algorithm>
+#include <vector>
+
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE(test_random_shuffle)
+{
+ boost::mt19937 engine(1234);
+ boost::random::random_number_generator<boost::mt19937> generator(engine);
+
+ std::vector<int> testVec;
+
+ for (int i = 0; i < 200; ++i) {
+ testVec.push_back(i);
+ }
+
+ std::random_shuffle(testVec.begin(), testVec.end(), generator);
+}
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