From 5e3ec0e89155c2790c11bc06fb5e7e4b18cef87a Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Sun, 12 Jul 2015 10:28:28 +0000 Subject: [PATCH] Move pseudo RNG seeding to start of application Also switched to qrand() in place of rand() where possible as it is thread safe. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@5716 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- Modulator.cpp | 7 +++---- WSPRBandHopping.cpp | 7 ------- getfile.cpp | 4 ++-- main.cpp | 13 +++++++++++++ mainwindow.cpp | 2 +- psk_reporter.cpp | 1 - 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Modulator.cpp b/Modulator.cpp index e6bf3da1d..d8023547d 100644 --- a/Modulator.cpp +++ b/Modulator.cpp @@ -32,14 +32,13 @@ Modulator::Modulator (unsigned frameRate, unsigned periodLengthInSeconds, , m_phi {0.0} , m_toneSpacing {0.0} , m_fSpread {0.0} + , m_itone0 {0} , m_frameRate {frameRate} , m_period {periodLengthInSeconds} , m_state {Idle} , m_tuning {false} , m_cwLevel {false} { - qsrand (QDateTime::currentMSecsSinceEpoch()); // Initialize random seed - m_itone0=0; } void Modulator::start (unsigned symbolsLength, double framesPerSymbol, @@ -232,8 +231,8 @@ qint64 Modulator::readData (char * data, qint64 maxSize) int j=m_ic/480; if(m_fSpread>0.0 and j!=j0) { - float x1=(float)rand()/RAND_MAX; - float x2=(float)rand()/RAND_MAX; + float x1=(float)qrand()/RAND_MAX; + float x2=(float)qrand()/RAND_MAX; toneFrequency = toneFrequency0 + 0.5*m_fSpread*(x1+x2-1.0); m_dphi = m_twoPi * toneFrequency / m_frameRate; j0=j; diff --git a/WSPRBandHopping.cpp b/WSPRBandHopping.cpp index ce1e1d568..1638f5368 100644 --- a/WSPRBandHopping.cpp +++ b/WSPRBandHopping.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include "SettingsGroup.hpp" #include "Configuration.hpp" @@ -119,12 +118,6 @@ Dialog::Dialog (QSettings * settings, Configuration const * configuration, BandL main_layout->addLayout (bottom_layout); setLayout (main_layout); - - //init random seed for random number generator used in tx scheduler - struct timeval time; - gettimeofday(&time,NULL); - srand((time.tv_sec*1000)+(time.tv_usec/1000)); - } Dialog::~Dialog () diff --git a/getfile.cpp b/getfile.cpp index baa41801d..2dd55443d 100644 --- a/getfile.cpp +++ b/getfile.cpp @@ -145,8 +145,8 @@ float gran() * that are inside the unit circle */ do { - v1 = 2.0 * (float)rand() / RAND_MAX - 1; - v2 = 2.0 * (float)rand() / RAND_MAX - 1; + v1 = 2.0 * (float)qrand() / RAND_MAX - 1; + v2 = 2.0 * (float)qrand() / RAND_MAX - 1; rsq = v1*v1 + v2*v2; } while(rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0*log(rsq)/rsq); diff --git a/main.cpp b/main.cpp index a2bf312db..5ed5b2aa6 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -28,6 +29,18 @@ #include "TraceFile.hpp" #include "mainwindow.h" +namespace +{ + struct RNGSetup + { + RNGSetup () + { + // one time seed of pseudo RNGs from current time + auto seed = QDateTime::currentMSecsSinceEpoch (); + qsrand (seed); // this is good for rand() as well + } + } seeding; +} int main(int argc, char *argv[]) { diff --git a/mainwindow.cpp b/mainwindow.cpp index 88d5b720b..d275c8e0b 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4009,7 +4009,7 @@ void MainWindow::p1ReadFromStdout() //p1readFromStdout m_nWSPRdecodes=0; ui->DecodeButton->setChecked (false); if(m_uploadSpots) { - float x=rand()/((double)RAND_MAX + 1.0); + float x=qrand()/((double)RAND_MAX + 1.0); int msdelay=20000*x; uploadTimer->start(msdelay); //Upload delay } else { diff --git a/psk_reporter.cpp b/psk_reporter.cpp index dd2b424ea..82db043a6 100644 --- a/psk_reporter.cpp +++ b/psk_reporter.cpp @@ -38,7 +38,6 @@ PSK_Reporter::PSK_Reporter(MessageClient * message_client, QObject *parent) : "00960004"; // Report time - qsrand(QDateTime::currentDateTime().toTime_t()); m_randomId_h = QString("%1").arg(qrand(),8,16,QChar('0')); QHostInfo::lookupHost("report.pskreporter.info", this, SLOT(dnsLookupResult(QHostInfo)));