SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Random.hpp
Go to the documentation of this file.
1#ifndef _RANDOM_H_
2#define _RANDOM_H_
3
4#include <random>
5#include <Math/Vector.hpp>
6#include <Math/Color.hpp>
7
8namespace Sleak {
9 namespace Math {
10
11 /**
12 * @class Random
13 * @brief A class for generating random numbers and values.
14 *
15 * This class provides a comprehensive set of functions for generating
16 * various types of random numbers and values, including integers,
17 * floating-point numbers, vectors, and colors. It uses the Mersenne
18 * Twister engine for high-quality random number generation.
19 * @ingroup math
20 */
21 class Random {
22 public:
23 // Seed the generator with a specific value
24 static void Seed(unsigned int seed) {
25 generator_.seed(seed);
26 }
27
28 // Seed the generator with a non-deterministic random value
29 static void SeedWithTime() {
30 std::random_device rd;
31 generator_.seed(rd());
32 }
33
34 // Generate a random integer between min and max (inclusive)
35 static int Range(int min, int max) {
36 std::uniform_int_distribution<int> distribution(min, max);
37 return distribution(generator_);
38 }
39
40 // Generate a random float between 0.0f and 1.0f
41 static float Value() {
42 std::uniform_real_distribution<float> distribution(0.0f, 1.0f);
43 return distribution(generator_);
44 }
45
46 // Generate a random float between min and max
47 static float Range(float min, float max) {
48 std::uniform_real_distribution<float> distribution(min, max);
49 return distribution(generator_);
50 }
51
52 // Generate a random Vector3D with components between 0.0f and 1.0f
56
57 // Generate a random Vector3D with components between min and max
58 static Sleak::Math::Vector3D Vector3D(float min, float max) {
60 Range(min, max), Range(min, max), Range(min, max));
61 }
62
63 // Generate a random Vector2D with components between 0.0f and 1.0f
65 return Vector2D(Value(), Value());
66 }
67
68 // Generate a random Vector2D with components between min and max
69 static Sleak::Math::Vector2D Vector2D(float min, float max) {
70 return Vector2D(Range(min, max), Range(min, max));
71 }
72
73 // Generate a random unit vector (direction)
75 float z = Range(-1.0f, 1.0f);
76 float a = Range(0.0f, 2.0f * PI);
77 float r = sqrt(1.0f - z * z);
78 float x = r * cos(a);
79 float y = r * sin(a);
80 return Sleak::Math::Vector3D(x, y, z);
81 }
82
83 // Generate a random color with components between 0.0f and 1.0f
84 static Sleak::Math::Color Color(bool AlphaIncluded = false) {
85 return Sleak::Math::Color(Value(), Value(), Value(),
86 AlphaIncluded ? Value() : 1.0f); // Assuming Color is RGBA
87 }
88
89 // Generate a random color with components between min and max
90 static Sleak::Math::Color Color(float min, float max, bool AlphaIncluded = false) {
91 return Sleak::Math::Color(Range(min, max), Range(min, max),
92 Range(min, max),
93 AlphaIncluded ? Range(min, max) : 1.0f); // Assuming Color is RGBA
94 }
95
96 // Generate a random boolean value (true or false)
97 static bool Boolean() {
98 return Range(0, 1) == 1;
99 }
100
101 // Generate a random normal distribution with given mean and standard deviation
102 static float NormalDistribution(float mean, float stddev) {
103 std::normal_distribution<float> distribution(mean, stddev);
104 return distribution(generator_);
105 }
106
107 private:
108 inline static std::mt19937 generator_;
109 };
110
111}
112
113}
114
115#endif // _RANDOM_H_
#define PI
Definition Math.hpp:7
A class for generating random numbers and values.
Definition Random.hpp:21
static void Seed(unsigned int seed)
Definition Random.hpp:24
static Sleak::Math::Vector2D Vector2D()
Definition Random.hpp:64
static Sleak::Math::Color Color(float min, float max, bool AlphaIncluded=false)
Definition Random.hpp:90
static Sleak::Math::Vector3D Vector3D(float min, float max)
Definition Random.hpp:58
static void SeedWithTime()
Definition Random.hpp:29
static float Value()
Definition Random.hpp:41
static float Range(float min, float max)
Definition Random.hpp:47
static Sleak::Math::Vector3D Vector3D()
Definition Random.hpp:53
static Sleak::Math::Color Color(bool AlphaIncluded=false)
Definition Random.hpp:84
static Sleak::Math::Vector3D UnitVector()
Definition Random.hpp:74
static bool Boolean()
Definition Random.hpp:97
static Sleak::Math::Vector2D Vector2D(float min, float max)
Definition Random.hpp:69
static float NormalDistribution(float mean, float stddev)
Definition Random.hpp:102
static int Range(int min, int max)
Definition Random.hpp:35
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10