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
17
18namespace dl
19{
21class Random
22{
23 using FloatType = Float;
24public:
29 static void SetSeed(uint32_t seed)
30 {
31 if (seed == 0)
32 {
33 std::random_device rd;
34 _re.seed(_seed = rd());
35 }
36 else
37 _re.seed(_seed = seed);
38 }
39
43 template <typename T>
44 static T Float(T min, T max)
45 {
46 //参数检查
47 if (min > max)
48 {
49 log("min > max!");
50 std::swap(min, max);
51 }
52 static std::uniform_real_distribution<T> dist_double;
53 //设定区间
54 using D = std::decay_t<decltype(dist_double)>;
55 using P = typename D::param_type;
56 dist_double.param(P{min, max});
57 return dist_double(_re);
58 }
59
64 template <typename T>
65 static T Integer(T min, T max)
66 {
67 //参数检查
68 if (min > max)
69 {
70 log("min > max!");
71 std::swap(min, max);
72 }
73 static std::uniform_int_distribution<T> dist_int;
74 //设定区间
75 using D = std::decay_t<decltype(dist_int)>;
76 using P = typename D::param_type;
77 dist_int.param(P{min, max});
78 return dist_int(_re);
79 }
80
85 template <typename T>
86 static T Normal(T u = 0, T o = 1)
87 {
88 static std::normal_distribution<T> dist;
89 //设定区间
90 using D = std::decay_t<decltype(dist)>;
91 using P = typename D::param_type;
92 dist.param(P{ u, o });
93 return dist(_re);
94 }
95
96 static FloatType FloatNormal(FloatType u = 0_f, FloatType o = 1_f)
97 {
98 return Normal(u, o);
99 }
100
103 static FloatType FloatNormalX(
104 FloatType scale = 1/3_f,
105 FloatType min = -1, FloatType max = 1)
106 {
107#if DL_PLATFORM == DL_PLATFORM_LINUX
108 return dl::clamp(FloatNormal() * scale, min, max);
109#else
110 return std::clamp(FloatNormal() * scale, min, max);
111#endif
112 }
113
114
115 static bool Bool()
116 {
117 return Integer(0, 1) == 1;
118 }
119
123 static int Int(int min, int max)
124 {
125 return Integer(min, max);
126 }
127
130 static unsigned Uint(unsigned min, unsigned max)
131 {
132 return Integer(min, max);
133 }
134
137 static size_t SizeT(size_t min, size_t max)
138 {
139 return Integer(min, max);
140 }
141
147 template <typename C>
148 static auto& Select(C& container)
149 {
150 assert(!container.empty());
151 size_t index = Integer<size_t>(0, container.size() - 1);
152 auto iter = container.begin();
153 advance(iter, index);
154 return *iter;
155 }
156
162 template <typename C>
163 static auto SelectErase(C& container)
164 {
165 assert(!container.empty());
166 size_t index = Integer<size_t>(0, container.size() - 1);
167 auto iter = container.begin();
168 advance(iter, index);
169 auto ret = *iter;
170 container.erase(iter);
171 return ret;
172 }
173
174 static Point ExPoint(const Point& p0, const Point& p1)
175 {
176 return { Int(p0[0], p1[0]), Int(p0[1], p1[1]) };
177 }
178 static PointU ExPointU(const PointU& p0, const PointU& p1)
179 {
180 return { Uint(p0[0], p1[0]), Uint(p0[1], p1[1]) };
181 }
183 {
184 float r = Random::Angle();
185 return { std::cos(r), std::sin(r)};
186 }
187
190 static bool Percent(int v)
191 {
192 return Int(0, 99) < 50;
193 }
194
197 static float Angle(float min = 0, float max = 3.1415926f * 2.0f)
198 {
199 return Float(min, max);
200 }
201
203 static std::default_random_engine& GetEngine()
204 {
205 return _re;
206 }
207private:
208 static uint32_t _seed;
209 static std::default_random_engine _re;
210};
211}
随机数
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 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 std::default_random_engine & GetEngine()
一般不使用,传给标准库接口才使用
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 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
Point2U PointU
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出
constexpr T clamp(const T &x, const T &a, const T &b)
限制数值到 [a, b]
Point2 Point