Chebyshev
Unit testing for scientific software
random.h
Go to the documentation of this file.
1 
5 
6 #ifndef CHEBYSHEV_RANDOM_H
7 #define CHEBYSHEV_RANDOM_H
8 
9 #include <cstdlib>
10 #include "../prec/interval.h"
11 
12 
13 namespace chebyshev {
14 
17  namespace random {
18 
19 
22  struct random_settings {
23 
25  uint64_t seed = 0;
26 
27  } settings;
28 
29 
31  inline void setup(uint64_t seed = 0) {
32 
33  if(seed == 0)
34  seed = time(nullptr);
35 
36  settings.seed = seed;
37  srand(settings.seed);
38  }
39 
40 
42  inline uint64_t natural() {
43  return rand();
44  }
45 
46 
53  inline long double uniform(long double a, long double b) {
54  return (rand() / (long double) RAND_MAX) * (b - a) + a;
55  }
56 
57 
64  template<typename Vector>
65  inline Vector& sample_uniform(Vector& x, const std::vector<prec::interval> intervals) {
66 
67  if(x.size() != intervals.size())
68  throw std::runtime_error(
69  "Vector and domain size mismatch in chebyshev::sample_uniform");
70 
71  for (int i = 0; i < x.size(); ++i)
72  x[i] = uniform(intervals[i].a, intervals[i].b);
73 
74  return x;
75  }
76 
77 
84  inline std::string string(size_t length) {
85 
86  std::string str;
87  str.resize(length);
88 
89  for (unsigned int i = 0; i < length; ++i)
90  str[i] = '!' + (char) (natural() % 95);
91 
92  return str;
93  }
94 
95 
103  inline std::string string(size_t length, std::string alphabet) {
104 
105  if(!alphabet.size())
106  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
107 
108  std::string str;
109  str.resize(length);
110 
111  for (unsigned int i = 0; i < length; ++i)
112  str[i] = alphabet[natural() % alphabet.size()];
113 
114  return str;
115  }
116 
117 
125  inline std::string string(size_t length, std::vector<char> alphabet) {
126 
127  if(!alphabet.size())
128  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
129 
130  std::string str;
131  str.resize(length);
132 
133  for (unsigned int i = 0; i < length; ++i)
134  str[i] = alphabet[natural() % alphabet.size()];
135 
136  return str;
137  }
138 
139 
146  template<typename T>
147  inline std::vector<T> string(size_t length, std::vector<T> alphabet) {
148 
149  if(!alphabet.size())
150  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
151 
152  std::vector<T> str;
153  str.resize(length);
154 
155  for (unsigned int i = 0; i < length; ++i)
156  str[i] = alphabet[natural() % alphabet.size()];
157 
158  return str;
159  }
160 
161  }
162 }
163 
164 #endif
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:84
uint64_t natural()
Generate a random natural number.
Definition: random.h:42
long double uniform(long double a, long double b)
Generate a uniformly distributed random number.
Definition: random.h:53
void setup(uint64_t seed=0)
Initialize the random module.
Definition: random.h:31
Vector & sample_uniform(Vector &x, const std::vector< prec::interval > intervals)
Fill an already allocated vector with uniformly distributed numbers over different intervals.
Definition: random.h:65
std::vector< T > string(size_t length, std::vector< T > alphabet)
Generate a random string made of the elements of the given alphabet, of arbitrary type.
Definition: random.h:147
General namespace of the framework.
Definition: benchmark_structures.h:16
Definition: random.h:22
uint64_t seed
The seed for random number generation.
Definition: random.h:25