FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_random.inl
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace fge
18{
19
20template<typename TEngine>
21Random<TEngine>::Random() :
22 g_engine(std::chrono::system_clock::now().time_since_epoch().count())
23{}
24template<typename TEngine>
26 g_engine(seed)
27{}
28
29template<typename TEngine>
30void Random<TEngine>::setSeed(uint64_t seed)
31{
32 std::scoped_lock<std::mutex> const lck(this->g_mutex);
33 this->g_engine.seed(seed);
34}
35
36template<typename TEngine>
37TEngine const& Random<TEngine>::getEngine() const
38{
39 return this->g_engine;
40}
41template<typename TEngine>
43{
44 return this->g_engine;
45}
46
47template<typename TEngine>
48template<typename T>
50{
51 static_assert(std::is_arithmetic<T>::value, "T must be arithmetic !");
52 std::scoped_lock<std::mutex> const lck(this->g_mutex);
53
54 if constexpr (std::is_floating_point<T>::value)
55 {
56 std::uniform_real_distribution<T> uniform_dist(min, max);
57 return uniform_dist(this->g_engine);
58 }
59 else
60 {
61 std::uniform_int_distribution<T> uniform_dist(min, max);
62 return uniform_dist(this->g_engine);
63 }
64}
65
66template<typename TEngine>
67template<typename T>
69{
70 static_assert(std::is_arithmetic<T>::value, "T must be arithmetic !");
71 std::scoped_lock<std::mutex> const lck(this->g_mutex);
72
73 if constexpr (std::is_floating_point<T>::value)
74 {
75 std::uniform_real_distribution<T> uniform_dist(-1, 1);
76 return uniform_dist(this->g_engine);
77 }
78 else
79 {
80 std::uniform_int_distribution<T> uniform_dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
81 return uniform_dist(this->g_engine);
82 }
83}
84
85template<typename TEngine>
86template<typename T>
87fge::Vector2<T> Random<TEngine>::rangeVec2(T min_x, T max_x, T min_y, T max_y)
88{
89 return fge::Vector2<T>(this->range<T>(min_x, max_x), this->range<T>(min_y, max_y));
90}
91
92template<typename TEngine>
93template<typename T>
94fge::Vector3<T> Random<TEngine>::rangeVec3(T min_x, T max_x, T min_y, T max_y, T min_z, T max_z)
95{
96 return fge::Vector3<T>(this->range<T>(min_x, max_x), this->range<T>(min_y, max_y), this->range<T>(min_z, max_z));
97}
98
99template<typename TEngine>
100fge::Color Random<TEngine>::rangeColor(uint32_t min, uint32_t max)
101{
102 return fge::Color(this->range<uint32_t>(min, max));
103}
104template<typename TEngine>
106 uint8_t max_r,
107 uint8_t min_g,
108 uint8_t max_g,
109 uint8_t min_b,
110 uint8_t max_b,
111 uint8_t min_a,
112 uint8_t max_a)
113{
114 return fge::Color(this->range<uint8_t>(min_r, max_r), this->range<uint8_t>(min_g, max_g),
115 this->range<uint8_t>(min_b, max_b), this->range<uint8_t>(min_a, max_a));
116}
117
118template<typename TEngine>
119template<typename T>
121{
122 return fge::Vector2<T>(this->rand<T>(), this->rand<T>());
123}
124
125template<typename TEngine>
126template<typename T>
128{
129 return fge::Vector3<T>(this->rand<T>(), this->rand<T>(), this->rand<T>());
130}
131
132template<typename TEngine>
134{
135 return fge::Color(this->rand<uint32_t>());
136}
137
138template<typename TEngine>
139std::string Random<TEngine>::randStr(std::size_t length, std::string const& bucket)
140{
141 if ((length == 0) || bucket.empty())
142 {
143 return {};
144 }
145
146 std::string result;
147 result.resize(length);
148
149 for (std::size_t i = 0; i < length; ++i)
150 {
151 result[i] = bucket[this->range<std::size_t>(0, bucket.size() - 1)];
152 }
153
154 return result;
155}
156
157} // namespace fge
Definition C_color.hpp:35
A class to generate random numbers.
Definition C_random.hpp:43
fge::Vector2< T > rangeVec2(T min_x, T max_x, T min_y, T max_y)
Generate a random vector2 within a range.
Definition C_random.inl:87
fge::Vector2< T > randVec2()
Generate a random vector2.
Definition C_random.inl:120
fge::Vector3< T > randVec3()
Generate a random vector3.
Definition C_random.inl:127
fge::Color rangeColor(uint32_t min, uint32_t max)
Generate a random color.
Definition C_random.inl:100
fge::Color randColor()
Generate a random color.
Definition C_random.inl:133
TEngine const & getEngine() const
Get the random engine.
Definition C_random.inl:37
void setSeed(uint64_t seed)
Set the 64-bit seed of the random number generator.
Definition C_random.inl:30
fge::Vector3< T > rangeVec3(T min_x, T max_x, T min_y, T max_y, T min_z, T max_z)
Generate a random vector3 within a range.
Definition C_random.inl:94
T range(T min, T max)
Generate a random number within a range.
Definition C_random.inl:49
T rand()
Generate a random value.
Definition C_random.inl:68
std::string randStr(std::size_t length, std::string const &bucket="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789")
Generate a random character sequence.
Definition C_random.inl:139