1#ifndef NEFORCE_CORE_NUMERIC_RANDOM_HPP__
2#define NEFORCE_CORE_NUMERIC_RANDOM_HPP__
14NEFORCE_BEGIN_NAMESPACE__
66template <
typename Generator>
76 while (lo < threshold) {
93 static constexpr seed_type a = 1103515245;
100 seed_ = a * seed_ + c;
102 return static_cast<uint32_t>(seed_);
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());
134 template <
typename T>
138 if (
max <= 0 ||
max == 1) {
141 return static_cast<T
>(
142 _NEFORCE lemire_bounded([
this]()
noexcept {
return generate_64bit(); },
static_cast<uint64_t>(
max)));
152 template <
typename T>
165 template <
typename T>
177 if (
max == 0 ||
max == 1) {
180 return lemire_bounded([
this]()
noexcept {
return generate_64bit(); },
max);
194 template <
typename T>
198 using IntT =
decay_t<
decltype(gen)>;
209 template <
typename T>
223 template <
typename T>
242 static constexpr size_t n = 624;
243 static constexpr size_t m = 397;
244 static constexpr seed_type a = 0x9908b0df;
247 static constexpr seed_type b = 0x9d2c5680;
249 static constexpr seed_type c = 0xefc60000;
255 void twist() noexcept;
257 seed_type generate_32bit() noexcept;
288 template <
typename T>
292 if (
max <= 0 ||
max == 1) {
295 return static_cast<T
>(
296 _NEFORCE lemire_bounded([
this]()
noexcept {
return generate_64bit(); },
static_cast<uint64_t>(
max)));
306 template <
typename T>
319 template <
typename T>
331 if (
max == 0 ||
max == 1) {
334 return _NEFORCE lemire_bounded([
this]()
noexcept {
return generate_64bit(); },
max);
348 template <
typename T>
352 using IntT =
decay_t<
decltype(gen)>;
363 template <
typename T>
377 template <
typename T>
393 static void get_random_bytes(
byte_t* buffer,
size_t length);
397 get_random_bytes(
reinterpret_cast<byte_t*
>(&value),
sizeof(value));
403 get_random_bytes(
reinterpret_cast<byte_t*
>(&value),
sizeof(value));
417 template <
typename T>
427 return static_cast<T
>(
428 _NEFORCE lemire_bounded([]() {
return secret::generate_64bit(); },
static_cast<uint64_t>(
max)));
438 template <
typename T>
451 template <
typename T>
454 return static_cast<T
>(secret::generate(
bool_constant<
sizeof(T) <= 4>()));
463 if (
max == 0 ||
max == 1) {
466 return lemire_bounded([]() {
return secret::generate_64bit(); },
max);
480 template <
typename T>
483 auto gen = secret::generate(
bool_constant<
sizeof(T) <= 4>());
484 using IntT =
decay_t<
decltype(gen)>;
495 template <
typename T>
510 template <
typename T>
525NEFORCE_END_NAMESPACE__
static constexpr T max() noexcept
获取类型的最大值
uint64_t next_uint64(uint64_t max) noexcept
生成 [0, max) 范围内的随机 64 位整数
T next_int() noexcept
生成完整范围的随机整数
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
默认构造函数 默认使用当前时间戳值作为种子。
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
布尔常量包装器