Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Random.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Circ.h"
28 #include "Portability.h"
29 #include "Rect.h"
30 #include "Vector.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
46  class GF_API Random {
47  public:
56  Random();
57 
68  Random(std::uint_fast32_t seed)
69  : m_engine(seed)
70  {
71 
72  }
73 
81  template<typename T>
82  T computeUniformInteger(T min, T max) {
83  std::uniform_int_distribution<T> dist(min, max);
84  return dist(m_engine);
85  }
86 
94  template<typename T>
95  T computeUniformFloat(T min, T max) {
96  std::uniform_real_distribution<T> dist(min, max);
97  return dist(m_engine);
98  }
99 
107  template<typename T>
108  T computeNormalFloat(T mean, T stddev) {
109  std::normal_distribution<T> dist(mean, stddev);
110  return dist(m_engine);
111  }
112 
119  bool computeBernoulli(double p) {
120  std::bernoulli_distribution dist(p);
121  return dist(m_engine);
122  }
123 
130  Vector2f computePosition(const RectF& area);
131 
138  Vector2i computePosition(const RectI& area);
139 
146  Vector2f computePosition(const CircF& area);
147 
155  float computeRadius(float radiusMin, float radiusMax);
156 
162  float computeAngle();
163 
169  std::mt19937& getEngine() {
170  return m_engine;
171  }
172 
173  private:
174  std::mt19937 m_engine;
175  };
176 
177 #ifndef DOXYGEN_SHOULD_SKIP_THIS
178 }
179 #endif
180 }
181 
182 #endif // GF_RANDOM_H
A random engine.
Definition: Random.h:46
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:108
T computeUniformFloat(T min, T max)
Compute a float with a uniform distribution.
Definition: Random.h:95
Random(std::uint_fast32_t seed)
Constructor with simple initialization.
Definition: Random.h:68
The namespace for gf classes.
Definition: Action.h:35
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:119
std::mt19937 & getEngine()
Get the underlying engine.
Definition: Random.h:169
T computeUniformInteger(T min, T max)
Compute an integer with a uniform distribution.
Definition: Random.h:82