C + +: Random number of normal distribution (norm distribution) detailed

Source: Internet
Author: User

The normal distribution (norm distribution), as an important distributing law, has a wide range of uses;

Note that the normal distribution contains two parameters, mean (mean) and standard deviation (standard deviation);

Random library (#include <random>), which contains normal distribution objects, NORM_DISTRIBUTION<>, can be used to generate normal distribution;

The code is as follows:

#include <iostream>  
#include <vector>  
#include <string>  
#include <random>  
#include <algorithm>  
#include <cmath>  
      
      
using namespace std;  
      
      
int main ()  
{  
    std::d efault_random_engine E;//engine  
    std::normal_distribution<double> N (4, 1.5);//mean value, Variance  
    std::vector<unsigned> Vals (9);  
    For (std::size_t i=0 i!= ++i) {  
        unsigned v = std::lround (n (e));//rounding-nearest integer  
        if (v < vals.size ())  
            + + VALS[V];  
    }  
    For (std::size_t j=0 J!= vals.size (); ++j)  
        std::cout << J << ":" << Vals[j] << Std::strin G (Vals[j], ' * ') << Std::endl;  
      
    int sum = std::accumulate (Vals.begin (), Vals.end (), 0);  
    Std::cout << "sum =" << sum << std::endl;  
    return 0;  
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Output:

0:3***  
1:8********  
2:20********************  
3:38**************************************  
4:58***  
5:42******************************************  
6:23***********************  
7:7*******  
8:1*  
sum = 200

Author: csdn Blog spike_king

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.