Gamedev Framework (gf) 0.21.0
A C++17 framework for 2D games
Random.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2021 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 "CoreApi.h"
29#include "Id.h"
30#include "Rect.h"
31#include "Vector.h"
32
33namespace gf {
34#ifndef DOXYGEN_SHOULD_SKIP_THIS
35inline namespace v1 {
36#endif
37
47 class GF_CORE_API Random {
48 public:
58
69 Random(std::uint_fast32_t seed)
70 : m_engine(seed)
71 {
72
73 }
74
82 template<typename T>
83 T computeUniformInteger(T min, T max) {
84 std::uniform_int_distribution<T> dist(min, max);
85 return dist(m_engine);
86 }
87
95 template<typename T>
96 T computeUniformFloat(T min, T max) {
97 std::uniform_real_distribution<T> dist(min, max);
98 return dist(m_engine);
99 }
100
108 template<typename T>
109 T computeNormalFloat(T mean, T stddev) {
110 std::normal_distribution<T> dist(mean, stddev);
111 return dist(m_engine);
112 }
113
120 bool computeBernoulli(double p) {
121 std::bernoulli_distribution dist(p);
122 return dist(m_engine);
123 }
124
132
140
148
156 float computeRadius(float radiusMin, float radiusMax);
157
164
171
177 std::mt19937& getEngine() {
178 return m_engine;
179 }
180
181 private:
182 std::mt19937 m_engine;
183 };
184
185#ifndef DOXYGEN_SHOULD_SKIP_THIS
186}
187#endif
188}
189
190#endif // GF_RANDOM_H
A random engine.
Definition: Random.h:47
float computeRadius(float radiusMin, float radiusMax)
Compute a uniform radius.
std::mt19937 & getEngine()
Get the underlying engine.
Definition: Random.h:177
Random(std::uint_fast32_t seed)
Constructor with simple initialization.
Definition: Random.h:69
Random()
Default constructor with complex initialization.
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:120
float computeAngle()
Compute a uniform angle in the range .
T computeUniformFloat(T min, T max)
Compute a float with a uniform distribution.
Definition: Random.h:96
Vector2f computePosition(const CircF &area)
Compute a uniform position in a circle.
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:109
gf::Id computeId()
Compute an id.
Vector2i computePosition(const RectI &area)
Compute a uniform position in a given area.
Vector2f computePosition(const RectF &area)
Compute a uniform position in a given area.
T computeUniformInteger(T min, T max)
Compute an integer with a uniform distribution.
Definition: Random.h:83
uint64_t Id
An identifier.
Definition: Id.h:37
The namespace for gf classes.
Definition: Action.h:35
Utility class for manipulating circles.
Definition: Circ.h:61