diff options
-rw-r--r-- | gnuradio-runtime/include/gnuradio/random.h | 2 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/random.cc | 22 | ||||
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/qa_random.py | 29 | ||||
-rw-r--r-- | gr-analog/lib/random_uniform_source_X_impl.cc.t | 4 | ||||
-rw-r--r-- | 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 c5761d7359..2167247526 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 b35dfa106e..5e16c96ea4 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 6e321894b9..d3e5410454 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 a93f887c54..98af5d02fa 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 = (@TYPE@*)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 289ee3c634..67fcfc9893 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 */ |