From 0f6f4c9db81ce100baa2502075dc611fa50aa116 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 3 Apr 2016 12:53:41 +0200 Subject: Implement deterministic justification i.e. the mersenne twister seed now depends on the data. --- src/line_accumulator.cc | 26 +++++++++++++++----------- src/line_accumulator.h | 4 +--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc index 0abe1a4..9922f04 100644 --- a/src/line_accumulator.cc +++ b/src/line_accumulator.cc @@ -6,23 +6,29 @@ namespace { +static std::mt19937 generator; + std::uint8_t getRandomIndex( - std::mt19937& random, + const int seed, const std::uint8_t n ) { - return std::uniform_int_distribution{0, n}(random); + generator.seed(seed); + + return std::uniform_int_distribution{0, n}(generator); } std::vector getRandomIndizes( - std::mt19937& random, + const int seed, const std::uint8_t n ) { + generator.seed(seed); + std::vector indizes(n); std::iota(indizes.begin(), indizes.end(), 0); std::shuffle( indizes.begin(), indizes.end(), - random + generator ); return indizes; @@ -34,8 +40,6 @@ namespace justify { LineAccumulator::LineAccumulator(const std::uint8_t max_length): max_length_{max_length}, - device_{}, - random_{device_()}, length_{0}, tokens_{} { } @@ -62,6 +66,9 @@ void LineAccumulator::operator()(const std::string& token) { } void LineAccumulator::justify() { + const int seed = this->tokens_.size() + + this->getMissing(); + switch ( this->tokens_.size() ) { // There is no sensible block justification of null or any single token case 0: @@ -105,16 +112,13 @@ void LineAccumulator::justify() { // randomly distribute missing spaces case 1: { this->tokens_[ - getRandomIndex(this->random_, this->tokens_.size() - 2) + getRandomIndex(seed, this->tokens_.size() - 2) ].second += 1; break; } default: { - const auto indizes = getRandomIndizes( - this->random_, - this->tokens_.size() - 2 - ); + const auto indizes = getRandomIndizes(seed, this->tokens_.size() - 2); std::for_each( indizes.begin(), diff --git a/src/line_accumulator.h b/src/line_accumulator.h index cb07210..2ca271c 100644 --- a/src/line_accumulator.h +++ b/src/line_accumulator.h @@ -19,9 +19,7 @@ class LineAccumulator { private: const std::uint8_t max_length_; - std::random_device device_; - std::mt19937 random_; - std::uint8_t length_; + std::uint8_t length_; std::vector< std::pair -- cgit v1.2.3