1、四种分布 随机数拟合C+MATLAB四种函数的拟合1正态分布#include/#include iomanip.h#include iomanip#include#include using namespace std;const int A=2045;const int C=1;const int M=1048576;/2的20次方double junyun(double a,double b,long int *seed)/采取的方法是混合同余法其中A,C,M都是自定义的常数 double t; *seed=A*(*seed)+C; *seed=(*seed)%M; t=(*seed)/(
2、double)M; t=a+(b-a)*t; return t;double gauss(double mean,double sigma,long int *s) int i; double x,y; for(x=0,i=0;i12;i+) x+=junyun(0.0,1.0,s); x=x-6.0; y=mean+x*sigma; return y;int main() int i,j,num; double x, mean,sigma; long int s; cout输入均值和方差的值meansigma; cout输入随机种子:s; cout输入生成随机数个数:num; ofstrea
3、m cout(zhengtai.txt); for(i=0;inum;i+) x=gauss(mean,sigma,&s); coutx ; cout ; return 0;运行程序:正态分布拟合图2均匀分布#include #include #include #include #include #include /#include graphics.hfloat a,b,c;int n,type;float nextGaussian(float a,float b) float v0,v1, v2, s; float nextNextGaussian; /int haveNextNextGa
4、ussian=0; do do v1 = 2 * (float)rand()/RAND_MAX)-1; / between -1.0 and 1.0 v2 = 2 * (float)rand()/RAND_MAX)-1; / between -1.0 and 1.0 s = v1 * v1 + v2 * v2; while (s = 1 | s = 0); float multiplier = float(sqrt(-2*log(s)/s); nextNextGaussian = v2 * multiplier; /haveNextNextGaussian = 1;v0=v1 * multip
5、lier; /均值为0,方差为1的标准正态分布 while(s = 1 | s = 0); return v0*b+a; /产生均值为a,方差为b的随机信号 float AverageRandom(float min,float max) float c1; c1=(float)rand()/RAND_MAX)*(max-min)+min; return c1;float sl(int n,int type,float a,float b) float a1,b1; a1=a;b1=b; if(type=0) c=AverageRandom(a1,b1); /产生均匀分布U(a,b) retu
6、rn c; else if(type=1) c=nextGaussian(a1,b1); /产生高斯分布N(a,b) return c; void main() int det;int *count;float before,next;float *xplot; float *s; FILE *outfile; srand(time(0); couttype; if(type=1) coutab; else coutab; coutn; s=new floatn; outfile = fopen(output.txt,w); for(int i=0;in;i+) c=sl(n,type,a,b
7、); fprintf(outfile,%f ,c); si=c; coutdet; count=new intdet; xplot=new floatdet;FILE *outfile1;FILE *outfile2;outfile1 = fopen(count.txt,w);outfile2 = fopen(xplot.txt,w);if(type=0) /如果是均匀分布的话before=a;next=a+(b-a)/det; for(int k=0;kdet;k+) countk=0; for(int j=0;jbefore&sjnext) countk=countk+1; xplotk=
8、(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; for(int k=0;kdet;k+) countk=0; for(int j=0;jbefore&sjnext) countk=countk+1; xplotk=(before+next)/2; before=before+(b-a)/det; next=next+(b-a)/det; 运行程序:均匀分布数据直方图3泊松分布#include #i
9、nclude #include #include #include double U_Random(); / 均匀随机数0到1int possion(int Lambda); / 泊松分布随机数 main()double u; float a; int Lambda; / 可以改为需要的目标平均值int i;int n;float *s;FILE *outfile;printf(Lambda=n);scanf(%d,&Lambda);srand( (unsigned)time( NULL ) ); / 种子u = U_Random();printf(n=n);scanf(%d,&n);s=ne
10、w floatn;outfile = fopen(output.txt,w);for (i=0;i=l)u = U_Random();p *= u;k+;return k-1;double U_Random() /* 0 to 1 rd */ double f; f = (double)(rand() % 1001); return f/1000.0;运行程序:泊松分布拟合图4指数分布#include/#include iomanip.h#include iomanip#include#include using namespace std;const int A=2045;const int
11、 C=1;const int M=1048576;/2的20次方double junyun(double a,double b,long int *seed)/采取的方法是混合同余法其中D,C,M都是自定义的常数 double t; *seed=A*(*seed)+C; *seed=(*seed)%M; t=(*seed)/(double)M; t=a+(b-a)*t; return t;double exponent(double beta,long int *s) double u,x; u=junyun(0.0,1.0,s); x=-beta*log(u); return x;int main() int i,j,num; double x,beta; long int s; cout输入均值:beta; cout输入随机种子:s; cout输入生成随机数个数:num; ofstream cout(zhishu.txt); for(i=0;inum;i+) x=exponent(beta,&s); coutx ; return 0;运行程序:指数分布直方图指数分布曲线图
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1