diff --git a/src/DataSet/DataSet.cpp b/src/DataSet/DataSet.cpp index 1501e1ac44707e5f7a628607ce1b8bffd795b3be..820c1e61a230633edfe69cacefbb1a4888463671 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 7793b7481d0d2761cb572c0e367b383cfa1b5b00..fd20913a88cbd331a3d4ec37a39cf13d2cd182e1 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 */