Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Random.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_RANDOM_H
22 #define GF_RANDOM_H
23 
24 #include <cstdint>
25 #include <random>
26 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
43  class GF_API Random {
44  public:
53  Random();
54 
65  Random(std::uint_fast32_t seed)
66  : m_engine(seed)
67  {
68 
69  }
70 
78  template<typename T>
79  T computeUniformInteger(T min, T max) {
80  std::uniform_int_distribution<T> dist(min, max);
81  return dist(m_engine);
82  }
83 
91  template<typename T>
92  T computeUniformFloat(T min, T max) {
93  std::uniform_real_distribution<T> dist(min, max);
94  return dist(m_engine);
95  }
96 
104  template<typename T>
105  T computeNormalFloat(T mean, T stddev) {
106  std::normal_distribution<T> dist(mean, stddev);
107  return dist(m_engine);
108  }
109 
116  bool computeBernoulli(double p) {
117  std::bernoulli_distribution dist(p);
118  return dist(m_engine);
119  }
120 
126  std::mt19937& getEngine() {
127  return m_engine;
128  }
129 
130  private:
131  std::mt19937 m_engine;
132  };
133 
134 #ifndef DOXYGEN_SHOULD_SKIP_THIS
135 }
136 #endif
137 }
138 
139 #endif // GF_RANDOM_H
A random engine.
Definition: Random.h:43
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:105
T computeUniformFloat(T min, T max)
Compute a float with a uniform distribution.
Definition: Random.h:92
Random(std::uint_fast32_t seed)
Constructor with simple initialization.
Definition: Random.h:65
The namespace for gf classes.
Definition: Action.h:34
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:116
std::mt19937 & getEngine()
Get the underlying engine.
Definition: Random.h:126
T computeUniformInteger(T min, T max)
Compute an integer with a uniform distribution.
Definition: Random.h:79