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
14NEFORCE_BEGIN_NAMESPACE__
15
65
66template <typename Generator>
67uint64_t lemire_bounded(Generator&& gen, const uint64_t max) noexcept(noexcept(gen())) {
68 uint64_t hi = 0;
69 uint64_t lo = _NEFORCE _umul128(gen(), max, &hi);
70
71 if (lo >= max) {
72 return hi;
73 }
74
75 const uint64_t threshold = (static_cast<uint64_t>(0) - max) % max;
76 while (lo < threshold) {
77 lo = _NEFORCE _umul128(gen(), max, &hi);
78 }
79 return hi;
80}
81
88class NEFORCE_API random_lcd {
89public:
91
92private:
93 static constexpr seed_type a = 1103515245;
94 static constexpr seed_type c = 12345;
95 static constexpr seed_type m = 1U << 31;
96
97 seed_type seed_;
98
99 uint32_t generate_32bit() noexcept {
100 seed_ = a * seed_ + c;
101 seed_ %= m;
102 return static_cast<uint32_t>(seed_);
103 }
104
105 uint64_t generate_64bit() noexcept {
106 const auto hi = static_cast<uint64_t>(generate_32bit()) << 32;
107 const auto lo = static_cast<uint64_t>(generate_32bit());
108 return hi | lo;
109 }
110
111 decltype(auto) generate(true_type /*unused*/) noexcept { return generate_32bit(); }
112 decltype(auto) generate(false_type /*unused*/) noexcept { return generate_64bit(); }
113
114public:
119 random_lcd() noexcept;
120
125 explicit random_lcd(const seed_type seed) noexcept :
126 seed_(seed) {}
127
134 template <typename T>
135 T next_int(T max) noexcept {
136 static_assert(is_integral_v<T>, "only integral types are supported");
137
138 if (max <= 0 || max == 1) {
139 return 0;
140 }
141 return static_cast<T>(
142 _NEFORCE lemire_bounded([this]() noexcept { return generate_64bit(); }, static_cast<uint64_t>(max)));
143 }
144
152 template <typename T>
153 T next_int(T min, T max) noexcept {
154 if (min >= max) {
155 return min;
156 }
157 return min + this->next_int<T>(max - min);
158 }
159
165 template <typename T>
166 T next_int() noexcept {
167 static_assert(is_integral_v<T>, "only integral types are supported");
168 return static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
169 }
170
177 if (max == 0 || max == 1) {
178 return 0;
179 }
180 return lemire_bounded([this]() noexcept { return generate_64bit(); }, max);
181 }
182
187 uint64_t next_uint64() noexcept { return generate_64bit(); }
188
194 template <typename T>
195 T next_float() noexcept {
196 static_assert(is_floating_point_v<T>, "only floating point types are supported");
197 auto gen = this->generate(bool_constant<sizeof(T) <= 4>());
198 using IntT = decay_t<decltype(gen)>;
199 return static_cast<T>(gen) / static_cast<T>(numeric_traits<IntT>::max());
200 }
201
209 template <typename T>
210 T next_float(T min, T max) noexcept {
211 if (min >= max) {
212 return min;
213 }
214 return min + (max - min) * next_float<T>();
215 }
216
223 template <typename T>
224 T next_float(T max) noexcept {
225 return this->next_float(static_cast<T>(0), max);
226 }
227};
228
229
237class NEFORCE_API random_mt {
238public:
240
241private:
242 static constexpr size_t n = 624;
243 static constexpr size_t m = 397;
244 static constexpr seed_type a = 0x9908b0df;
245 static constexpr seed_type u = 11;
246 static constexpr seed_type s = 7;
247 static constexpr seed_type b = 0x9d2c5680;
248 static constexpr seed_type t = 15;
249 static constexpr seed_type c = 0xefc60000;
250 static constexpr seed_type l = 18;
251
252 seed_type state_[n] = {};
253 size_t index_ = n;
254
255 void twist() noexcept;
256
257 seed_type generate_32bit() noexcept;
258 uint64_t generate_64bit() noexcept;
259
260 decltype(auto) generate(true_type /*unused*/) noexcept { return generate_32bit(); }
261 decltype(auto) generate(false_type /*unused*/) noexcept { return generate_64bit(); }
262
263public:
268 random_mt() noexcept;
269
274 explicit random_mt(const seed_type seed) noexcept { set_seed(seed); }
275
280 void set_seed(seed_type seed) noexcept;
281
288 template <typename T>
289 T next_int(T max) noexcept {
290 static_assert(is_integral_v<T>, "only integral types are supported");
291
292 if (max <= 0 || max == 1) {
293 return 0;
294 }
295 return static_cast<T>(
296 _NEFORCE lemire_bounded([this]() noexcept { return generate_64bit(); }, static_cast<uint64_t>(max)));
297 }
298
306 template <typename T>
307 T next_int(T min, T max) noexcept {
308 if (min >= max) {
309 return min;
310 }
311 return min + this->next_int<T>(max - min);
312 }
313
319 template <typename T>
320 T next_int() noexcept {
321 static_assert(is_integral_v<T>, "only integral types are supported");
322 return static_cast<T>(this->generate(bool_constant<sizeof(T) <= 4>()));
323 }
324
331 if (max == 0 || max == 1) {
332 return 0;
333 }
334 return _NEFORCE lemire_bounded([this]() noexcept { return generate_64bit(); }, max);
335 }
336
341 uint64_t next_uint64() noexcept { return generate_64bit(); }
342
348 template <typename T>
349 T next_float() noexcept {
350 static_assert(is_floating_point_v<T>, "only floating point types are supported");
351 auto gen = this->generate(bool_constant<sizeof(T) <= 4>());
352 using IntT = decay_t<decltype(gen)>;
353 return static_cast<T>(gen) / static_cast<T>(numeric_traits<IntT>::max());
354 }
355
363 template <typename T>
364 T next_float(T min, T max) noexcept {
365 if (min >= max) {
366 return min;
367 }
368 return min + (max - min) * next_float<T>();
369 }
370
377 template <typename T>
378 T next_float(T max) noexcept {
379 return this->next_float(static_cast<T>(0), max);
380 }
381};
382
383
391class NEFORCE_API secret {
392private:
393 static void get_random_bytes(byte_t* buffer, size_t length);
394
395 static uint32_t generate_32bit() {
396 uint32_t value = 0;
397 get_random_bytes(reinterpret_cast<byte_t*>(&value), sizeof(value));
398 return value;
399 }
400
401 static uint64_t generate_64bit() {
402 uint64_t value = 0;
403 get_random_bytes(reinterpret_cast<byte_t*>(&value), sizeof(value));
404 return value;
405 }
406
407 static decltype(auto) generate(true_type /*unused*/) { return generate_32bit(); }
408 static decltype(auto) generate(false_type /*unused*/) { return generate_64bit(); }
409
410public:
417 template <typename T>
418 static T next_int(T max) {
419 static_assert(is_integral_v<T>, "only integral types are supported");
420
421 if (max <= 0) {
422 return 0;
423 }
424 if (max == 1) {
425 return 0;
426 }
427 return static_cast<T>(
428 _NEFORCE lemire_bounded([]() { return secret::generate_64bit(); }, static_cast<uint64_t>(max)));
429 }
430
438 template <typename T>
439 static T next_int(T min, T max) {
440 if (min >= max) {
441 return min;
442 }
443 return min + secret::next_int<T>(max - min);
444 }
445
451 template <typename T>
452 static T next_int() {
453 static_assert(is_integral_v<T>, "only integral types are supported");
454 return static_cast<T>(secret::generate(bool_constant<sizeof(T) <= 4>()));
455 }
456
463 if (max == 0 || max == 1) {
464 return 0;
465 }
466 return lemire_bounded([]() { return secret::generate_64bit(); }, max);
467 }
468
473 static uint64_t next_uint64() { return generate_64bit(); }
474
480 template <typename T>
481 static T next_float() {
482 static_assert(is_floating_point_v<T>, "only floating point types are supported");
483 auto gen = secret::generate(bool_constant<sizeof(T) <= 4>());
484 using IntT = decay_t<decltype(gen)>;
485 return static_cast<T>(gen) / static_cast<T>(numeric_traits<IntT>::max());
486 }
487
495 template <typename T>
496 static T next_float(T min, T max) {
497 static_assert(is_floating_point_v<T>, "only floating point types are supported");
498 if (min >= max) {
499 return min;
500 }
501 return min + (max - min) * secret::next_float<T>();
502 }
503
510 template <typename T>
511 static T next_float(T max) {
512 static_assert(is_floating_point_v<T>, "only floating point types are supported");
513 return secret::next_float(static_cast<T>(0), max);
514 }
515
520 static bool system_supported();
521};
522 // RandomGenerators
524
525NEFORCE_END_NAMESPACE__
526#endif // NEFORCE_CORE_NUMERIC_RANDOM_HPP__
static 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 位整数
constexpr bool is_integral_v
is_integral的便捷变量模板
constexpr bool is_floating_point_v
is_floating_point的便捷变量模板
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 uint64_t _umul128(const uint64_t a, const uint64_t b, uint64_t *hi_out) noexcept
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
布尔常量包装器
MSVC内部函数替代实现
数值特征
类型萃取