From e1c0227bb9bdb1476520115cb3181ad84de049f2 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Wed, 7 Jun 2017 16:37:47 +0200 Subject: [PATCH] Avoid the use of rand() --- .../volk_gnsssdr/lib/qa_utils.cc | 15 ++++++++++++--- .../gnuradio_blocks/signal_generator_c.cc | 11 ++++++----- .../gnuradio_blocks/signal_generator_c.h | 6 +++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/qa_utils.cc b/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/qa_utils.cc index c5b3f5262..18176f0e2 100644 --- a/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/qa_utils.cc +++ b/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/qa_utils.cc @@ -32,14 +32,20 @@ #include #include #include +#include #include #include #include #include -float uniform() { - return 2.0f * ((float) rand() / RAND_MAX - 0.5f); // uniformly (-1, 1) +float uniform() +{ + // Seed with a real random value, if available + std::random_device r; + std::default_random_engine e1(r()); + std::uniform_real_distribution<> uniform_dist(-1, 1); + return static_cast(uniform_dist(e1)); } template @@ -51,6 +57,9 @@ void random_floats (t *buf, unsigned n) void load_random_data(void *data, volk_gnsssdr_type_t type, unsigned int n) { + std::random_device r; + std::default_random_engine e1(r()); + std::uniform_int_distribution uniform_dist(-1, 1); if(type.is_complex) n *= 2; if(type.is_float) { @@ -63,7 +72,7 @@ void load_random_data(void *data, volk_gnsssdr_type_t type, unsigned int n) if(type.is_signed) int_max /= 2.0; for(unsigned int i = 0; i < n; i++) { - float scaled_rand = (((float) (rand() - (RAND_MAX/2))) / static_cast((RAND_MAX/2))) * int_max; + float scaled_rand = static_cast(uniform_dist(e1)) * int_max; //man i really don't know how to do this in a more clever way, you have to cast down at some point switch(type.size) { diff --git a/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.cc b/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.cc index 74a1cf9f2..4ce5e8daf 100644 --- a/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.cc +++ b/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.cc @@ -15,7 +15,7 @@ * GNSS-SDR is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. +* (at your option) any later version. * * GNSS-SDR is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -33,7 +33,6 @@ #include #include #include -//#include #include #include "gps_sdr_signal_processing.h" #include "galileo_e1_signal_processing.h" @@ -131,6 +130,8 @@ void signal_generator_c::init() } } random_ = new gr::random(); + std::default_random_engine e1(r()); + std::uniform_int_distribution uniform_dist(0, RAND_MAX); } @@ -295,7 +296,7 @@ int signal_generator_c::general_work (int noutput_items __attribute__((unused)), if (ms_counter_[sat] == 0 && data_flag_) { // New random data bit - current_data_bits_[sat] = gr_complex((rand() % 2) == 0 ? 1 : -1, 0); + current_data_bits_[sat] = gr_complex((uniform_dist(e1) % 2) == 0 ? 1 : -1, 0); } for (k = delay_samples; k < samples_per_code_[sat]; k++) @@ -330,7 +331,7 @@ int signal_generator_c::general_work (int noutput_items __attribute__((unused)), if (ms_counter_[sat]%data_bit_duration_ms_[sat] == 0 && data_flag_) { // New random data bit - current_data_bit_int_[sat] = (rand()%2) == 0 ? 1 : -1; + current_data_bit_int_[sat] = (uniform_dist(e1)%2) == 0 ? 1 : -1; } data_modulation_[sat] = current_data_bit_int_[sat] * (Galileo_E5a_I_SECONDARY_CODE.at((ms_counter_[sat]+delay_sec_[sat]) % 20) == '0' ? 1 : -1); pilot_modulation_[sat] = (Galileo_E5a_Q_SECONDARY_CODE[PRN_[sat] - 1].at((ms_counter_[sat] + delay_sec_[sat]) % 100) == '0' ? 1 : -1); @@ -362,7 +363,7 @@ int signal_generator_c::general_work (int noutput_items __attribute__((unused)), if (ms_counter_[sat] == 0 && data_flag_) { // New random data bit - current_data_bits_[sat] = gr_complex((rand() % 2) == 0 ? 1 : -1, 0); + current_data_bits_[sat] = gr_complex((uniform_dist(e1) % 2) == 0 ? 1 : -1, 0); } for (k = delay_samples; k < samples_per_code_[sat]; k++) diff --git a/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.h b/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.h index e35dd0229..3700f8af9 100644 --- a/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.h +++ b/src/algorithms/signal_generator/gnuradio_blocks/signal_generator_c.h @@ -33,6 +33,8 @@ #include #include +#include +#include #include #include #include @@ -122,6 +124,9 @@ private: gr_complex* complex_phase_; unsigned int work_counter_; + std::random_device r; + std::default_random_engine e1; + std::uniform_int_distribution uniform_dist; public: ~signal_generator_c(); // public destructor @@ -135,4 +140,3 @@ public: }; #endif /* GNSS_SDR_SIGNAL_GENERATOR_C_H */ -