四种分布 随机数拟合C+MATLABWord文件下载.docx
《四种分布 随机数拟合C+MATLABWord文件下载.docx》由会员分享,可在线阅读,更多相关《四种分布 随机数拟合C+MATLABWord文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
doublegauss(doublemean,doublesigma,longint*s)
inti;
doublex,y;
for(x=0,i=0;
i<
12;
i++)
x+=junyun(0.0,1.0,s);
x=x-6.0;
y=mean+x*sigma;
returny;
intmain()
inti,j,num;
doublex,mean,sigma;
longints;
cout<
<
"
输入均值和方差的值"
endl;
cin>
>
mean>
sigma;
输入随机种子:
s;
输入生成随机数个数:
num;
ofstreamcout("
zhengtai.txt"
);
for(i=0;
{
x=gauss(mean,sigma,&
s);
cout<
x;
cout<
"
;
}
return0;
运行程序:
正态分布拟合图
2均匀分布
stdio.h>
math.h>
stdlib.h>
iostream.h>
conio.h>
time.h>
graphics.h"
floata,b,c;
intn,type;
floatnextGaussian(floata,floatb)
{
floatv0,v1,v2,s;
floatnextNextGaussian;
//inthaveNextNextGaussian=0;
do
{do
v1=2*(((float)rand()/RAND_MAX))-1;
//between-1.0and1.0
v2=2*(((float)rand()/RAND_MAX))-1;
s=v1*v1+v2*v2;
}while(s>
=1||s==0);
floatmultiplier=float(sqrt(-2*log(s)/s));
nextNextGaussian=v2*multiplier;
//haveNextNextGaussian=1;
v0=v1*multiplier;
//均值为0,方差为1的标准正态分布
}while(s>
returnv0*b+a;
//产生均值为a,方差为b的随机信号
floatAverageRandom(floatmin,floatmax)
floatc1;
c1=(((float)rand()/RAND_MAX))*(max-min)+min;
returnc1;
floatsl(intn,inttype,floata,floatb)
floata1,b1;
a1=a;
b1=b;
if(type==0)
c=AverageRandom(a1,b1);
//产生均匀分布U(a,b)
returnc;
elseif(type==1)
c=nextGaussian(a1,b1);
//产生高斯分布N(a,b)
voidmain()
{intdet;
int*count;
floatbefore,next;
float*xplot;
float*s;
FILE*outfile;
srand(time(0));
请输入产生的信号类型--0:
平均分布,1:
高斯分布:
cin>
type;
if(type==1)
{
请输入产生点的均值和方差:
cin>
a>
b;
}
else
{cout<
请输入产生点范围:
请输入产生点的个数:
n;
s=newfloat[n];
outfile=fopen("
output.txt"
"
w"
for(inti=0;
i++)
{c=sl(n,type,a,b);
fprintf(outfile,"
%f"
c);
s[i]=c;
cout<
请输入测试的精确度1--1000:
cin>
det;
count=newint[det];
xplot=newfloat[det];
FILE*outfile1;
FILE*outfile2;
outfile1=fopen("
count.txt"
outfile2=fopen("
xplot.txt"
if(type==0)//如果是均匀分布的话
{before=a;
next=a+(b-a)/det;
for(intk=0;
k<
k++)
{count[k]=0;
for(intj=0;
j<
j++)
{if(s[j]>
before&
&
s[j]<
next)
count[k]=count[k]+1;
xplot[k]=(before+next)/2;
before=before+(b-a)/det;
next=next+(b-a)/det;
}
else//如果是高斯分布的话
{a=a-3*b;
b=a+6*b;
before=a;
next=a+(b-a)/det;
{
if(s[j]>
均匀分布数据直方图
3泊松分布
doubleU_Random();
//均匀随机数0到1
intpossion(intLambda);
//泊松分布随机数
main()
doubleu;
floata;
intLambda;
//可以改为需要的目标平均值
inti;
intn;
float*s;
FILE*outfile;
printf("
Lambda=\n"
scanf("
%d"
&
Lambda);
srand((unsigned)time(NULL));
//种子
u=U_Random();
n=\n"
n);
s=newfloat[n];
outfile=fopen("
for(i=0;
i++)//产生n个
a=possion(Lambda);
fprintf(outfile,"
a);
s[i]=a;
delete[]s;
intpossion(intLambda)/*generatepoissonrd,withmean=Lamda*/
intk=0;
doublep=1.0;
doublel,u;
l=exp(-Lambda);
/*exp(-Lambda)isdicimalzero*/
//printf("
l=%.15lf\n"
l);
while(p>
=l)
p*=u;
k++;
returnk-1;
doubleU_Random()/*0to1rd*/
doublef;
f=(double)(rand()%1001);
returnf/1000.0;
泊松分布拟合图
4指数分布
doublejunyun(doublea,doubleb,longint*seed)//采取的方法是混合同余法其中D,C,M都是自定义的常数
doubleexponent(doublebeta,longint*s)
doubleu,x;
u=junyun(0.0,1.0,s);
x=-beta*log(u);
returnx;
doublex,beta;
输入均值μ:
beta;
zhishu.txt"
x=exponent(beta,&
x<
指数分布直方图
指数分布曲线图