名稱空間
變體
操作

std::mersenne_twister_engine

來自 cppreference.com
< 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) [編輯]

[編輯] 示例