名稱空間
變體
操作

std::linear_congruential_engine

來自 cppreference.com
< cpp‎ | 數值‎ | 隨機
 
 
 
 
 
定義於標頭檔案 <random>
template<

    class UIntType,
    UIntType a,
    UIntType c,
    UIntType m

> class linear_congruential_engine;
(C++11 起)

linear_congruential_engine 是一個基於線性同餘生成器(LCG)的隨機數引擎。

目錄

[編輯] 模板引數

UIntType - 生成器生成的返回型別。如果它不是 unsigned shortunsigned intunsigned longunsigned long long 之一,則效果未定義。
a - 乘數項
c - 增量項
m - 模數項

m 不為零時,如果 a >= mc >= mtrue,則程式是非良構的。

[編輯] 生成器屬性

linear_congruential_engine 的狀態大小1,每個狀態由一個整陣列成。

實際模數 m0 定義如下:

  • 如果 m 不為零,則 m0m
  • 如果 m 為零,則 m0std::numeric_limits<result_type>::max()1 的值(這意味著 m0 不必能表示為 result_type)。

linear_congruential_engine轉換演算法TA(xi) = (a·xi+c) mod m0

linear_congruential_engine生成演算法GA(xi) = (a·xi+c) mod m0

用當前狀態生成的偽隨機數也是後續狀態。

[編輯] 預定義特化

以下特化定義了帶有兩組常用引數的隨機數引擎:

定義於標頭檔案 <random>
型別 定義
minstd_rand0 (C++11) std::linear_congruential_engine<std::uint_fast32_t,
                                16807, 0, 2147483647>

於 1969 年由 Lewis、Goodman 和 Miller 發現,1988 年由 Park 和 Miller 採納為“最小標準”[編輯]

minstd_rand (C++11)

std::linear_congruential_engine<std::uint_fast32_t,
                                48271, 0, 2147483647>
較新的“最小標準”,1993 年由 Park、Miller 和 Stockmeyer 推薦[編輯]

[編輯] 巢狀型別

型別 定義
result_type UIntType

[編輯] 資料成員

constexpr UIntType multiplier
[靜態]
a
(public static 成員常量)
constexpr UIntType increment
[靜態]
c
(public static 成員常量)
constexpr UIntType modulus
[靜態]
m
(public static 成員常量)
constexpr UIntType default_seed
[靜態]
1u
(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) [編輯]

[編輯] 示例