diff options
author | Thomas Habets <thomas@habets.se> | 2020-04-10 11:40:13 +0100 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2020-04-10 16:27:57 +0200 |
commit | 78955a24c6c24f92f7b22ab9223e8e0b60b1dc4b (patch) | |
tree | 09984db7dd2309ebfdb567f3fc3d8c72f42936a0 | |
parent | 55b6b2724ae872b36dd93c1479a829bb4054bdda (diff) |
runtime: Use nanosecond resolution for default random seed
This exposed some missing forwarding of RNG seed from one object to
another, also fixed in this commit.
-rw-r--r-- | gnuradio-runtime/include/gnuradio/random.h | 3 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/random.cc | 5 | ||||
-rw-r--r-- | gr-analog/lib/fastnoise_source_impl.cc | 6 |
3 files changed, 9 insertions, 5 deletions
diff --git a/gnuradio-runtime/include/gnuradio/random.h b/gnuradio-runtime/include/gnuradio/random.h index 70156f5e3d..30849b0032 100644 --- a/gnuradio-runtime/include/gnuradio/random.h +++ b/gnuradio-runtime/include/gnuradio/random.h @@ -42,8 +42,7 @@ public: /*! * \brief Change the seed for the initialized number generator. seed = 0 initializes - * the random number generator with the system time. Note that a fast initialization - * of various instances can result in the same seed. + * the random number generator with the system time. */ void reseed(unsigned int seed); diff --git a/gnuradio-runtime/lib/math/random.cc b/gnuradio-runtime/lib/math/random.cc index a25cc1c72e..8b67532207 100644 --- a/gnuradio-runtime/lib/math/random.cc +++ b/gnuradio-runtime/lib/math/random.cc @@ -30,6 +30,7 @@ #include <gnuradio/math.h> #include <gnuradio/random.h> +#include <chrono> #include <cmath> namespace gr { @@ -54,7 +55,9 @@ void random::reseed(unsigned int seed) { d_seed = seed; if (d_seed == 0) { - d_rng.seed(time(nullptr)); + auto now = std::chrono::system_clock::now().time_since_epoch(); + auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count(); + d_rng.seed(ns); } else { d_rng.seed(d_seed); } diff --git a/gr-analog/lib/fastnoise_source_impl.cc b/gr-analog/lib/fastnoise_source_impl.cc index b0ad85d769..a8d6d91650 100644 --- a/gr-analog/lib/fastnoise_source_impl.cc +++ b/gr-analog/lib/fastnoise_source_impl.cc @@ -59,7 +59,8 @@ fastnoise_source_impl<T>::fastnoise_source_impl(noise_type_t type, io_signature::make(0, 0, 0), io_signature::make(1, 1, sizeof(T))), d_type(type), - d_ampl(ampl) + d_ampl(ampl), + d_rng(seed) { d_samples.resize(samples); xoroshiro128p_seed(d_state, (uint64_t)seed); @@ -76,7 +77,8 @@ fastnoise_source_impl<gr_complex>::fastnoise_source_impl(noise_type_t type, io_signature::make(0, 0, 0), io_signature::make(1, 1, sizeof(gr_complex))), d_type(type), - d_ampl(ampl / sqrtf(2.0f)) + d_ampl(ampl / sqrtf(2.0f)), + d_rng(seed) { d_samples.resize(samples); xoroshiro128p_seed(d_state, (uint64_t)seed); |