6 #ifndef CHEBYSHEV_RANDOM_H
7 #define CHEBYSHEV_RANDOM_H
11 #include "../core/common.h"
32 inline void setup(uint64_t seed = 0) {
54 inline long double uniform(
long double a,
long double b) {
55 return (rand() / (
long double) RAND_MAX) * (b - a) + a;
65 template<
typename Vector>
66 inline Vector&
sample_uniform(Vector& x,
const std::vector<prec::interval> intervals) {
68 if(x.size() != intervals.size())
69 throw std::runtime_error(
70 "Vector and domain size mismatch in chebyshev::sample_uniform");
72 for (
int i = 0; i < x.size(); ++i)
73 x[i] =
uniform(intervals[i].a, intervals[i].b);
84 inline long double gaussian(
long double m,
long double s) {
86 const long double x =
uniform(0.0, 1.0);
87 const long double y =
uniform(0.0, 1.0);
89 const long double v = std::sqrt(-2 * std::log(x));
90 const long double u = v * std::cos(2 *
PI_CONST * y);
107 for (
unsigned int i = 0; i < length; ++i)
108 str[i] =
'!' + (
char) (
natural() % 95);
124 throw std::runtime_error(
"alphabet in chebyshev::random::string cannot be empty.");
129 for (
unsigned int i = 0; i < length; ++i)
130 str[i] = alphabet[
natural() % alphabet.size()];
146 throw std::runtime_error(
"alphabet in chebyshev::random::string cannot be empty.");
151 for (
unsigned int i = 0; i < length; ++i)
152 str[i] = alphabet[
natural() % alphabet.size()];
165 inline std::vector<T>
string(
size_t length, std::vector<T> alphabet) {
168 throw std::runtime_error(
"alphabet in chebyshev::random::string cannot be empty.");
173 for (
unsigned int i = 0; i < length; ++i)
174 str[i] = alphabet[
natural() % alphabet.size()];
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