C + +: Random generator Bernoulli distribution (Bernoulli distribution) detailed

Source: Internet
Author: User
Tags bool generator

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.