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
是一個基於 Mersenne Twister 演算法的隨機數引擎。它在區間 [0, 2w
) 內生成高質量但非密碼安全的 UIntType
型別的無符號整數隨機數。
目錄 |
[編輯] 模板引數
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
- 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 組成。Xj 代表 X 的第 j mod n 個值(從0開始)。
給定以下位運算子號
mersenne_twister_engine
的轉換演算法(TA(xi))定義如下
- 將 Xi-n 的高 w - r 位與 Xi+1-n 的低 r 位連線起來,得到一個無符號整數值 Y。
- 設 y 為 a·(Y bitand 1),並將 Xi 設定為 Xi+m−n xor (Y rshift 1) xor y。
mersenne_twister_engine
的生成演算法(GA(xi))定義如下
- 設 z1 為 Xi xor ((Xi rshift u) bitand d)。
- 設 z2 為 Xi xor (((Xi lshift s) mod 2w
) bitand b)。 - 設 z3 為 Xi xor (((Xi lshift t) mod 2w
) bitand c)。 - 設 z4 為 z3 xor (z3 rshift l)。
- 返回 z4 作為結果(即 GA(xi)=z4)。
[編輯] 預定義特化
以下特化定義了帶有兩組常用引數的隨機數引擎:
定義於標頭檔案
<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 (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) |
(C++11) |
對偽隨機數引擎執行流輸入和輸出 (function template) |
[編輯] 示例
本節不完整 原因:無示例 |