Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Random.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 #include "Rect.h"
29 #include "Vector.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
45  class GF_API Random {
46  public:
55  Random();
56 
67  Random(std::uint_fast32_t seed)
68  : m_engine(seed)
69  {
70 
71  }
72 
80  template<typename T>
81  T computeUniformInteger(T min, T max) {
82  std::uniform_int_distribution<T> dist(min, max);
83  return dist(m_engine);
84  }
85 
93  template<typename T>
94  T computeUniformFloat(T min, T max) {
95  std::uniform_real_distribution<T> dist(min, max);
96  return dist(m_engine);
97  }
98 
106  template<typename T>
107  T computeNormalFloat(T mean, T stddev) {
108  std::normal_distribution<T> dist(mean, stddev);
109  return dist(m_engine);
110  }
111 
118  bool computeBernoulli(double p) {
119  std::bernoulli_distribution dist(p);
120  return dist(m_engine);
121  }
122 
129  Vector2f computePosition(const RectF& area);
130 
137  Vector2i computePosition(const RectI& area);
138 
144  std::mt19937& getEngine() {
145  return m_engine;
146  }
147 
148  private:
149  std::mt19937 m_engine;
150  };
151 
152 #ifndef DOXYGEN_SHOULD_SKIP_THIS
153 }
154 #endif
155 }
156 
157 #endif // GF_RANDOM_H
A random engine.
Definition: Random.h:45
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:107
T computeUniformFloat(T min, T max)
Compute a float with a uniform distribution.
Definition: Random.h:94
Random(std::uint_fast32_t seed)
Constructor with simple initialization.
Definition: Random.h:67
The namespace for gf classes.
Definition: Action.h:34
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:118
std::mt19937 & getEngine()
Get the underlying engine.
Definition: Random.h:144
T computeUniformInteger(T min, T max)
Compute an integer with a uniform distribution.
Definition: Random.h:81