diff --git a/openbr/core/common.cpp b/openbr/core/common.cpp index 17584ec..55cbd3e 100644 --- a/openbr/core/common.cpp +++ b/openbr/core/common.cpp @@ -15,11 +15,15 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "common.h" +#include using namespace std; /**** GLOBAL ****/ void Common::seedRNG() { + static QMutex seedControl; + QMutexLocker lock(&seedControl); + static bool seeded = false; if (!seeded) { srand(0); // We seed with 0 instead of time(NULL) to have reproducible randomness @@ -29,8 +33,6 @@ void Common::seedRNG() { QList Common::RandSample(int n, int max, int min, bool unique) { - seedRNG(); - QList samples; samples.reserve(n); int range = max-min; if (range <= 0) qFatal("Non-positive range."); @@ -50,8 +52,6 @@ QList Common::RandSample(int n, int max, int min, bool unique) QList Common::RandSample(int n, const QSet &values, bool unique) { - seedRNG(); - QList valueList = values.toList(); if (unique && (values.size() <= n)) return valueList; diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index d681843..5f2a855 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -891,6 +891,8 @@ void br::Context::initialize(int &argc, char *argv[], QString sdkPath, bool use_ qInstallMessageHandler(messageHandler); + Common::seedRNG(); + // Search for SDK if (sdkPath.isEmpty()) { QStringList checkPaths; checkPaths << QDir::currentPath() << QCoreApplication::applicationDirPath(); diff --git a/openbr/plugins/independent.cpp b/openbr/plugins/independent.cpp index fd650dc..3886e8c 100644 --- a/openbr/plugins/independent.cpp +++ b/openbr/plugins/independent.cpp @@ -35,7 +35,6 @@ static TemplateList Downsample(const TemplateList &templates, int classes, int i if ((classes != std::numeric_limits::max()) && (uniqueLabels.size() < classes)) qWarning("Downsample requested %d classes but only %d are available.", classes, uniqueLabels.size()); - Common::seedRNG(); QList selectedLabels = uniqueLabels; if (classes < uniqueLabels.size()) { std::random_shuffle(selectedLabels.begin(), selectedLabels.end());