命名空间
变体
操作

std::linear_congruential_engine

来自 cppreference.cn
< cpp‎ | 数值‎ | 随机
 
 
 
 
 
定义于头文件 <random>
template<

    class UIntType,
    UIntType a,
    UIntType c,
    UIntType m

> class linear_congruential_engine;
(C++11 起)

linear_congruential_engine 是一个基于线性同余生成器(LCG)的随机数引擎。

目录

[编辑] 模板参数

UIntType - 生成器生成的返回类型。如果它不是 unsigned shortunsigned intunsigned longunsigned long long 之一,则效果未定义。
a - 乘数项
c - 增量项
m - 模数项

m 不为零时,如果 a >= mc >= mtrue,则程序是非良构的。

[编辑] 生成器属性

linear_congruential_engine 的状态大小1,每个状态由一个整数组成。

实际模数 m0 定义如下:

  • 如果 m 不为零,则 m0m
  • 如果 m 为零,则 m0std::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,
                                48271, 0, 2147483647>
较新的“最小标准”,1993 年由 Park、Miller 和 Stockmeyer 推荐[编辑]

[编辑] 嵌套类型

类型 定义
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) [编辑]
对伪随机数引擎执行流输入和输出
(function template) [编辑]

[编辑] 示例