From f3d69ba680517be33ca5e68ccf922916ec1a1d03 Mon Sep 17 00:00:00 2001
From: Michal Kravcenko <michal.kravcenko@vsb.cz>
Date: Wed, 27 Feb 2019 13:54:06 +0100
Subject: [PATCH] MOD: placed the random generator to the body of the class for
 more robust rng

---
 src/DataSet/DataSet.cpp | 16 ++++++++++------
 src/DataSet/DataSet.h   |  5 +++++
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/DataSet/DataSet.cpp b/src/DataSet/DataSet.cpp
index 1501e1ac..820c1e61 100644
--- a/src/DataSet/DataSet.cpp
+++ b/src/DataSet/DataSet.cpp
@@ -17,10 +17,13 @@ namespace lib4neuro {
         this->n_elements = 0;
         this->input_dim = 0;
         this->output_dim = 0;
+        this->gen = boost::random::mt19937(std::time(0));
     }
 
     DataSet::DataSet(std::string file_path) {
         std::ifstream ifs(file_path);
+        this->gen = boost::random::mt19937(std::time(0));
+
         if(ifs.is_open()) {
             try {
                 boost::archive::text_iarchive ia(ifs);
@@ -42,6 +45,7 @@ namespace lib4neuro {
         this->data = *data_ptr;
         this->input_dim = this->data[0].first.size();
         this->output_dim = this->data[0].second.size();
+        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -62,6 +66,7 @@ namespace lib4neuro {
         this->n_elements = 0;
         this->input_dim = 1;
         this->output_dim = 1;
+        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -82,6 +87,7 @@ namespace lib4neuro {
         this->input_dim = bounds.size() / 2;
         this->output_dim = output_dim;
         this->n_elements = 0;
+        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -400,19 +406,17 @@ namespace lib4neuro {
      * Method returning random amount of data pairs between 1-max
      */
     std::vector<std::pair<std::vector<double>, std::vector<double>>> DataSet::get_random_data_batch(size_t max) {
-        if (max <= 0) {
+        if (max <= 0 || max >= this->data.size()) {
             return this->data;
         } else {
             std::vector<std::pair<std::vector<double>, std::vector<double>>> newData;
-            srand(time(NULL));  //TODO use Mersen twister from Boost
+            boost::random::uniform_int_distribution<> dist(0, this->data.size() - 1);
 
-            size_t n_chosen = rand() % std::min(max, this->data.size())+1;
-            n_chosen = max;
             std::vector<size_t> chosens;
             size_t chosen;
 
-            for (int i = 0; i < n_chosen; i++) {
-                chosen = rand() % this->data.size();
+            for (int i = 0; i < max; i++) {
+                chosen = dist(gen);
                 auto it = std::find(chosens.begin(), chosens.end(), chosen);
 
                 if (it != chosens.end()) {
diff --git a/src/DataSet/DataSet.h b/src/DataSet/DataSet.h
index 7793b748..fd20913a 100644
--- a/src/DataSet/DataSet.h
+++ b/src/DataSet/DataSet.h
@@ -12,6 +12,9 @@
 #include <string>
 #include <functional>
 #include <limits>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+#include <ctime>
 
 #include "../settings.h"
 #include "../NormalizationStrategy/NormalizationStrategy.h"
@@ -26,6 +29,8 @@ namespace lib4neuro {
 
     private:
 
+        boost::random::mt19937 gen;
+
         /**
          * Number of elements in the data set
          */
-- 
GitLab