Tenncor
random.hpp
Go to the documentation of this file.
1 
8 #include <random>
9 #include <type_traits>
10 #include <functional>
11 
12 #ifndef ETEQ_RANDOM_HPP
13 #define ETEQ_RANDOM_HPP
14 
15 namespace eteq
16 {
17 
19 using EngineT = std::default_random_engine;
20 
22 template <typename T>
23 using GenF = std::function<T()>;
24 
26 EngineT& get_engine (void);
27 
29 template <typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
30 T unif (const T& a, const T& b)
31 {
32  std::uniform_int_distribution<T> dist(a, b);
33  return dist(get_engine());
34 }
35 
37 template <typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
38 T unif (const T& a, const T& b)
39 {
40  std::uniform_real_distribution<T> dist(a, b);
41  return dist(get_engine());
42 }
43 
45 template <typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
46 GenF<T> unif_gen (const T& a, const T& b)
47 {
48  std::uniform_int_distribution<T> dist(a, b);
49  return std::bind(dist, get_engine());
50 }
51 
53 template <typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
54 GenF<T> unif_gen (T a, T b)
55 {
56  std::uniform_real_distribution<T> dist(a, b);
57  return std::bind(dist, get_engine());
58 }
59 
61 template <typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
62 GenF<T> norm_gen (T mean, T stdev)
63 {
64  std::normal_distribution<T> dist(mean, stdev);
65  return std::bind(dist, get_engine());
66 }
67 
68 }
69 
70 #endif // ETEQ_RANDOM_HPP
Definition: constant.hpp:17
GenF< T > unif_gen(const T &a, const T &b)
Return uniformly generator function that produces numbers between a and b (integers only) ...
Definition: random.hpp:46
std::default_random_engine EngineT
RNG engine used.
Definition: random.hpp:19
GenF< T > norm_gen(T mean, T stdev)
Return normally generator function that produces numbers with mean and stdev (decimals only) ...
Definition: random.hpp:62
T unif(const T &a, const T &b)
Return uniformly generated number between a and b (integers only)
Definition: random.hpp:30
std::function< T()> GenF
Function that returns a generated number.
Definition: random.hpp:23
EngineT & get_engine(void)
Return global random generator.