There is no standard deviation function C++, so you’d need to do write all the necessary functions yourself — Generate random numbers and calculate the standard deviation.
double stDev(const vector<double>& data) {
double mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size();
double sqSum = std::inner_product(data.begin(), data.end(), data.begin(), 0.0);
return std::sqrt(sqSum / data.size() - mean * mean);
}
int main() {
double x_mu = 25;
double x_sigma = 5;
size_t size = 1000;
std::normal_distribution<double> x_distribution(x_mu, x_sigma);
//generate random numbers and store them in a vector
vector<double> data(size);
std::random_device rd;
std::mt19937 gen(rd());
for(size_t i=0; i<size; i++) {
data[i] = x_distribution(gen);
}
double test_distribution = stDev(data);
cout << test_distribution << endl;
return 0;
}
Update:
To get mean, variance and standard deviation, you may create separate functions to do the calculations. One possible implementation would be:
double mean(const vector<double>& data) {
return std::accumulate(data.begin(), data.end(), 0.0) / data.size();
}
double variance(const vector<double>& data) {
double xBar = mean(data);
double sqSum = std::inner_product(data.begin(), data.end(), data.begin(), 0.0);
return sqSum / data.size() - xBar * xBar;
}
double stDev(const vector<double>& data) {
return std::sqrt(variance(data));
}
3
solved How to Calculate the sample mean, standard deviation, and variance in C++ from random distributed data and compare with original mean and sigma