eDSP  0.0.1
A cross-platform DSP library written in C++.
random_generator.hpp
Go to the documentation of this file.
1 /*
2  * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3  * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation, either version 3 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along width
16  * this program. If not, see <http://www.gnu.org/licenses/>
17  *
18  * File: random_generator.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 31/7/2018
21  */
22 #ifndef EDSP_FISHER_DISTRIBUTION_HPP
23 #define EDSP_FISHER_DISTRIBUTION_HPP
24 
25 #include <random>
26 #include <chrono>
27 
28 namespace edsp { namespace random {
29 
34  enum class DistributionType {
35  Uniform, /*<! Produces real values evenly distributed across a range */
36  Bernoulli, /*<! Produces random values on a Bernoulli distribution */
37  Binomial, /*<! Produces random values on a binomial distribution */
38  Geometric, /*<! Produces random values on a geometric distribution */
39  Poisson, /*<! Produces random values on a Poisson distribution */
40  Exponential, /*<! Produces random values on a exponential distribution */
41  Gamma, /*<! Produces random values on a Gamma distribution */
42  Weibull, /*<! Produces random values on a Weibull distribution */
43  ExtremeValue, /*<! Produces random values on a extreme value distribution */
44  Normal, /*<! Produces random values on a normal distribution */
45  LogNormal, /*<! Produces random values on a logarithmic normal distribution */
46  ChiSquared, /*<! Produces random values on a chi-squared distribution */
47  Cauchy, /*<! Produces random values on a Cauchy distribution */
48  Fisher, /*<! Produces random values on a Fisher distribution */
49  Student, /*<! Produces random values on a Student distribution */
50  Discrete, /*<! Produces random values on a discrete distribution */
51  PieceWiseConstant, /*<! Produces real values distributed on constant subintervals. */
52  PieceWiseLinear /*<! Produces real values distributed on defined subintervals. */
53  };
54 
55  namespace internal {
56 
57  template <typename Distribution, typename Engine = std::mt19937>
58  struct RandomGeneratorImpl {
59  using value_type = typename Distribution::value_type;
60  template <typename... Args>
61  explicit RandomGeneratorImpl(Args... arg) :
62  generator_(
63  Engine(static_cast<std::size_t>(std::chrono::system_clock::now().time_since_epoch().count()))),
64  distribution_(Distribution(std::forward(arg...))) {}
65 
66  inline value_type operator()() {
67  return static_cast<value_type>(distribution_(generator_));
68  }
69 
70  private:
71  Engine generator_;
72  Distribution distribution_;
73  };
74 
75  template <DistributionType Type, typename T>
76  struct _RandomGenerator;
77 
78  template <typename T>
79  struct _RandomGenerator<DistributionType::Uniform, T>
80  : public RandomGeneratorImpl<std::uniform_real_distribution<T>> {};
81 
82  template <typename T>
83  struct _RandomGenerator<DistributionType::Bernoulli, T>
84  : public RandomGeneratorImpl<std::bernoulli_distribution, T> {};
85 
86  template <typename T>
87  struct _RandomGenerator<DistributionType::Binomial, T>
88  : public RandomGeneratorImpl<std::binomial_distribution<T>> {};
89 
90  template <typename T>
91  struct _RandomGenerator<DistributionType::Geometric, T>
92  : public RandomGeneratorImpl<std::geometric_distribution<T>> {};
93 
94  template <typename T>
95  struct _RandomGenerator<DistributionType::Poisson, T>
96  : public RandomGeneratorImpl<std::poisson_distribution<T>> {};
97 
98  template <typename T>
99  struct _RandomGenerator<DistributionType::Exponential, T>
100  : public RandomGeneratorImpl<std::exponential_distribution<T>> {};
101 
102  template <typename T>
103  struct _RandomGenerator<DistributionType::Gamma, T> : public RandomGeneratorImpl<std::gamma_distribution<T>> {};
104 
105  template <typename T>
106  struct _RandomGenerator<DistributionType::Weibull, T>
107  : public RandomGeneratorImpl<std::weibull_distribution<T>> {};
108 
109  template <typename T>
110  struct _RandomGenerator<DistributionType::ExtremeValue, T>
111  : public RandomGeneratorImpl<std::extreme_value_distribution<T>> {};
112 
113  template <typename T>
114  struct _RandomGenerator<DistributionType::Normal, T> : public RandomGeneratorImpl<std::normal_distribution<T>> {
115  };
116 
117  template <typename T>
118  struct _RandomGenerator<DistributionType::LogNormal, T>
119  : public RandomGeneratorImpl<std::lognormal_distribution<T>> {};
120 
121  template <typename T>
122  struct _RandomGenerator<DistributionType::ChiSquared, T>
123  : public RandomGeneratorImpl<std::chi_squared_distribution<T>> {};
124 
125  template <typename T>
126  struct _RandomGenerator<DistributionType::Fisher, T>
127  : public RandomGeneratorImpl<std::fisher_f_distribution<T>> {};
128 
129  template <typename T>
130  struct _RandomGenerator<DistributionType::Student, T>
131  : public RandomGeneratorImpl<std::student_t_distribution<T>> {};
132 
133  template <typename T>
134  struct _RandomGenerator<DistributionType::Discrete, T>
135  : public RandomGeneratorImpl<std::discrete_distribution<T>> {};
136 
137  template <typename T>
138  struct _RandomGenerator<DistributionType::PieceWiseConstant, T>
139  : public RandomGeneratorImpl<std::piecewise_constant_distribution<T>> {};
140 
141  template <typename T>
142  struct _RandomGenerator<DistributionType::PieceWiseLinear, T>
143  : public RandomGeneratorImpl<std::piecewise_linear_distribution<T>> {};
144 
145  template <typename T>
146  struct _RandomGenerator<DistributionType::Cauchy, T> : public RandomGeneratorImpl<std::cauchy_distribution<T>> {
147  };
148 
149  } // namespace internal
150 
158  template <DistributionType dist, typename T>
160  using value_type = T;
161 
167  template <typename... Args>
168  explicit random_generator(Args... arg) : generator_(arg...) {}
169 
175  return generator_.operator()();
176  }
177 
178  private:
179  internal::_RandomGenerator<dist, T> generator_;
180  };
181 
182 }} // namespace edsp::random
183 
184 #endif // EDSP_RANDOM_GENERATORDISTRIBUTION_HPP
This class implements a random generator according to one of the discrete probability function availa...
Definition: random_generator.hpp:159
T value_type
Definition: random_generator.hpp:160
value_type operator()()
Generates a random number based in the chosen distribution.
Definition: random_generator.hpp:174
random_generator(Args... arg)
Creates a random generator.
Definition: random_generator.hpp:168
DistributionType
The DistributionType enum represents all the available distributions in the pseudo-random number gene...
Definition: random_generator.hpp:34
Definition: amplifier.hpp:29