From 5a8a7bea65fbc6e636f3608b26ec1d7639627718 Mon Sep 17 00:00:00 2001 From: sale Date: Tue, 22 Nov 2016 16:32:30 +0000 Subject: [PATCH] Added basic random utility --- include/utils/random/generator.h | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 include/utils/random/generator.h diff --git a/include/utils/random/generator.h b/include/utils/random/generator.h new file mode 100644 index 000000000..bc2140068 --- /dev/null +++ b/include/utils/random/generator.h @@ -0,0 +1,76 @@ +#include +#include + +// namespace ::utils +namespace utils { + +// namespace utils::random +namespace random { + +template +class RandomGenerator { + private: + std::random_device device_; + + protected: + Generator gen_; + Distribution dist_; + + public: + RandomGenerator(Distribution dist) : gen_(device_()), dist_(dist) {} + +}; + +class StringGenerator + : public RandomGenerator, + std::default_random_engine> { + public: + StringGenerator() + : RandomGenerator(std::uniform_int_distribution(32, 126)) {} + + std::string next(int size) { + std::string random_string; + + for (int i = 0; i < size; i++) random_string += (dist_(gen_) + '\0'); + return random_string; + } + +}; + +template +class IntegerGenerator : public RandomGenerator { + + public: + IntegerGenerator(DistributionRangeType start, DistributionRangeType end) : + RandomGenerator(Distribution(start, end)){} + + int next() { + return this->dist_(this->gen_); + + } + +}; + +template +class DecimalGenerator : public RandomGenerator { + + public: + DecimalGenerator(DistributionRangeType start, DistributionRangeType end) : + RandomGenerator(Distribution(start, end)) {} + + auto next() { + return this->dist_(this->gen_); + } + +}; + +template +auto generate_vector(RandomGenerator& gen, int size) { + std::vector elements(size); + for (int i = 0; i < size; i++) + elements[i] = gen.next(); + return elements; +} + +}; // namespace utils::random +}; // namespace ::utils