c语言画伯努利分布图像,C++ - 随机生成器 伯努利分布(bernoulli distribution) 的 详解 及 代码...
隨機生成器 伯努利分布(bernoulli distribution) 的 詳解 及 代碼
本文地址:?http://blog.csdn.net/caroline_wendy/article/details/17335871
伯努利分布(bernoulli distribution), 是判斷某件事情發生或者未發生的概率;
給定參數p, 可以修改概率的值, 發生概率(true)是p,未發生概率(false)是1-p;
隨機庫, 提供分布對象bernoulli_distribution, 輸出bool值, 發生為true, 未發生為false;
伯努利分布, 概率為0.5時, 可以等概率輸出一個二元事件, 如先后順序;
注意: 引擎和分布對象, 聲明在函數外, 則每次調用, 都會產生不同的值, 但卻是固定的, 可以使用time(0), 定義不同的引擎;
代碼如下:
#include
#include
#include
#include
#include
#include
using namespace std;
bool play (bool first) {
std::default_random_engine e;
std::bernoulli_distribution b(0.6); //獲勝概率較大
bool win = b(e);
if(first) //我們獲勝的概率大
return win;
else
return !win;
}
int main()
{
std::string resp;
std::default_random_engine e;
std::bernoulli_distribution b;
do {
bool first = b(e); //伯努利生產器
std::cout << (first ? "We go first" : "You get to go first") << std::endl; //判斷先后手
std::cout << ((play(first)) ? "congrats, you won" : "sorry, you lost") << std::endl;
std::cout << "play again? Enter 'yes' or 'no' " << std::endl;
} while (std::cin >>resp && resp[0] == 'y');
return 0;
}
輸出:
We go first
congrats, you won
play again? Enter 'yes' or 'no'
yes
We go first
congrats, you won
play again? Enter 'yes' or 'no'
yes
We go first
congrats, you won
play again? Enter 'yes' or 'no'
總結
以上是生活随笔為你收集整理的c语言画伯努利分布图像,C++ - 随机生成器 伯努利分布(bernoulli distribution) 的 详解 及 代码...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 能给视频批量添加背景图片的小妙招
- 下一篇: C++使用FFmpeg库实现图片转视频