tudocomp
– The TU Dortmund Compression Framework
RandomUniformGenerator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <random>
5 #include <tudocomp/Generator.hpp>
6 
7 namespace tdc {
8 
18 
19 public:
20  inline static Meta meta() {
21  Meta m("generator", "random", "Generates random strings.");
22  m.option("length").dynamic();
23  m.option("seed").dynamic(0);
24  m.option("min").dynamic('0');
25  m.option("max").dynamic('9');
26  return m;
27  }
28 
29  inline static std::string generate(
30  size_t length, size_t seed = 0, size_t min = '0', size_t max = '9') {
31 
32  if(min > max) std::swap(min, max);
33  if(!seed) seed = std::chrono::system_clock::now().time_since_epoch().count();
34 
35  std::string s(length,0);
36  std::default_random_engine engine(seed);
37  std::uniform_int_distribution<char> dist(min, max);
38 
39  for(size_t i = 0; i < length; ++i) {
40  s[i] = dist(engine);
41  }
42 
43  return s;
44  }
45 
46  using Generator::Generator;
47 
48  inline virtual std::string generate() override {
49  return generate(
50  env().option("length").as_integer(),
51  env().option("seed").as_integer(),
52  env().option("min").as_integer(),
53  env().option("max").as_integer());
54  }
55 };
56 
57 } //ns
58 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Generates a random string of uniformly distributed characters.
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
void swap(IntVector< T > &lhs, IntVector< T > &rhs)
Definition: IntVector.hpp:532
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
Base for string generators.
Definition: Generator.hpp:13
virtual std::string generate() override
Generates a string based on the environment settings.
static std::string generate(size_t length, size_t seed=0, size_t min='0', size_t max='9')
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
void dynamic()
Declares that this option accepts values of a simple type that can be parsed from a string (e...
Definition: Meta.hpp:150