名稱空間
變體
操作

std::gamma_distribution

來自 cppreference.com
< cpp‎ | 數值‎ | 隨機
 
 
 
 
 
定義於標頭檔案 <random>
template< class RealType = double >
class gamma_distribution;
(C++11 起)

生成隨機正浮點值 x,其分佈遵循以下機率密度函式:

P(x|α,β) =
e-x/β
βα
· Γ(α)
· xα-1

其中 α 稱為形狀引數,β 稱為尺度引數。形狀引數有時用字母 k 表示,尺度引數有時用字母 θ 表示。

對於浮點 α,所獲得的值是 α 個獨立指數分佈隨機變數的和,每個變數的平均值為 β

std::gamma_distribution 滿足 RandomNumberDistribution

目錄

[編輯] 模板引數

RealType - 生成器生成的結果型別。如果這不是 floatdoublelong double 之一,則效果未定義。

[編輯] 成員型別

成員型別 定義
result_type (C++11) RealType
param_type (C++11) 引數集的型別,參見 RandomNumberDistribution

[編輯] 成員函式

構造新的分佈
(public member function) [編輯]
(C++11)
重置分佈的內部狀態
(public member function) [編輯]
生成
生成分佈中的下一個隨機數
(public member function) [編輯]
特性
(C++11)
返回分佈引數
(public member function) [編輯]
(C++11)
獲取或設定分佈引數物件
(public member function) [編輯]
(C++11)
返回可能生成的最小值
(public member function) [編輯]
(C++11)
返回可能生成的最大值
(public member function) [編輯]

[編輯] 非成員函式

(C++11起)(C++11起)(C++20中移除)
比較兩個分佈物件
(function) [編輯]
對偽隨機數分佈執行流輸入和輸出
(function template) [編輯]

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
 
    // A gamma distribution with alpha = 1, and beta = 2
    // approximates an exponential distribution.
    std::gamma_distribution<> d(1, 2);
 
    std::map<int, int> hist;
    for (int n = 0; n != 10000; ++n)
        ++hist[2 * d(gen)];
 
    for (auto const& [x, y] : hist)
        if (y / 100.0 > 0.5)
            std::cout << std::fixed << std::setprecision(1)
                      << x / 2.0 << '-' << (x + 1) / 2.0 << ' '
                      << std::string(y / 100, '*') << '\n';
}

可能的輸出

0.0-0.5 **********************
0.5-1.0 ****************
1.0-1.5 *************
1.5-2.0 **********
2.0-2.5 ********
2.5-3.0 ******
3.0-3.5 *****
3.5-4.0 ****
4.0-4.5 ***
4.5-5.0 **
5.0-5.5 **
5.5-6.0 *
6.0-6.5 *
6.5-7.0
7.0-7.5
7.5-8.0

[編輯] 外部連結

Weisstein, Eric W. "Gamma Distribution." From MathWorld — A Wolfram Web Resource.