用c++编写一段程序,产生均值为0,方差为1的高斯随机数,求大神支招,急求
http://blog.chinaunix.net/uid-22666248-id-357093.html
int x;//x是你输入的值
int outPut=0;
outPut = exp(-x*x/2)/sqrt(2*3.14159);
//outPut 是输出的均值为0,方差为1的高斯函数值
把你随机得到的数赋值给x,就可以得到对应的高斯值outPut了。
你可以先生成100个随机数,然后把这些随机数当做x,得到outPut就可以了
#include
#include
double gaussrand()
{
static double V1, V2, S;
static int phase = 0;
double X;
if(phase == 0)
{
do
{
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
}
while(S > = 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
}
else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return X;
}
是不是你需要的
int x;//x是你输入的值
int outPut=0;
outPut = exp(-x*x/2)/sqrt(2*3.14159);
//outPut 是输出的均值为0,方差为1的高斯函数值
把你随机得到的数赋值给x,就可以得到对应的高斯值outPut了。