aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/line_accumulator.cc26
-rw-r--r--src/line_accumulator.h4
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<std::uint8_t>{0, n}(random);
+ generator.seed(seed);
+
+ return std::uniform_int_distribution<std::uint8_t>{0, n}(generator);
}
std::vector<std::uint8_t> getRandomIndizes(
- std::mt19937& random,
+ const int seed,
const std::uint8_t n
) {
+ generator.seed(seed);
+
std::vector<std::uint8_t> 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<std::string, std::uint8_t>