From 32b13de33f0cc1006837b720d09a3e243248e8bf Mon Sep 17 00:00:00 2001 From: averrin Date: Wed, 20 Sep 2017 22:14:11 +0300 Subject: [PATCH] fix random --- include/mapgen/MapGenerator.hpp | 5 ++--- src/MapGenerator.cpp | 13 ++----------- src/names.cpp | 14 +++++++------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/include/mapgen/MapGenerator.hpp b/include/mapgen/MapGenerator.hpp index cfa4370..1678adb 100644 --- a/include/mapgen/MapGenerator.hpp +++ b/include/mapgen/MapGenerator.hpp @@ -7,6 +7,7 @@ #include "noise/noiseutils.h" #include #include +#include //TODO: fix it and use header #include "../../src/Map.cpp" @@ -102,10 +103,8 @@ class MapGenerator { template Iter select_randomly(Iter start, Iter end); - template - Iter select_randomly_seed(Iter start, Iter end, int s); - void genRandomSites(std::vector >& sites, sf::Rect& bbox, unsigned int dx, unsigned int dy, unsigned int numSites); + std::mt19937 _gen; }; #endif diff --git a/src/MapGenerator.cpp b/src/MapGenerator.cpp index b7a4c14..de0b249 100644 --- a/src/MapGenerator.cpp +++ b/src/MapGenerator.cpp @@ -43,15 +43,6 @@ bool clusterOrdered(Cluster *s1, Cluster *s2) { template Iter MapGenerator::select_randomly(Iter start, Iter end) { - std::mt19937 gen(_seed); - std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1); - std::advance(start, dis(gen)); - return start; -} - -template -Iter MapGenerator::select_randomly_seed(Iter start, Iter end, int s) { - std::mt19937 gen(s); std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1); std::advance(start, dis(gen)); return start; @@ -68,6 +59,7 @@ MapGenerator::MapGenerator(int w, int h) : _w(w), _h(h) { currentOperation = ""; temperature = DEFAULT_TEMPERATURE; map = nullptr; + _gen = std::mt19937(_seed); } double getDistance(Point p, Point p2) { @@ -277,8 +269,7 @@ void MapGenerator::makeCaves() { int n = c->regions.size() / 50 + 1; while (n != 0) { - Region *r = *select_randomly_seed(c->regions.begin(), c->regions.end(), - std::clock()); + Region *r = *select_randomly(c->regions.begin(), c->regions.end()); if (r->location != nullptr) { continue; } diff --git a/src/names.cpp b/src/names.cpp index d175ddf..2071178 100644 --- a/src/names.cpp +++ b/src/names.cpp @@ -28,30 +28,30 @@ std::vector city_first_names (_city_first_names, _city_first_names std::vector city_second_names (_city_second_names, _city_second_names + sizeof(_city_second_names) / sizeof(_city_second_names[0]) ); +std::mt19937 gen(std::clock()); template -Iter select_randomly(Iter start, Iter end, int s) { - std::mt19937 gen(s); +Iter select_randomly(Iter start, Iter end) { std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1); std::advance(start, dis(gen)); return start; } std::string generateRiverName() { - std::string fn = *select_randomly(river_first_names.begin(), river_first_names.end(), std::clock()); - std::string sn = *select_randomly(river_second_names.begin(), river_second_names.end(), std::clock()); + std::string fn = *select_randomly(river_first_names.begin(), river_first_names.end()); + std::string sn = *select_randomly(river_second_names.begin(), river_second_names.end()); return fn+" "+sn; } std::string generateLandName() { - return *select_randomly(island_first_names.begin(), island_first_names.end(), std::clock()) + " " + *select_randomly(island_second_names.begin(), island_second_names.end(), std::clock()); + return *select_randomly(island_first_names.begin(), island_first_names.end()) + " " + *select_randomly(island_second_names.begin(), island_second_names.end()); } std::string generateSeaName() { - return *select_randomly(sea_first_names.begin(), sea_first_names.end(), std::clock()) + " " + *select_randomly(sea_second_names.begin(), sea_second_names.end(), std::clock()); + return *select_randomly(sea_first_names.begin(), sea_first_names.end()) + " " + *select_randomly(sea_second_names.begin(), sea_second_names.end()); } std::string generateCityName() { - auto name = *select_randomly(city_first_names.begin(), city_first_names.end(), std::clock()) + *select_randomly(city_second_names.begin(), city_second_names.end(), std::clock()); + auto name = *select_randomly(city_first_names.begin(), city_first_names.end()) + *select_randomly(city_second_names.begin(), city_second_names.end()); name[0] = toupper(name[0]); return name; }