Skip to content
Snippets Groups Projects
Commit f3d69ba6 authored by Michal Kravcenko's avatar Michal Kravcenko
Browse files

MOD: placed the random generator to the body of the class for more robust rng

parent b62a844d
No related branches found
No related tags found
No related merge requests found
...@@ -17,10 +17,13 @@ namespace lib4neuro { ...@@ -17,10 +17,13 @@ namespace lib4neuro {
this->n_elements = 0; this->n_elements = 0;
this->input_dim = 0; this->input_dim = 0;
this->output_dim = 0; this->output_dim = 0;
this->gen = boost::random::mt19937(std::time(0));
} }
DataSet::DataSet(std::string file_path) { DataSet::DataSet(std::string file_path) {
std::ifstream ifs(file_path); std::ifstream ifs(file_path);
this->gen = boost::random::mt19937(std::time(0));
if(ifs.is_open()) { if(ifs.is_open()) {
try { try {
boost::archive::text_iarchive ia(ifs); boost::archive::text_iarchive ia(ifs);
...@@ -42,6 +45,7 @@ namespace lib4neuro { ...@@ -42,6 +45,7 @@ namespace lib4neuro {
this->data = *data_ptr; this->data = *data_ptr;
this->input_dim = this->data[0].first.size(); this->input_dim = this->data[0].first.size();
this->output_dim = this->data[0].second.size(); this->output_dim = this->data[0].second.size();
this->gen = boost::random::mt19937(std::time(0));
if(ns) { if(ns) {
this->normalization_strategy = ns; this->normalization_strategy = ns;
...@@ -62,6 +66,7 @@ namespace lib4neuro { ...@@ -62,6 +66,7 @@ namespace lib4neuro {
this->n_elements = 0; this->n_elements = 0;
this->input_dim = 1; this->input_dim = 1;
this->output_dim = 1; this->output_dim = 1;
this->gen = boost::random::mt19937(std::time(0));
if(ns) { if(ns) {
this->normalization_strategy = ns; this->normalization_strategy = ns;
...@@ -82,6 +87,7 @@ namespace lib4neuro { ...@@ -82,6 +87,7 @@ namespace lib4neuro {
this->input_dim = bounds.size() / 2; this->input_dim = bounds.size() / 2;
this->output_dim = output_dim; this->output_dim = output_dim;
this->n_elements = 0; this->n_elements = 0;
this->gen = boost::random::mt19937(std::time(0));
if(ns) { if(ns) {
this->normalization_strategy = ns; this->normalization_strategy = ns;
...@@ -400,19 +406,17 @@ namespace lib4neuro { ...@@ -400,19 +406,17 @@ namespace lib4neuro {
* Method returning random amount of data pairs between 1-max * 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) { 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; return this->data;
} else { } else {
std::vector<std::pair<std::vector<double>, std::vector<double>>> newData; 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; std::vector<size_t> chosens;
size_t chosen; size_t chosen;
for (int i = 0; i < n_chosen; i++) { for (int i = 0; i < max; i++) {
chosen = rand() % this->data.size(); chosen = dist(gen);
auto it = std::find(chosens.begin(), chosens.end(), chosen); auto it = std::find(chosens.begin(), chosens.end(), chosen);
if (it != chosens.end()) { if (it != chosens.end()) {
......
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include <limits> #include <limits>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <ctime>
#include "../settings.h" #include "../settings.h"
#include "../NormalizationStrategy/NormalizationStrategy.h" #include "../NormalizationStrategy/NormalizationStrategy.h"
...@@ -26,6 +29,8 @@ namespace lib4neuro { ...@@ -26,6 +29,8 @@ namespace lib4neuro {
private: private:
boost::random::mt19937 gen;
/** /**
* Number of elements in the data set * Number of elements in the data set
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment