std::negative_binomial_distribution

出自cppreference.com
< cpp‎ | numeric‎ | random
 
 
 
 
 
在標頭 <random> 定義
template< class IntType = int >
class negative_binomial_distribution;
(C++11 起)

產生隨機非負整數值 i,其分布按照離散概率函數:

P(i|k,p) =

k + i − 1
i


· pk
· (1 − p)i

該值表示一系列獨立的是/否試驗在準確出現 k 次成功前的失敗次數(每次成功概率為 p)。

std::negative_binomial_distribution 滿足隨機數分布 (RandomNumberDistribution)

目錄

[編輯] 模板形參

IntType - 生成器所生成的結果類型。如果它不是 shortintlonglong longunsigned shortunsigned intunsigned longunsigned long long 之一,那麼效果未定義。

[編輯] 成員類型

成員類型 定義
result_type (C++11) IntType
param_type(C++11) 參數集的類型,見隨機數分布 (RandomNumberDistribution)

[編輯] 成員函數

構造新分布
(公開成員函數) [編輯]
(C++11)
重置分布的內部狀態
(公開成員函數) [編輯]
生成
生成分布中的下個隨機數
(公開成員函數) [編輯]
特徵
(C++11)
返回分布參數
(公開成員函數) [編輯]
(C++11)
獲取或設置隨機參數對象
(公開成員函數) [編輯]
(C++11)
返回潛在生成的最小值
(公開成員函數) [編輯]
(C++11)
返回潛在生成的最大值
(公開成員函數) [編輯]

[編輯] 非成員函數

(C++11)(C++11)(C++20 移除)
比較兩個分布對象
(函數) [編輯]
執行偽隨機數分布的流輸入和輸出
(函數模板) [編輯]

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    // Pat 挨家挨户卖饼干
    // 在每家,有 75% 的几率卖出一盒
    // 试问她在卖出 5 盒前要卖多少次?
    std::negative_binomial_distribution<> d(5, 0.75);
 
    std::map<int, int> hist;
    for (int n = 0; n != 10000; ++n)
        ++hist[d(gen)];
 
    for (auto [x, y] : hist)
        std::cout << std::hex << x << ' ' << std::string(y / 100, '*') << '\n';
}

可能的輸出:

0 ***********************
1 *****************************
2 **********************
3 *************
4 ******
5 ***
6 *
7
8
9
a
b

[編輯] 外部鏈接

Weisstein, Eric W. 「負二項分布。」來自 MathWorld--A Wolfram Web Resource。