伯努利分佈在資料結構中
伯努利分佈是一種離散分佈,有兩個可能的結果,分別標記為 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
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP