命名空间
变体
操作

std::mersenne_twister_engine

来自 cppreference.cn
< cpp‎ | numeric‎ | random
 
 
 
 
 
定义于头文件 <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;
(since C++11)

mersenne_twister_engine 是一个基于梅森旋转算法的随机数引擎。它生成高质量,但非密码学安全的,类型为 UIntType 的无符号整数随机数,范围在 [0, 2w
)
区间内。

目录

[编辑] 模板参数

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

如果违反以下任何限制,则程序是非良构的

  • m[1n] 范围内。
  • 以下表达式均为 true
  • 给定 (1u << w) - 1uw1,以下表达式均为 true
  • a <= w1
  • b <= w1
  • c <= w1
  • d <= w1
  • f <= w1

[编辑] 生成器属性

mersenne_twister_engine 的状态的大小n,每个状态由类型为 result_typen 个值的序列 X 组成。Xj 代表 X 的第 j mod n 个值(从 0 开始)。

给定以下按位运算符号

  • bitand,内建按位与
  • xor,内建按位异或
  • lshift,内建按位左移
  • rshift,内建按位右移

mersenne_twister_engine转移算法TA(xi))定义如下

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

mersenne_twister_engine生成算法GA(xi))定义如下

  1. z1Xi xor ((Xi rshift u) bitand d)
  2. z2Xi xor (((Xi lshift s) mod 2w
    ) bitand b)
  3. z3Xi xor (((Xi lshift t) mod 2w
    ) bitand c)
  4. z4z3 xor (z3 rshift l)
  5. 交付 z4 作为结果(即 GA(xi)=z4)。

[编辑] 预定义的特化

以下特化使用两个常用参数集定义了随机数引擎

定义于头文件 <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>
Matsumoto 和 Nishimura 于 1998 年提出的 32 位梅森旋转算法[编辑]

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>
Matsumoto 和 Nishimura 于 2000 年提出的 64 位梅森旋转算法[编辑]

[编辑] 嵌套类型

类型 定义
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)(removed in C++20)
比较两个伪随机数引擎的内部状态
(函数) [编辑]
对伪随机数引擎执行流输入和输出
(函数模板) [编辑]

[编辑] 示例