Bernoulli distribution (Bernoulli distribution) is the probability of judging whether something is happening or not happening;
Given the parameter p, the probability value can be modified, the occurrence probability (true) is P, and the 1-p probability (false) is the same;
Random library, providing distributed object bernoulli_distribution, output bool value, occurrence is true, not occurred as false;
Bernoulli distribution, the probability of 0.5, can be equal to the probability output a two-dollar event, such as the sequence;
Note: The engine and the distribution object, declared outside the function, will produce a different value each time it is invoked, but it is fixed, and can be defined by a different engine, using the Times (0);
The code is as follows:
#include <iostream> #include <vector> #include <string> #include <random> #include &
Lt;algorithm> #include <cmath> using namespace std;
BOOL Play (BOOL i) {std::d efault_random_engine E; Std::bernoulli_distribution b (0.6);
Win probability greater bool win = B (e);
if (a)//We win the probability of Big return win;
else return!win;
int main () {std::string resp;
std::d efault_random_engine E;
Std::bernoulli_distribution b; do {bool = B (e);//Bernoulli producer Std::cout << (a) "We go Before": "I get to go" << Std::endl; Judge successively hand Std::cout << (play (a)?
"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; }
Output:
We go
to the congrats, you won play
again? Enter ' yes ' or ' no '
yes
We go
to Congrats, your won play
again? Enter ' yes ' or ' no '
yes
We go
to Congrats, your won play
again? Enter ' yes ' or ' no '
Author: csdn Blog spike_king
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/