命名空间
变体
操作

std::mersenne_twister_engine

来自 cppreference.cn
< cpp‎ | 数值‎ | 随机
 
 
 
 
 
定义于头文件 <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 算法的随机数引擎。它在区间 [0, 2w
)
内生成高质量但非密码安全的 UIntType 类型的无符号整数随机数。

目录

[编辑] 模板参数

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

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

  • m 位于 [1n]
  • 以下表达式均为 true
  • 给定 (1u << w) - 1u 作为 w1,以下表达式均为 true
  • a <= w1
  • b <= w1
  • c <= w1
  • d <= w1
  • f <= w1

[编辑] 生成器属性

mersenne_twister_engine 状态的大小n,每个状态由一个 nresult_type 类型值的序列 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>
松本和西村在1998年提出的32位Mersenne Twister[编辑]

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>
松本和西村在2000年提出的64位Mersenne Twister[编辑]

[编辑] 嵌套类型

类型 定义
result_type UIntType

[编辑] 数据成员

constexpr size_t word_size
[静态]
w
(public static 成员常量)
constexpr size_t state_size
[静态]
n
(public static 成员常量)
constexpr size_t shift_size
[静态]
m
(public static 成员常量)
constexpr size_t mask_bits
[静态]
r
(public static 成员常量)
constexpr UIntType xor_mask
[静态]
a
(public static 成员常量)
constexpr size_t tempering_u
[静态]
u
(public static 成员常量)
constexpr UIntType tempering_d
[静态]
d
(public static 成员常量)
constexpr size_t tempering_s
[静态]
s
(public static 成员常量)
constexpr UIntType tempering_b
[静态]
b
(public static 成员常量)
constexpr size_t tempering_t
[静态]
t
(public static 成员常量)
constexpr UIntType tempering_c
[静态]
c
(public static 成员常量)
constexpr size_t tempering_l
[静态]
l
(public static 成员常量)
constexpr UIntType initialization_multiplier
[静态]
f
(public static 成员常量)
constexpr UIntType default_seed
[静态]
5489u
(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) [编辑]

[编辑] 示例