[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] [gnuradio] 02/07: random-analog: fixed reseed issue
From: |
git |
Subject: |
[Commit-gnuradio] [gnuradio] 02/07: random-analog: fixed reseed issue |
Date: |
Wed, 7 Oct 2015 20:51:23 +0000 (UTC) |
This is an automated email from the git hooks/post-receive script.
jcorgan pushed a commit to branch master
in repository gnuradio.
commit a09f44a82ec9c8f8932f815f9b5f90127d1edbe2
Author: Johannes Demel <address@hidden>
Date: Mon Sep 28 11:00:27 2015 +0200
random-analog: fixed reseed issue
---
gnuradio-runtime/include/gnuradio/random.h | 2 +-
gnuradio-runtime/lib/math/random.cc | 22 ++++++++++++------
gnuradio-runtime/python/gnuradio/gr/qa_random.py | 29 ++++++++++++------------
gr-analog/lib/random_uniform_source_X_impl.cc.t | 4 ++--
gr-analog/lib/random_uniform_source_X_impl.h.t | 2 +-
5 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/gnuradio-runtime/include/gnuradio/random.h
b/gnuradio-runtime/include/gnuradio/random.h
index c5761d7..2167247 100644
--- a/gnuradio-runtime/include/gnuradio/random.h
+++ b/gnuradio-runtime/include/gnuradio/random.h
@@ -66,7 +66,7 @@ namespace gr {
void set_integer_limits(const int minimum, const int maximum);
/*!
- * Uniform random integers in the range set by 'set_integer_limits'.
+ * Uniform random integers in the range set by 'set_integer_limits' [min,
max).
*/
int ran_int();
diff --git a/gnuradio-runtime/lib/math/random.cc
b/gnuradio-runtime/lib/math/random.cc
index b35dfa1..5e16c96 100644
--- a/gnuradio-runtime/lib/math/random.cc
+++ b/gnuradio-runtime/lib/math/random.cc
@@ -48,13 +48,14 @@ namespace gr {
{
d_gauss_stored = false; // set gasdev (gauss distributed numbers) on
calculation state
- // Setup random number generator
- d_rng = new boost::mt19937;
+ // Setup random number generators
+ d_rng = new boost::mt19937; // random numbers are generated here.
+ d_uniform = new boost::uniform_real<float>; // map random number to
distribution
+ d_integer_dis = new boost::random::uniform_int_distribution<>(0, 1); //
another "mapper"
+ d_generator = NULL; // MUST be reinstantiated on every call to reseed.
+ d_integer_generator = NULL; // MUST be reinstantiated on everytime d_rng
or d_integer_dis is changed.
reseed(seed); // set seed for random number generator
- d_uniform = new boost::uniform_real<float>;
- d_integer_dis = new boost::random::uniform_int_distribution<>(min_integer,
max_integer - 1);
- d_generator = new boost::variate_generator<boost::mt19937&,
boost::uniform_real<float> > (*d_rng,*d_uniform); // create number generator in
[0,1) from boost.random
- d_integer_generator = new boost::variate_generator<boost::mt19937&,
boost::random::uniform_int_distribution<> >(*d_rng, *d_integer_dis);
+ set_integer_limits(min_integer, max_integer);
}
random::~random()
@@ -72,10 +73,14 @@ namespace gr {
void
random::reseed(unsigned int seed)
{
- //FIXME: method without effect after c'tor.
if(seed==0) d_seed = static_cast<unsigned int>(std::time(0));
else d_seed = seed;
d_rng->seed(d_seed);
+ // reinstantiate generators. Otherwise reseed doesn't take effect.
+ delete d_generator;
+ d_generator = new boost::variate_generator<boost::mt19937&,
boost::uniform_real<float> > (*d_rng,*d_uniform); // create number generator in
[0,1) from boost.random
+ delete d_integer_generator;
+ d_integer_generator = new boost::variate_generator<boost::mt19937&,
boost::random::uniform_int_distribution<> >(*d_rng, *d_integer_dis);
}
void
@@ -87,6 +92,9 @@ namespace gr {
d_integer_generator = new boost::variate_generator<boost::mt19937&,
boost::random::uniform_int_distribution<> >(*d_rng, *d_integer_dis);
}
+ /*!
+ * Uniform random integers in the range set by 'set_integer_limits' [min,
max).
+ */
int
random::ran_int(){
return (*d_integer_generator)();
diff --git a/gnuradio-runtime/python/gnuradio/gr/qa_random.py
b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
index 6e32189..d3e5410 100644
--- a/gnuradio-runtime/python/gnuradio/gr/qa_random.py
+++ b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
@@ -38,39 +38,38 @@ class test_random(gr_unittest.TestCase):
self.assertLess(value, 1)
self.assertGreaterEqual(value, 0)
- # Check reseed method (init with time and seed as fix number)
- def test_2(self):
+ # Same seed should yield same random values.
+ def test_2_same_seed(self):
num = 5
-
- rndm0 = gr.random(42) # init with time
- rndm1 = gr.random(42) # init with fix seed
+ # Init with fixed seed.
+ rndm0 = gr.random(42)
+ rndm1 = gr.random(42)
for k in range(num):
x = rndm0.ran1()
y = rndm1.ran1()
self.assertEqual(x, y)
+ # reseed should yield same numbers.
+ def test_003_reseed(self):
+ num = 5
x = np.zeros(num)
y = np.zeros(num)
- rndm0 = gr.random(42) # init with fix seed 1
- for k in range(num):
- x[k] = rndm0.ran1()
- rndm1.reseed(43) # init with fix seed 2
+ rndm = gr.random(43) # init with fix seed 1
for k in range(num):
- y[k] = rndm0.ran1()
+ x[k] = rndm.ran1()
+ rndm.reseed(43) # init with fix seed 2
for k in range(num):
- self.assertNotEqual(x[k], y[k])
+ y[k] = rndm.ran1()
+ self.assertFloatTuplesAlmostEqual(x, y)
- def test_003_integer(self):
+ def test_004_integer(self):
nitems = 100000
minimum = 2
maximum = 42
-
rng = gr.random(1, minimum, maximum)
-
rnd_vals = np.zeros(nitems, dtype=int)
for i in range(nitems):
rnd_vals[i] = rng.ran_int()
-
self.assertGreaterEqual(minimum, np.min(rnd_vals))
self.assertLess(np.max(rnd_vals), maximum)
diff --git a/gr-analog/lib/random_uniform_source_X_impl.cc.t
b/gr-analog/lib/random_uniform_source_X_impl.cc.t
index a93f887..98af5d0 100644
--- a/gr-analog/lib/random_uniform_source_X_impl.cc.t
+++ b/gr-analog/lib/random_uniform_source_X_impl.cc.t
@@ -52,7 +52,7 @@ namespace gr {
}
int
- @IMPL_NAME@::random_int()
+ @IMPL_NAME@::random_value()
{
return d_rng->ran_int();
}
@@ -65,7 +65,7 @@ namespace gr {
@TYPE@ *out = (@address@hidden)output_items[0];
for(int i = 0; i < noutput_items; i++){
- *out++ = (@TYPE@) random_int();
+ *out++ = (@TYPE@) random_value();
}
// Tell runtime system how many output items we produced.
diff --git a/gr-analog/lib/random_uniform_source_X_impl.h.t
b/gr-analog/lib/random_uniform_source_X_impl.h.t
index 289ee3c..67fcfc9 100644
--- a/gr-analog/lib/random_uniform_source_X_impl.h.t
+++ b/gr-analog/lib/random_uniform_source_X_impl.h.t
@@ -45,7 +45,7 @@ namespace gr {
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
- int random_int();
+ int random_value();
};
} /* namespace filter */
- [Commit-gnuradio] [gnuradio] branch master updated (8a4904d -> b1883cb), git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 04/07: Merge remote-tracking branch 'jdemel/random-int', git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 02/07: random-analog: fixed reseed issue,
git <=
- [Commit-gnuradio] [gnuradio] 05/07: analog: fix failing QA test from incorrect import, git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 03/07: fec: add getter to 'ber_bf' total_errors member, change counter types to long, git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 07/07: Merge remote-tracking branch 'jdemel/fec-ber-error-getter', git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 01/07: random-analog: added new uniform integer distribution source, git, 2015/10/08
- [Commit-gnuradio] [gnuradio] 06/07: Merge branch 'maint', git, 2015/10/08