ExRandom  3.0
table_gen.hpp
Go to the documentation of this file.
1 /**
2  * @file table_gen.hpp
3  * @author Charles Karney <charles.karney@sri.com>
4  * @brief Definition of table_gen
5  *
6  * Copyright (c) Charles Karney (2014) and licensed under the MIT/X11 License.
7  * For more information, see http://exrandom.sourceforge.net/
8  */
9 
10 #if !defined(EXRANDOM_TABLE_GEN_HPP)
11 #define EXRANDOM_TABLE_GEN_HPP 1
12 
13 #include <string>
14 #include <algorithm> // for std::min
15 #include <stdexcept> // for std::runtime_error
16 
18 
19 namespace exrandom {
20 
21  /**
22  * @brief a class to generate tabulated random numbers.
23  *
24  * The "seed" for this generate is a std::string of decimal digits.
25  */
26  class table_gen {
27  public:
28  /**
29  * The constructor.
30  *
31  * @param seed the string of random digits.
32  */
33  table_gen(const std::string& seed = "") : _seed(seed), _pos(0U) {}
34  /**
35  * Reset the seed.
36  *
37  * @param seed the new string of random digits.
38  */
39  void seed(const std::string& seed = "") { _seed = seed; _pos = 0U; }
40  /**
41  * @return the value of the next random digit.
42  *
43  * If there are no more digits left, throw an exception.
44  */
45  uint_t operator()() {
46  if (_pos >= _seed.size())
47  throw std::runtime_error("table_gen: overflow");
48  return (std::min)(uint_t(_seed[_pos++] - '0'), uint_t(9));
49  }
50  private:
51  std::string _seed;
52  size_t _pos;
53  };
54 
55 }
56 
57 #endif // EXRANDOM_TABLE_GEN_HPP
void seed(const std::string &seed="")
Definition: table_gen.hpp:39
Definition of digit_arithmetic.
a class to generate tabulated random numbers.
Definition: table_gen.hpp:26
table_gen(const std::string &seed="")
Definition: table_gen.hpp:33
The common namespace.
Definition: aux_info.hpp:18