MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
random.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_NUMERIC_RANDOM_HPP__
2#define MSTL_CORE_NUMERIC_RANDOM_HPP__
3
11
14
20
27class MSTL_API random_lcd {
28public:
30
31private:
32 static constexpr seed_type a = 1103515245;
33 static constexpr seed_type c = 12345;
34 static constexpr seed_type m = 1u << 31;
35
36 seed_type seed_;
37
38public:
44
49 explicit random_lcd(const seed_type seed) : seed_(seed) {}
50
51 random_lcd(const random_lcd&) = default;
52 random_lcd& operator =(const random_lcd&) = default;
53 random_lcd(random_lcd&&) = default;
54 random_lcd& operator =(random_lcd&&) = default;
55
61 int next_int(const int max) {
62 seed_ = a * seed_ + c;
63 seed_ %= m;
64 return static_cast<int>(static_cast<double>(seed_) / m * max);
65 }
66
73 int next_int(const int min, const int max) {
74 return min + next_int(max - min);
75 }
76
81 int next_int() {
83 }
84
89 double next_double() {
90 seed_ = a * seed_ + c;
91 seed_ %= m;
92 return static_cast<double>(seed_) / m;
93 }
94
101 double next_double(const double min, const double max) {
102 return min + (max - min) * next_double();
103 }
104
110 double next_double(const double max) {
111 return next_double(0, max);
112 }
113};
114
115
123class MSTL_API random_mt {
124public:
126
127private:
128 static constexpr size_t n = 624;
129 static constexpr size_t m = 397;
130 static constexpr seed_type a = 0x9908b0df;
131 static constexpr seed_type u = 11;
132 static constexpr seed_type s = 7;
133 static constexpr seed_type b = 0x9d2c5680;
134 static constexpr seed_type t = 15;
135 static constexpr seed_type c = 0xefc60000;
136 static constexpr seed_type l = 18;
137
138 seed_type state_[n] = {};
139 size_t index_ = n;
140
144 void twist();
145
146public:
152
157 explicit random_mt(const seed_type seed) { set_seed(seed); }
158
159 random_mt(const random_mt& other) = default;
160 random_mt& operator =(const random_mt& other) = default;
161 random_mt(random_mt&& other) noexcept = default;
162 random_mt& operator =(random_mt&& other) noexcept = default;
163
168 void set_seed(seed_type seed);
169
175 int next_int(int max);
176
183 int next_int(const int min, const int max) {
184 if (min >= max) return min;
185 return min + next_int(max - min);
186 }
187
192 int next_int() {
194 }
195
200 double next_double();
201
208 double next_double(const double min, const double max) {
209 if (min >= max) return min;
210 return min + (max - min) * next_double();
211 }
212
218 double next_double(const double max) {
219 return next_double(0.0, max);
220 }
221};
222
223
231class MSTL_API secret {
232public:
240
246 static int32_t next_int(const int32_t max) { return next_int(0, max); }
247
252 static double next_double();
253
258 static bool is_supported();
259
260private:
268 static void get_random_bytes(byte_t* buffer, size_t length);
269};
270 // RandomGenerators
272
274#endif // MSTL_CORE_NUMERIC_RANDOM_HPP__
static MSTL_NODISCARD constexpr T max() noexcept
获取类型的最大值
double next_double(const double min, const double max)
生成[min, max)范围内的随机双精度浮点数
random_lcd(random_lcd &&)=default
移动构造函数
double next_double(const double max)
生成[0.0, max)范围内的随机双精度浮点数
int next_int()
生成[0, INT32_MAX)范围内的随机整数
random_lcd(const random_lcd &)=default
拷贝构造函数
uint32_t seed_type
种子类型
double next_double()
生成[0.0, 1.0)范围内的随机双精度浮点数
int next_int(const int max)
生成[0, max)范围内的随机整数
random_lcd()
默认构造函数 默认使用当前时间戳值作为种子。
random_lcd(const seed_type seed)
带种子构造函数
int next_int(const int min, const int max)
生成[min, max)范围内的随机整数
double next_double(const double min, const double max)
生成[min, max)范围内的随机双精度浮点数
int next_int(const int min, const int max)
生成[min, max)范围内的随机整数
void set_seed(seed_type seed)
设置随机数种子
int next_int()
生成[0, numeric_traits<int32_t>::max())范围内的随机整数
int next_int(int max)
生成[0, max)范围内的随机整数
uint32_t seed_type
种子类型
random_mt(const random_mt &other)=default
拷贝构造函数
double next_double(const double max)
生成[0.0, max)范围内的随机双精度浮点数
random_mt(const seed_type seed)
带种子构造函数
double next_double()
生成[0.0, 1.0)范围内的随机双精度浮点数
random_mt()
默认构造函数 默认使用当前时间戳值作为种子。
random_mt(random_mt &&other) noexcept=default
移动构造函数
真随机数生成器
static int32_t next_int(int32_t min, int32_t max)
生成[min, max)范围内的随机整数
static double next_double()
生成[0.0, 1.0)范围内的随机双精度浮点数
static int32_t next_int(const int32_t max)
生成[0, max)范围内的随机整数
static bool is_supported()
检查系统是否支持真随机数生成
constexpr const T & max(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(a, b)))
返回两个值中的较大者
constexpr const T & min(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(b, a)))
返回两个值中的较小者
unsigned char byte_t
字节类型,定义为无符号字符
unsigned int uint32_t
32位无符号整数类型
int int32_t
32位有符号整数类型
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL数值特征