Namespaces
Variants

std:: mersenne_twister_engine

From cppreference.net
定义于头文件 <random>
template <

class UIntType, std:: size_t w, std:: size_t n, std:: size_t m, std:: size_t r,
UIntType a, std:: size_t u, UIntType d, std:: size_t s,
UIntType b, std:: size_t t, UIntType c, std:: size_t l, UIntType f

> class mersenne_twister_engine ;
(C++11 起)

mersenne_twister_engine 是一种基于 Mersenne Twister 算法的随机数引擎。它能够生成高质量但非密码学安全的 UIntType 类型无符号整数随机数,其取值范围为 [0, 2 w
)

目录

模板参数

UIntType - 生成器产生的结果类型。如果该类型不是 unsigned short unsigned int unsigned long unsigned long long 之一,则效果未定义。
w - 决定引擎生成值范围的2的幂次
n - 递归次数
m - 中间字,用于定义状态的递归关系中的偏移量
r - 低位掩码的位数,也称为扭曲值
a - 条件异或掩码,即有理规范形式扭曲矩阵的系数
u, d, s, b, t, c, l - 位扰乱(调温)矩阵的第1至第7个分量
f - 初始化乘数

如果违反以下任一限制,则程序格式错误:

  • m 的取值范围是 [ 1 , n ]
  • 下列表达式均为 true
  • 给定 ( 1u << w ) - 1u w1 ,以下表达式均为 true
  • a <= w1
  • b <= w1
  • c <= w1
  • d <= w1
  • f <= w1
(注:根据要求,所有HTML标签、属性及 标签内的C++代码均未翻译,仅翻译了可译的文本部分。由于原文中除代码外无其他可译文本,故输出保持原样)

生成器属性

mersenne_twister_engine 的状态大小为 n ,每个状态由类型为 result_type n 个值组成的序列 X 构成。 X j 表示序列 X 中第 j mod n 个值(从 0 开始计数)。

给定以下位运算操作符表示法:

  • bitand ,内置 按位与 运算符。
  • xor ,内置 按位异或 运算符。
  • lshift ,内置 按位左移 运算符。
  • rshift ,内置 按位右移 运算符。

mersenne_twister_engine 状态转移算法 TA(x i ) )定义如下:

  1. X i-n 的高位 w - r 位与 X i+1-n 的低位 r 位拼接,得到一个无符号整数值 Y
  2. y a·(Y bitand 1) ,并将 X i 设为 X i+m−n xor (Y rshift 1) xor y

mersenne_twister_engine 生成算法 GA(x i ) )定义如下:

  1. z 1 等于 X i xor ((X i rshift u) bitand d)
  2. z 2 等于 X i xor (((X i lshift s) mod 2 w
    ) bitand b)
  3. z 3 等于 X i xor (((X i lshift t) mod 2 w
    ) bitand c)
  4. z 4 等于 z 3 xor (z 3 rshift l)
  5. z 4 作为结果输出(即 GA(x i )=z 4 )。

预定义特化

以下特化定义了随机数引擎,包含两种常用参数集:

定义于头文件 <random>
类型 定义
mt19937 (C++11)

std :: mersenne_twister_engine < std:: uint_fast32_t ,
32 , 624 , 397 , 31 ,
0x9908b0df , 11 ,
0xffffffff , 7 ,
0x9d2c5680 , 15 ,
0xefc60000 , 18 , 1812433253 >
松本与西村设计的32位梅森旋转算法,1998

mt19937_64 (C++11)

std :: mersenne_twister_engine < std:: uint_fast64_t ,
64 , 312 , 156 , 31 ,
0xb5026f5aa96619e9 , 29 ,
0x5555555555555555 , 17 ,
0x71d67fffeda60000 , 37 ,
0xfff7eee000000000 , 43 ,
6364136223846793005 >
松本与西村设计的64位梅森旋转算法,2000

嵌套类型

类型 定义
result_type UIntType

数据成员

constexpr size_t word_size
[static]
w
(公共静态成员常量)
constexpr size_t state_size
[static]
n
(公共静态成员常量)
constexpr size_t shift_size
[static]
m
(公共静态成员常量)
constexpr size_t mask_bits
[static]
r
(公共静态成员常量)
constexpr UIntType xor_mask
[static]
a
(公共静态成员常量)
constexpr size_t tempering_u
[static]
u
(公共静态成员常量)
constexpr UIntType tempering_d
[static]
d
(公共静态成员常量)
constexpr size_t tempering_s
[static]
s
(公共静态成员常量)
constexpr UIntType tempering_b
[static]
b
(公共静态成员常量)
constexpr size_t tempering_t
[static]
t
(公共静态成员常量)
constexpr UIntType tempering_c
[static]
c
(公共静态成员常量)
constexpr size_t tempering_l
[static]
l
(公共静态成员常量)
constexpr UIntType initialization_multiplier
[static]
f
(公共静态成员常量)
constexpr UIntType default_seed
[static]
5489u
(公共静态成员常量)

成员函数

构造与播种
构造引擎
(公开成员函数)
设置引擎的当前状态
(公开成员函数)
生成
推进引擎状态并返回生成值
(公开成员函数)
按指定量推进引擎状态
(公开成员函数)
特性
[static]
获取输出范围中的最小可能值
(公开静态成员函数)
[static]
获取输出范围中的最大可能值
(公开静态成员函数)

非成员函数

(C++11) (C++11) (removed in C++20)
比较两个伪随机数引擎的内部状态
(函数)
对伪随机数引擎执行流输入和输出操作
(函数模板)

示例