dl
dl_random.h
浏览该文件的文档.
1
10#pragma once
11
12#include <random>
13#include "io/dl_log.h"
14#include "base/dl_type_def.h"
15#include "math/dl_algorithm.h"
16#include "memory/dl_buffer.h"
17
18
19namespace dl
20{
22class Random
23{
24 using FloatType = Float;
25public:
30 static void SetSeed(uint32_t seed)
31 {
32 if (seed == 0)
33 {
34 std::random_device rd;
35 _re.seed(_seed = rd());
36 }
37 else
38 _re.seed(_seed = seed);
39 }
40
44 template <typename T>
45 static T Float(T min, T max)
46 {
47 //参数检查
48 if (min > max)
49 {
50 log("min > max!");
51 std::swap(min, max);
52 }
53 static std::uniform_real_distribution<T> dist_double;
54 //设定区间
55 using D = std::decay_t<decltype(dist_double)>;
56 using P = typename D::param_type;
57 dist_double.param(P{min, max});
58 return dist_double(_re);
59 }
60
65 template <typename T>
66 static T Integer(T min, T max)
67 {
68 //参数检查
69 if (min > max)
70 {
71 log("min > max!");
72 std::swap(min, max);
73 }
74 static std::uniform_int_distribution<T> dist_int;
75 //设定区间
76 using D = std::decay_t<decltype(dist_int)>;
77 using P = typename D::param_type;
78 dist_int.param(P{min, max});
79 return dist_int(_re);
80 }
81
86 template <typename T>
87 static T Normal(T u = 0, T o = 1)
88 {
89 static std::normal_distribution<T> dist;
90 //设定区间
91 using D = std::decay_t<decltype(dist)>;
92 using P = typename D::param_type;
93 dist.param(P{ u, o });
94 return dist(_re);
95 }
96
97 static FloatType FloatNormal(FloatType u = 0_f, FloatType o = 1_f)
98 {
99 return Normal(u, o);
100 }
101
104 static FloatType FloatNormalX(
105 FloatType scale = 1/3_f,
106 FloatType min = -1, FloatType max = 1)
107 {
108#if DL_PLATFORM == DL_PLATFORM_LINUX
109 return dl::clamp(FloatNormal() * scale, min, max);
110#else
111 return std::clamp(FloatNormal() * scale, min, max);
112#endif
113 }
114
115
116 static bool Bool()
117 {
118 return Integer(0, 1) == 1;
119 }
120
124 static int Int(int min, int max)
125 {
126 return Integer(min, max);
127 }
128
131 static unsigned Uint(unsigned min, unsigned max)
132 {
133 return Integer(min, max);
134 }
135
138 static size_t SizeT(size_t min, size_t max)
139 {
140 return Integer(min, max);
141 }
142 template<size_t N>
143 static std::array<std::byte, N> Array()
144 {
145 std::array<std::byte, N> ret;
146 size_t X = N / 8;
147 size_t Y = N % 8;
148 for (size_t i = 0; i < X; ++i)
149 {
150 ((uint64_t*)ret.data())[i] = Integer<uint64_t>(0, (std::numeric_limits<uint64_t>::max)());
151 }
152 uint8_t* p = (uint8_t*)ret.data() + X * 8;
153 for (size_t j = 0; j < Y; ++j)
154 {
155 p[j] = (uint8_t)Int(0, 7);
156 }
157 return ret;
158 }
159
160 static Buffer BufferX(size_t len)
161 {
162 Buffer ret{ len };
163 size_t X = len / 8;
164 size_t Y = len % 8;
165 for (size_t i = 0; i < X; ++i)
166 {
167 ((uint64_t*)ret.data())[i] = Integer<uint64_t>(0, (std::numeric_limits<uint64_t>::max)());
168 }
169 uint8_t* p = (uint8_t*)ret.data() + X * 8;
170 for (size_t j = 0; j < Y; ++j)
171 {
172 p[j] = (uint8_t)Int(0, 7);
173 }
174 return ret;
175 }
176
182 template <typename C>
183 static auto& Select(C& container)
184 {
185 assert(!container.empty());
186 size_t index = Integer<size_t>(0, container.size() - 1);
187 auto iter = container.begin();
188 advance(iter, index);
189 return *iter;
190 }
191
197 template <typename C>
198 static auto SelectErase(C& container)
199 {
200 assert(!container.empty());
201 size_t index = Integer<size_t>(0, container.size() - 1);
202 auto iter = container.begin();
203 advance(iter, index);
204 auto ret = *iter;
205 container.erase(iter);
206 return ret;
207 }
208
209 static Point ExPoint(const Point& p0, const Point& p1)
210 {
211 return { Int(p0[0], p1[0]), Int(p0[1], p1[1]) };
212 }
213 static PointU ExPointU(const PointU& p0, const PointU& p1)
214 {
215 return { Uint(p0[0], p1[0]), Uint(p0[1], p1[1]) };
216 }
218 {
219 float r = Random::Angle();
220 return { std::cos(r), std::sin(r)};
221 }
222
225 static bool Percent(int v)
226 {
227 return Int(0, 99) < v;
228 }
229
232 static float Angle(float min = 0, float max = 3.1415926f * 2.0f)
233 {
234 return Float(min, max);
235 }
236 static float Float(const std::array<float, 2>& mm)
237 {
238 return Float(mm[0], mm[1]);
239 }
240 static float Float(const dl::Array<float, 2>& mm)
241 {
242 return Float(mm[0], mm[1]);
243 }
244
245 static std::default_random_engine& GetEngine()
246 {
247 return _re;
248 }
249private:
250 static uint32_t _seed;
251 static std::default_random_engine _re;
252};
253}
随机数
static auto & Select(C &container)
从容器随机选择一个
static size_t SizeT(size_t min, size_t max)
返回[min,max]区间的 size_t型整数
static T Float(T min, T max)
返回[min,max)区间的 浮点数随机值
static PointU ExPointU(const PointU &p0, const PointU &p1)
static float Float(const dl::Array< float, 2 > &mm)
static T Integer(T min, T max)
返回[min,max]区间的 随机整数
static int Int(int min, int max)
返回[min,max]区间的 int型整数
static Point ExPoint(const Point &p0, const Point &p1)
static FloatType FloatNormalX(FloatType scale=1/3_f, FloatType min=-1, FloatType max=1)
返回标准正态分布的缩放值,且限制到指定值
static void SetSeed(uint32_t seed)
设置随机数种子
static Buffer BufferX(size_t len)
static std::default_random_engine & GetEngine()
一般不使用,传给标准库接口才使用
static float Float(const std::array< float, 2 > &mm)
static bool Bool()
static Position ExDirection()
static unsigned Uint(unsigned min, unsigned max)
返回[min,max]区间的 unsigned型整数
static T Normal(T u=0, T o=1)
返回期望为u,标准差为o的正态分布随机值
static bool Percent(int v)
返回百分比命中
static std::array< std::byte, N > Array()
static FloatType FloatNormal(FloatType u=0_f, FloatType o=1_f)
static float Angle(float min=0, float max=3.1415926f *2.0f)
返回角度(弧度制)
static auto SelectErase(C &container)
从容器随机选择一个,并且从容器移除
表示一段内存
日志系统
通用类型定义
Position2 Position
float Float
void log(std::string_view str, DL_SOURCE_LOCATION)
以当前设置输出
Point2U PointU
constexpr T clamp(const T &x, const T &a, const T &b)
限制数值到 [a, b]
Point2 Point