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
16namespace dl
17{
19class Random
20{
21 using FloatType = Float;
22public:
27 static void SetSeed(uint32_t seed)
28 {
29 if (seed == 0)
30 {
31 std::random_device rd;
32 _re.seed(_seed = rd());
33 }
34 else
35 _re.seed(_seed = seed);
36 }
37
41 template <typename T>
42 static T Float(T min, T max)
43 {
44 //参数检查
45 if (min > max)
46 {
47 log("min > max!");
48 std::swap(min, max);
49 }
50 static std::uniform_real_distribution<T> dist_double;
51 //设定区间
52 using D = std::decay_t<decltype(dist_double)>;
53 using P = typename D::param_type;
54 dist_double.param(P{min, max});
55 return dist_double(_re);
56 }
57
62 template <typename T>
63 static T Integer(T min, T max)
64 {
65 //参数检查
66 if (min > max)
67 {
68 log("min > max!");
69 std::swap(min, max);
70 }
71 static std::uniform_int_distribution<T> dist_int;
72 //设定区间
73 using D = std::decay_t<decltype(dist_int)>;
74 using P = typename D::param_type;
75 dist_int.param(P{min, max});
76 return dist_int(_re);
77 }
78
83 template <typename T>
84 static T Normal(T u = 0, T o = 1)
85 {
86 static std::normal_distribution<T> dist;
87 //设定区间
88 using D = std::decay_t<decltype(dist)>;
89 using P = typename D::param_type;
90 dist.param(P{ u, o });
91 return dist(_re);
92 }
93
94 static FloatType FloatNormal(FloatType u = 0_f, FloatType o = 1_f)
95 {
96 return Normal(u, o);
97 }
98
101 static FloatType FloatNormalX(
102 FloatType scale = 1/3_f,
103 FloatType min = -1, FloatType max = 1)
104 {
105 return std::clamp(FloatNormal() * scale, min, max);
106 }
107
108
109 static bool Bool()
110 {
111 return Integer(0, 1) == 1;
112 }
113
117 static int Int(int min, int max)
118 {
119 return Integer(min, max);
120 }
121
124 static unsigned Uint(unsigned min, unsigned max)
125 {
126 return Integer(min, max);
127 }
128
131 static size_t SizeT(size_t min, size_t max)
132 {
133 return Integer(min, max);
134 }
135
141 template <typename C>
142 static auto& Select(C& container)
143 {
144 assert(!container.empty());
145 size_t index = Integer<size_t>(0, container.size() - 1);
146 auto iter = container.begin();
147 advance(iter, index);
148 return *iter;
149 }
150
156 template <typename C>
157 static auto SelectErase(C& container)
158 {
159 assert(!container.empty());
160 size_t index = Integer<size_t>(0, container.size() - 1);
161 auto iter = container.begin();
162 advance(iter, index);
163 auto ret = *iter;
164 container.erase(iter);
165 return ret;
166 }
167
168 static Point ExPoint(const Point& p0, const Point& p1)
169 {
170 return { Int(p0[0], p1[0]), Int(p0[1], p1[1]) };
171 }
172 static PointU ExPointU(const PointU& p0, const PointU& p1)
173 {
174 return { Uint(p0[0], p1[0]), Uint(p0[1], p1[1]) };
175 }
177 {
178 float r = Random::Angle();
179 return { std::cos(r), std::sin(r)};
180 }
181
184 static bool Percent(int v)
185 {
186 return Int(0, 99) < 50;
187 }
188
191 static float Angle(float min = 0, float max = 3.1415926f * 2.0f)
192 {
193 return Float(min, max);
194 }
195
197 static std::default_random_engine& GetEngine()
198 {
199 return _re;
200 }
201private:
202 static uint32_t _seed;
203 static std::default_random_engine _re;
204};
205}
随机数
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())
以当前设置输出
Point2 Point