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