1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-14 04:00:34 +00:00

Avoid the use of rand()

This commit is contained in:
Carles Fernandez 2017-06-07 16:37:47 +02:00
parent 0b2b0fceac
commit e1c0227bb9
3 changed files with 23 additions and 9 deletions

View File

@ -32,14 +32,20 @@
#include <ctime>
#include <cmath>
#include <limits>
#include <random>
#include <volk_gnsssdr/volk_gnsssdr.h>
#include <volk_gnsssdr/volk_gnsssdr_cpu.h>
#include <volk_gnsssdr/volk_gnsssdr_common.h>
#include <volk_gnsssdr/volk_gnsssdr_malloc.h>
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<float>(uniform_dist(e1));
}
template <class t>
@ -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<float> 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<float>((RAND_MAX/2))) * int_max;
float scaled_rand = static_cast<float>(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)
{

View File

@ -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 <iostream>
#include <fstream>
#include <gnuradio/io_signature.h>
//#include <volk/volk.h>
#include <volk_gnsssdr/volk_gnsssdr.h>
#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<int> 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++)

View File

@ -33,6 +33,8 @@
#include <string>
#include <vector>
#include <cstdlib>
#include <random>
#include <boost/scoped_array.hpp>
#include <gnuradio/random.h>
#include <gnuradio/block.h>
@ -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<int> uniform_dist;
public:
~signal_generator_c(); // public destructor
@ -135,4 +140,3 @@ public:
};
#endif /* GNSS_SDR_SIGNAL_GENERATOR_C_H */