Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Random.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 <limits>
26#include <random>
27
28#include "Circ.h"
29#include "CoreApi.h"
30#include "Id.h"
31#include "Rect.h"
32#include "Vector.h"
33
34namespace gf {
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36inline namespace v1 {
37#endif
38
46 class GF_CORE_API RandomEngine {
47 public:
48 using result_type = uint64_t;
49
51
53
54 static constexpr result_type min() {
55 return std::numeric_limits<result_type>::min();
56 }
57
58 static constexpr result_type max() {
59 return std::numeric_limits<result_type>::max();
60 }
61
63
64 void shortJump();
65 void longJump();
66
67 private:
68 uint64_t next();
69
70 private:
71 uint64_t m_state[4];
72 };
73
83 class GF_CORE_API Random {
84 public:
94
105 Random(uint64_t seed)
106 : m_engine(seed)
107 {
108
109 }
110
118 template<typename T>
119 T computeUniformInteger(T min, T max) {
120 std::uniform_int_distribution<T> dist(min, max);
121 return dist(m_engine);
122 }
123
131 template<typename T>
132 T computeUniformFloat(T min, T max) {
133 std::uniform_real_distribution<T> dist(min, max);
134 return dist(m_engine);
135 }
136
144 template<typename T>
145 T computeNormalFloat(T mean, T stddev) {
146 std::normal_distribution<T> dist(mean, stddev);
147 return dist(m_engine);
148 }
149
156 bool computeBernoulli(double p) {
157 std::bernoulli_distribution dist(p);
158 return dist(m_engine);
159 }
160
168
176
184
192 float computeRadius(float radiusMin, float radiusMax);
193
200
207
214 return m_engine;
215 }
216
217 private:
218 RandomEngine m_engine;
219 };
220
221#ifndef DOXYGEN_SHOULD_SKIP_THIS
222}
223#endif
224}
225
226#endif // GF_RANDOM_H
A random engine.
Definition: Random.h:46
static constexpr result_type max()
Definition: Random.h:58
result_type operator()()
static constexpr result_type min()
Definition: Random.h:54
RandomEngine(result_type seed)
uint64_t result_type
Definition: Random.h:48
A set of random utilities.
Definition: Random.h:83
float computeRadius(float radiusMin, float radiusMax)
Compute a uniform radius.
Random()
Default constructor with complex initialization.
RandomEngine & getEngine()
Get the underlying engine.
Definition: Random.h:213
bool computeBernoulli(double p)
Compute a boolean with a Bernoulli distribution.
Definition: Random.h:156
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:132
Vector2f computePosition(const CircF &area)
Compute a uniform position in a circle.
Random(uint64_t seed)
Constructor with simple initialization.
Definition: Random.h:105
T computeNormalFloat(T mean, T stddev)
Compute a float with a normal (Gaussian) distribution.
Definition: Random.h:145
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:119
uint64_t Id
An identifier.
Definition: Id.h:37
The namespace for gf classes.
Utility class for manipulating circles.
Definition: Circ.h:61