C ++中的正态(高斯)分布函数 [英] Normal(Gaussian) Distribution Function in C++
本文介绍了C ++中的正态(高斯)分布函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道一种具有50个数字的高斯分布的方法。我知道Boost库生成随机数。在我的情况下,我不需要随机,我需要50数字的正态分布。有什么办法吗?感谢。
I need to know a way to have Gaussian Distribution of 50 numbers. I know of the Boost library which generates random numbers. In my case i don't need random, i need the normal distribution of 50 numbers. Any way to have that? thanks.
推荐答案
从C ++ 11开始,在标准库中有一个正态(高斯)分布:
As of C++11 there is a normal (gaussian) distribution available in the standard library:
http://www.cplusplus.com/ reference / random / normal_distribution /
平均值和标准偏差在创建时作为参数传递。以上链接提供了一个很好的例子:
The mean value and standard deviation are passed as arguments when creating it. The link above provides a good example:
// normal_distribution
#include <iostream>
#include <random>
int main()
{
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0,2.0);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
double number = distribution(generator);
if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
}
std::cout << "normal_distribution (5.0,2.0):" << std::endl;
for (int i=0; i<10; ++i) {
std::cout << i << "-" << (i+1) << ": ";
std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
}
return 0;
}
这篇关于C ++中的正态(高斯)分布函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文