std::linear_congruential_engine
来自 cppreference.cn
定义于头文件 <random> |
||
template< class UIntType, |
(C++11 起) | |
linear_congruential_engine
是一个基于线性同余生成器(LCG)的随机数引擎。
目录 |
[编辑] 模板参数
UIntType | - | 生成器生成的返回类型。如果它不是 unsigned short、unsigned int、unsigned long 或 unsigned long long 之一,则效果未定义。 |
a | - | 乘数项 |
c | - | 增量项 |
m | - | 模数项 |
当 m 不为零时,如果 a >= m 或 c >= m 为 true,则程序是非良构的。
[编辑] 生成器属性
linear_congruential_engine
的状态大小为 1,每个状态由一个整数组成。
实际模数 m0 定义如下:
- 如果 m 不为零,则 m0 是 m。
- 如果 m 为零,则 m0 是 std::numeric_limits<result_type>::max() 加 1 的值(这意味着 m0 不必能表示为
result_type
)。
linear_congruential_engine
的转换算法为 TA(xi) = (a·xi+c) mod m0。
linear_congruential_engine
的生成算法为 GA(xi) = (a·xi+c) mod m0。
用当前状态生成的伪随机数也是后续状态。
[编辑] 预定义特化
以下特化定义了带有两组常用参数的随机数引擎:
定义于头文件
<random> | |
类型 | 定义 |
minstd_rand0 (C++11) |
std::linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647>于 1969 年由 Lewis、Goodman 和 Miller 发现,1988 年由 Park 和 Miller 采纳为“最小标准” |
minstd_rand (C++11) |
std::linear_congruential_engine<std::uint_fast32_t, |
[编辑] 嵌套类型
类型 | 定义 |
result_type
|
UIntType
|
[编辑] 数据成员
constexpr UIntType multiplier [静态] |
a (public static 成员常量) |
constexpr UIntType increment [静态] |
c (public static 成员常量) |
constexpr UIntType modulus [静态] |
m (public static 成员常量) |
constexpr UIntType default_seed [静态] |
1u (public static 成员常量) |
[编辑] 成员函数
构造与播种 | |
构造引擎 (public member function) | |
设置引擎的当前状态 (public member function) | |
生成 | |
推进引擎状态并返回生成的值 (public member function) | |
将引擎的状态推进指定量 (public member function) | |
特性 | |
[静态] |
获取输出范围中的最小可能值 (public static member function) |
[静态] |
获取输出范围中的最大可能值 (public static member function) |
[编辑] 非成员函数
(C++11起)(C++11起)(C++20中移除) |
比较两个伪随机数引擎的内部状态 (function) |
(C++11) |
对伪随机数引擎执行流输入和输出 (function template) |
[编辑] 示例
本节不完整 原因:无示例 |