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 <cmath>
11 #include "../core/common.h"
12 
13 
14 namespace chebyshev {
15 
18  namespace random {
19 
20 
23  struct random_settings {
24 
26  uint64_t seed = 0;
27 
28  } settings;
29 
30 
32  inline void setup(uint64_t seed = 0) {
33 
34  if(seed == 0)
35  seed = time(nullptr);
36 
37  settings.seed = seed;
38  srand(settings.seed);
39  }
40 
41 
43  inline uint64_t natural() {
44  return rand();
45  }
46 
47 
54  inline long double uniform(long double a, long double b) {
55  return (rand() / (long double) RAND_MAX) * (b - a) + a;
56  }
57 
58 
65  template<typename Vector>
66  inline Vector& sample_uniform(Vector& x, const std::vector<prec::interval> intervals) {
67 
68  if(x.size() != intervals.size())
69  throw std::runtime_error(
70  "Vector and domain size mismatch in chebyshev::sample_uniform");
71 
72  for (int i = 0; i < x.size(); ++i)
73  x[i] = uniform(intervals[i].a, intervals[i].b);
74 
75  return x;
76  }
77 
78 
84  inline long double gaussian(long double m, long double s) {
85 
86  const long double x = uniform(0.0, 1.0);
87  const long double y = uniform(0.0, 1.0);
88 
89  const long double v = std::sqrt(-2 * std::log(x));
90  const long double u = v * std::cos(2 * PI_CONST * y);
91 
92  return m + s * u;
93  }
94 
95 
102  inline std::string string(size_t length) {
103 
104  std::string str;
105  str.resize(length);
106 
107  for (unsigned int i = 0; i < length; ++i)
108  str[i] = '!' + (char) (natural() % 95);
109 
110  return str;
111  }
112 
113 
121  inline std::string string(size_t length, std::string alphabet) {
122 
123  if(!alphabet.size())
124  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
125 
126  std::string str;
127  str.resize(length);
128 
129  for (unsigned int i = 0; i < length; ++i)
130  str[i] = alphabet[natural() % alphabet.size()];
131 
132  return str;
133  }
134 
135 
143  inline std::string string(size_t length, std::vector<char> alphabet) {
144 
145  if(!alphabet.size())
146  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
147 
148  std::string str;
149  str.resize(length);
150 
151  for (unsigned int i = 0; i < length; ++i)
152  str[i] = alphabet[natural() % alphabet.size()];
153 
154  return str;
155  }
156 
157 
164  template<typename T>
165  inline std::vector<T> string(size_t length, std::vector<T> alphabet) {
166 
167  if(!alphabet.size())
168  throw std::runtime_error("alphabet in chebyshev::random::string cannot be empty.");
169 
170  std::vector<T> str;
171  str.resize(length);
172 
173  for (unsigned int i = 0; i < length; ++i)
174  str[i] = alphabet[natural() % alphabet.size()];
175 
176  return str;
177  }
178 
179  }
180 }
181 
182 #endif
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
uint64_t natural()
Generate a random natural number.
Definition: random.h:43
long double uniform(long double a, long double b)
Generate a uniformly distributed random number.
Definition: random.h:54
void setup(uint64_t seed=0)
Initialize the random module.
Definition: random.h:32
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:66
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:165
long double gaussian(long double m, long double s)
Generate a Gaussian distributed random number.
Definition: random.h:84
General namespace of the framework.
Definition: benchmark_structures.h:16
const long double PI_CONST
The Pi mathematical constant.
Definition: common.h:61
Settings for the random module.
Definition: random.h:23
uint64_t seed
The seed for random number generation.
Definition: random.h:26