Xped
Loading...
Searching...
No Matches
Random.hpp
Go to the documentation of this file.
1#ifndef XPED_RANDOM_H__
2#define XPED_RANDOM_H__
3
4#include <complex>
5#include <random>
6#include <thread>
7
8namespace Xped::random {
9
10template <typename Scalar, typename RealScalar>
11inline Scalar threadSafeRandUniform(RealScalar, RealScalar, bool = false){};
12
13template <>
14inline double threadSafeRandUniform<double, double>(double min, double max, bool FIXED_SEED)
15{
16 static thread_local std::mt19937 generatorUniformReal(std::random_device{}());
17 if(FIXED_SEED) generatorUniformReal.seed(std::time(0));
18 std::uniform_real_distribution<double> distribution(min, max);
19 return distribution(generatorUniformReal);
20}
21
22template <>
23inline std::complex<double> threadSafeRandUniform<std::complex<double>, double>(double min, double max, bool FIXED_SEED)
24{
25 static thread_local std::mt19937 generatorUniformComplex(std::random_device{}());
26 if(FIXED_SEED) generatorUniformComplex.seed(std::time(0));
27 std::uniform_real_distribution<double> distribution(min, max);
28 return std::complex<double>(distribution(generatorUniformComplex), distribution(generatorUniformComplex));
29}
30
31template <>
32inline int threadSafeRandUniform<int, int>(int min, int max, bool FIXED_SEED)
33{
34 static thread_local std::mt19937 generatorUniformInt(std::random_device{}());
35 if(FIXED_SEED) generatorUniformInt.seed(std::time(0));
36 std::uniform_int_distribution<int> distribution(min, max);
37 return distribution(generatorUniformInt);
38}
39} // namespace Xped::random
40#endif
Definition: Random.hpp:8
double threadSafeRandUniform< double, double >(double min, double max, bool FIXED_SEED)
Definition: Random.hpp:14
int threadSafeRandUniform< int, int >(int min, int max, bool FIXED_SEED)
Definition: Random.hpp:32
Scalar threadSafeRandUniform(RealScalar, RealScalar, bool=false)
Definition: Random.hpp:11