std::mersenne_twister_engine
在标头 <random> 定义
|
||
template< class UIntType, std::size_t w, std::size_t n, std::size_t m, std::size_t r, |
(C++11 起) | |
mersenne_twister_engine
是基于梅森缠绕器算法的随机数生成器。它产生高质量但非密码学安全的,UIntType
类型的区间 [0, 2w
) 上的无符号整数随机数。
模板形参
UIntType | - | 生成器所生成的结果类型。如果它不是 unsigned short、unsigned int、unsigned long 或 unsigned long long 之一,那么效果未定义。 |
w | - | 确定引擎生成的值的范围的2的幂 |
n | - | 递推度 |
m | - | 中间词,即定义状态的递推关系中使用的偏移 |
r | - | 低位掩码的位数,也被称为缠绕值 |
a | - | 条件性异或掩码,即有理范式缠绕矩阵的系数 |
u, d, s, b, t, c, l | - | 位扰乱(调和)矩阵的第一个到第七个组分 |
f | - | 初始化乘数 |
如果违背了以下任何限制,那么程序非良构:
- m 在范围
[
1,
n]
中。 - 以下表达式都是 true:
- w >= 3
- w >= r
- w >= u
- w >= s
- w >= t
- w >= l
- w <= std::numeric_limits<UIntType>::digits
- 给定 (1u << w) - 1u 为 w1,以下表达式都是 true:
- a <= w1
- b <= w1
- c <= w1
- d <= w1
- f <= w1
生成器属性
mersenne_twister_engine
的状态的大小是 n,它包含一个包含了 n 个 result_type
值的序列 X。X
j 表示 X 的第 j mod n 个值(从0开始算起)。
给定以下位操作表示:
mersenne_twister_engine
的变换算法(TA(x
i))定义如下:
- 拼接 X
i-n 的高 w - r 位和 X
i+1-n 的低 r 位,得到无符号整数值 Y。 - 设 y 为 a·(Y bitand 1),并且将 X
i 设置为 X
i+m−n xor (Y rshift 1) xor y。
mersenne_twister_engine
的生成算法(GA(x
i))定义如下:
- 设 z
1 为 X
i xor ((X
i rshift u) bitand d)。 - 设 z
2 为 X
i xor (((X
i lshift s) mod 2w
) bitand b)。 - 设 z
3 为 X
i xor (((X
i lshift t) mod 2w
) bitand c)。 - 设 z
4 为 z
3 xor (z
3 rshift l)。 - 以 z
4 作为生成结果(即 GA(x
i)=z
4)。
预定义特化
下列特化定义了两种常用参数集的随机数引擎:
在标头
<random> 定义 | |
类型 | 定义 |
mt19937 (C++11)
|
std::mersenne_twister_engine<std::uint_fast32_t, |
mt19937_64 (C++11)
|
std::mersenne_twister_engine<std::uint_fast64_t, |
嵌套类型
类型 | 定义 |
result_type
|
UIntType
|
数据成员
constexpr size_t word_size [静态] |
w (公开静态成员常量) |
constexpr size_t state_size [静态] |
n (公开静态成员常量) |
constexpr size_t shift_size [静态] |
m (公开静态成员常量) |
constexpr size_t mask_bits [静态] |
r (公开静态成员常量) |
constexpr UIntType xor_mask [静态] |
a (公开静态成员常量) |
constexpr size_t tempering_u [静态] |
u (公开静态成员常量) |
constexpr UIntType tempering_d [静态] |
d (公开静态成员常量) |
constexpr size_t tempering_s [静态] |
s (公开静态成员常量) |
constexpr UIntType tempering_b [静态] |
b (公开静态成员常量) |
constexpr size_t tempering_t [静态] |
t (公开静态成员常量) |
constexpr UIntType tempering_c [静态] |
c (公开静态成员常量) |
constexpr size_t tempering_l [静态] |
l (公开静态成员常量) |
constexpr UIntType initialization_multiplier [静态] |
f (公开静态成员常量) |
constexpr UIntType default_seed [静态] |
5489u (公开静态成员常量) |
成员函数
构造与播种 | |
(C++11) |
构造引擎 (公开成员函数) |
(C++11) |
设置引擎的当前状态 (公开成员函数) |
生成 | |
(C++11) |
推进引擎状态并返回生成的值 (公开成员函数) |
(C++11) |
令引擎状态前进指定量 (公开成员函数) |
特征 | |
[静态] (C++11) |
获取输出范围中的最小可能值 (公开静态成员函数) |
[静态] (C++11) |
获取输出范围中的最大可能值 (公开静态成员函数) |
非成员函数
(C++11)(C++11)(C++20 中移除) |
比较两个伪随机数引擎的内部状态 (函数) |
(C++11) |
执行伪随机数引擎的流输入和输出 (函数模板) |
示例
本节未完成 原因:暂无示例 |