Evoplex  0.2.1
prg.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PRG_H
18 #define PRG_H
19 
20 #include <random>
21 
22 namespace evoplex {
23 
29 class PRG
30 {
31 public:
36  explicit PRG(unsigned int seed);
37 
41  inline unsigned int seed() const
42  { return m_seed; }
43 
50  inline bool bernoulli(double p)
51  { std::bernoulli_distribution b(p); return b(m_mteng); }
52 
56  inline bool bernoulli()
57  { return m_bernoulli(m_mteng); }
58 
62  template <typename T>
63  T uniform(T min, T max)
64  { std::uniform_real_distribution<T> d(min, max); return d(m_mteng); }
65 
69  inline int uniform(int min, int max)
70  { std::uniform_int_distribution<int> d(min, max); return d(m_mteng); }
71 
75  inline size_t uniform(size_t min, size_t max)
76  { std::uniform_int_distribution<size_t> d(min, max); return d(m_mteng); }
77 
81  template <typename T>
82  T uniform(T max)
83  { std::uniform_real_distribution<T> d(0, max); return d(m_mteng); }
84 
88  inline int uniform(int max)
89  { std::uniform_int_distribution<int> d(0, max); return d(m_mteng); }
90 
94  inline size_t uniform(size_t max)
95  { std::uniform_int_distribution<size_t> d(0, max); return d(m_mteng); }
96 
100  inline double uniform()
101  { return m_doubleZeroOne(m_mteng); }
102 
106  template <typename T>
107  inline T uniform(std::uniform_real_distribution<T> d)
108  { return d(m_mteng); }
109 
113  template <typename T>
114  T uniform(std::uniform_int_distribution<T> d)
115  { return d(m_mteng); }
116 
117 private:
118  const unsigned int m_seed;
119  std::mt19937 m_mteng;
120  std::uniform_real_distribution<double> m_doubleZeroOne;
121  std::bernoulli_distribution m_bernoulli;
122 };
123 
124 } // evoplex
125 #endif // PRG_H
T uniform(T min, T max)
Generates a random double/float [min, max).
Definition: prg.h:63
bool bernoulli(double p)
Bernoulli distribution.
Definition: prg.h:50
T uniform(std::uniform_real_distribution< T > d)
Uniform continuous distribution for random numbers.
Definition: prg.h:107
double uniform()
Generates a random double [0, 1).
Definition: prg.h:100
unsigned int seed() const
Gets the initial PRG seed.
Definition: prg.h:41
int uniform(int min, int max)
Generates a random integer [min, max].
Definition: prg.h:69
size_t uniform(size_t min, size_t max)
Generates a random size_t [min, max].
Definition: prg.h:75
int uniform(int max)
Generates a random integer [0, max].
Definition: prg.h:88
size_t uniform(size_t max)
Generates a random size_t [0, max].
Definition: prg.h:94
bool bernoulli()
randBernoulli(p=0.5) alias.
Definition: prg.h:56
T uniform(std::uniform_int_distribution< T > d)
Uniform discrete distribution for random numbers.
Definition: prg.h:114
Pseudo-random number generator.
Definition: prg.h:29
PRG(unsigned int seed)
PRG constructor.
Definition: abstractgraph.h:29
T uniform(T max)
Generates a random double/float [0, max).
Definition: prg.h:82