伯努利分佈在資料結構中


伯努利分佈是一種離散分佈,有兩個可能的結果,分別標記為 x = 0 和 x = 1。其中 x = 1 表示成功,x = 0 表示失敗。成功的機率為 p,失敗的機率為 q(q = 1 – p)。因此

$$P\left( x\right)=\begin{cases}1-p\:for & x = 0\p\:for & x = 0\end{cases}$$

還可以寫成 −

$$P\left( x\right)=p^{n}\left(1-p\right)^{1-n}$$

示例

 實際演示

#include <iostream>
#include <random>
using namespace std;
int main(){
   const int nrolls=10000;
   default_random_engine generator;
   bernoulli_distribution distribution(0.7);
   int count=0; // count number of trues
   for (int i=0; i<nrolls; ++i)
      if (distribution(generator))
      count++;
   cout << "bernoulli_distribution (0.7) x 10000:" << endl;
   cout << "true: " << count << endl;
   cout << "false: " << nrolls-count << endl;
}

輸出

bernoulli_distribution (0.7) x 10000:
true:7024
false: 2976

更新時間:27-Aug-2019

296 次瀏覽

開啟您的 職業之旅

完成課程,獲得認證

入門
廣告
© . All rights reserved.