NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
random.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_NUMERIC_RANDOM_HPP__
2#define NEFORCE_CORE_NUMERIC_RANDOM_HPP__
3
10
13NEFORCE_BEGIN_NAMESPACE__
14
51
58class NEFORCE_API random_lcd {
59public:
61
62private:
63 static constexpr seed_type a = 1103515245;
64 static constexpr seed_type c = 12345;
65 static constexpr seed_type m = 1u << 31;
66
67 seed_type seed_;
68
69 uint32_t generate_32bit() noexcept {
70 seed_ = a * seed_ + c;
71 seed_ %= m;
72 return static_cast<uint32_t>(seed_);
73 }
74
75 uint64_t generate_64bit() noexcept {
76 uint64_t result = 0;
77 result = static_cast<uint64_t>(generate_32bit()) << 32;
78 result |= generate_32bit();
79 return result;
80 }
81
82 decltype(auto) generate(true_type) noexcept { return generate_32bit(); }
83
84 decltype(auto) generate(false_type) noexcept { return generate_64bit(); }
85
86public:
91 random_lcd() noexcept;
92
97 explicit random_lcd(const seed_type seed) noexcept :
98 seed_(seed) {}
99
106 template <typename T>
107 T next_int(T max) noexcept {
108 static_assert(is_integral_v<T>, "only integral types are supported");
109
110 if (max <= 0) {
111 return 0;
112 }
113 if (max == 1) {
114 return 0;
115 }
116
117 const uint64_t value = generate_64bit();
118 const uint64_t product = value * static_cast<uint64_t>(max);
119 return static_cast<T>(product >> 32);
120 }
121
129 template <typename T>
130 T next_int(T min, T max) noexcept {
131 if (min >= max) {
132 return min;
133 }
134 return min + this->next_int<T>(max - min);
135 }
136
142 template <typename T>
143 T next_int() noexcept {
144 static_assert(is_integral_v<T>, "only integral types are supported");
145 return static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
146 }
147
154 if (max <= 0) {
155 return 0;
156 }
157 if (max == 1) {
158 return 0;
159 }
160
161 const uint64_t value = generate_64bit();
162 return value % max;
163 }
164
169 uint64_t next_uint64() noexcept { return generate_64bit(); }
170
176 template <typename T>
177 T next_float() noexcept {
178 static_assert(is_floating_point_v<T>, "only floating point types are supported");
179 auto gen = static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
180 using IntT = decay_t<decltype(gen)>;
181 return gen / numeric_traits<IntT>::max();
182 }
183
191 template <typename T>
192 T next_float(T min, T max) noexcept {
193 if (min >= max) {
194 return min;
195 }
196 return min + (max - min) * next_float<T>();
197 }
198
205 template <typename T>
206 T next_float(T max) noexcept {
207 return this->next_float(static_cast<T>(0), max);
208 }
209};
210
211
219class NEFORCE_API random_mt {
220public:
222
223private:
224 static constexpr size_t n = 624;
225 static constexpr size_t m = 397;
226 static constexpr seed_type a = 0x9908b0df;
227 static constexpr seed_type u = 11;
228 static constexpr seed_type s = 7;
229 static constexpr seed_type b = 0x9d2c5680;
230 static constexpr seed_type t = 15;
231 static constexpr seed_type c = 0xefc60000;
232 static constexpr seed_type l = 18;
233
234 seed_type state_[n] = {};
235 size_t index_ = n;
236
237 void twist() noexcept;
238
239 seed_type generate_32bit() noexcept;
240 uint64_t generate_64bit() noexcept;
241
242 decltype(auto) generate(true_type) noexcept { return generate_32bit(); }
243
244 decltype(auto) generate(false_type) noexcept { return generate_64bit(); }
245
246public:
251 random_mt() noexcept;
252
257 explicit random_mt(const seed_type seed) noexcept { set_seed(seed); }
258
263 void set_seed(seed_type seed) noexcept;
264
271 template <typename T>
272 T next_int(T max) noexcept {
273 static_assert(is_integral_v<T>, "only integral types are supported");
274
275 if (max <= 0) {
276 return 0;
277 }
278 if (max == 1) {
279 return 0;
280 }
281
282 const uint64_t value = generate_64bit();
283 const uint64_t product = value * static_cast<uint64_t>(max);
284 return static_cast<T>(product >> 32);
285 }
286
294 template <typename T>
295 T next_int(T min, T max) noexcept {
296 if (min >= max) {
297 return min;
298 }
299 return min + this->next_int<T>(max - min);
300 }
301
307 template <typename T>
308 T next_int() noexcept {
309 static_assert(is_integral_v<T>, "only integral types are supported");
310 return static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
311 }
312
319 if (max <= 0) {
320 return 0;
321 }
322 if (max == 1) {
323 return 0;
324 }
325
326 const uint64_t value = generate_64bit();
327 return value % max;
328 }
329
334 uint64_t next_uint64() noexcept { return generate_64bit(); }
335
341 template <typename T>
342 T next_float() noexcept {
343 static_assert(is_floating_point_v<T>, "only floating point types are supported");
344 auto gen = static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
345 using IntT = decay_t<decltype(gen)>;
346 return gen / numeric_traits<IntT>::max();
347 }
348
356 template <typename T>
357 T next_float(T min, T max) noexcept {
358 if (min >= max) {
359 return min;
360 }
361 return min + (max - min) * next_float<T>();
362 }
363
370 template <typename T>
371 T next_float(T max) noexcept {
372 return this->next_float(static_cast<T>(0), max);
373 }
374};
375
376
384class NEFORCE_API secret {
385private:
386 static void get_random_bytes(byte_t* buffer, size_t length);
387
388 static uint32_t generate_32bit() {
389 uint32_t value;
390 get_random_bytes(reinterpret_cast<byte_t*>(&value), sizeof(value));
391 return value;
392 }
393
394 static uint64_t generate_64bit() {
395 uint64_t value;
396 get_random_bytes(reinterpret_cast<byte_t*>(&value), sizeof(value));
397 return value;
398 }
399
400 static decltype(auto) generate(true_type) { return generate_32bit(); }
401
402 static decltype(auto) generate(false_type) { return generate_64bit(); }
403
404public:
411 template <typename T>
412 static T next_int(T max) {
413 static_assert(is_integral_v<T>, "only integral types are supported");
414
415 if (max <= 0) {
416 return 0;
417 }
418 if (max == 1) {
419 return 0;
420 }
421
422 const uint64_t value = generate_64bit();
423 const uint64_t product = value * static_cast<uint64_t>(max);
424 return static_cast<T>(product >> 32);
425 }
426
434 template <typename T>
435 static T next_int(T min, T max) {
436 if (min >= max) {
437 return min;
438 }
439 return min + secret::next_int<T>(max - min);
440 }
441
447 template <typename T>
448 static T next_int() {
449 static_assert(is_integral_v<T>, "only integral types are supported");
450 return static_cast<T>(secret::generate(bool_constant<sizeof(T) <= 4>()));
451 }
452
459 if (max <= 0) {
460 return 0;
461 }
462 if (max == 1) {
463 return 0;
464 }
465
466 const uint64_t value = generate_64bit();
467 return value % max;
468 }
469
474 static uint64_t next_uint64() { return generate_64bit(); }
475
481 template <typename T>
482 static T next_float() {
483 static_assert(is_floating_point_v<T>, "only floating point types are supported");
484 auto gen = static_cast<T>(secret::generate(bool_constant<sizeof(T) <= 4>()));
485 using IntT = decay_t<decltype(gen)>;
486 return gen / numeric_traits<IntT>::max();
487 }
488
496 template <typename T>
497 static T next_float(T min, T max) {
498 static_assert(is_floating_point_v<T>, "only floating point types are supported");
499 if (min >= max) {
500 return min;
501 }
502 return min + (max - min) * next_float<T>();
503 }
504
511 template <typename T>
512 static T next_float(T max) {
513 static_assert(is_floating_point_v<T>, "only floating point types are supported");
514 return next_float(static_cast<T>(0), max);
515 }
516
521 static bool system_supported();
522};
523 // RandomGenerators
525
526NEFORCE_END_NAMESPACE__
527#endif // NEFORCE_CORE_NUMERIC_RANDOM_HPP__
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
uint64_t next_uint64(uint64_t max) noexcept
生成 [0, max) 范围内的随机 64 位整数
T next_int() noexcept
生成完整范围的随机整数
uint32_t seed_type
种子类型
T next_float(T min, T max) noexcept
生成 [min, max) 范围内的随机浮点数
T next_float(T max) noexcept
生成 [0, max) 范围内的随机浮点数
T next_float() noexcept
生成 [0, 1) 范围内的随机浮点数
uint64_t next_uint64() noexcept
生成完整范围的随机 64 位整数
random_lcd() noexcept
默认构造函数 默认使用当前时间戳值作为种子。
T next_int(T max) noexcept
生成 [0, max) 范围内的随机整数
T next_int(T min, T max) noexcept
生成 [min, max) 范围内的随机整数
T next_int(T max) noexcept
生成 [0, max) 范围内的随机整数
T next_int(T min, T max) noexcept
生成[min, max)范围内的随机整数
T next_float(T min, T max) noexcept
生成 [min, max) 范围内的随机浮点数
uint64_t next_uint64() noexcept
生成完整范围的随机 64 位整数
T next_float() noexcept
生成 [0, 1) 范围内的随机浮点数
uint64_t next_uint64(uint64_t max) noexcept
生成 [0, max) 范围内的随机 64 位整数
void set_seed(seed_type seed) noexcept
设置随机数种子
random_mt() noexcept
默认构造函数 默认使用当前时间戳值作为种子。
uint32_t seed_type
种子类型
T next_int() noexcept
生成完整范围的随机整数
T next_float(T max) noexcept
生成 [0, max) 范围内的随机浮点数
真随机数生成器
static T next_int(T min, T max)
生成 [min, max) 范围内的随机整数
static uint64_t next_uint64(uint64_t max)
生成 [0, max) 范围内的随机 64 位整数
static bool system_supported()
检查系统是否支持真随机数生成
static T next_float(T max)
生成 [0, max) 范围内的随机浮点数
static T next_int()
生成完整范围的随机整数
static T next_float(T min, T max)
生成 [min, max) 范围内的随机浮点数
static T next_int(T max)
生成 [0, max) 范围内的随机整数
static T next_float()
生成 [0, 1) 范围内的随机浮点数
static uint64_t next_uint64()
生成完整范围的随机 64 位整数
NEFORCE_INLINE17 constexpr bool is_floating_point_v
is_floating_point的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_integral_v
is_integral的便捷变量模板
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位无符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr void generate(Iterator first, Iterator last, Generator gen)
用生成器的值填充范围
typename decay< T >::type decay_t
decay的便捷别名
bool_constant< true > true_type
表示true的类型
bool_constant< false > false_type
表示false的类型
integral_constant< bool, Value > bool_constant
布尔常量包装器
数值特征
类型萃取