📄 check.c
字号:
#include <stdio.h>
#include <math.h>
#define a 314159269
#define c 453806246
#define m 2147483648
#define N 1000
int seed1=1;
float myrand(void)
{
int i;
for (i=0;i<10;i++)
seed1 = (a * seed1 + c) % m;
return (float) seed1/m;
}
/*main function to check whether the genrated numbers is satiesfied*/
main()
{
int j,n[4]={0,0,0,0},K;
float sum,u,s_sqr,v1,X2,p, rand[N];
char flag;
printf ("*********************************************************************\n");
printf ("* This program will check whether the random generator is qualified *\n");
printf ("*********************************************************************\n\n");
printf ("The first 1000 random numbers: \n\n");
/*store the random nums in an array and print these nums out*/
sum =0;
for (j=0;j<N;j++) {
rand[j] = myrand();
sum+=rand[j];
printf("%.3f ", rand[j]);
if ((j+1)%10 == 0) printf("\n");
}
/*mean value check*/
u= sum / N;
v1= pow(12*N,0.5)*(u-0.5);
printf ( "\nu = %.3f",u);
if ( (abs(v1) < 1.96) ) printf ("\nMean Value ACCEPTED!\n");
else printf ("\nMean Value REJECTED!\n");
/*sigma_sqr check*/
sum=0;
for (j=0;j<N;j++){
sum += (rand[j]-u)*(rand[j]-u);
}
s_sqr= sum / (N-1);
printf ( "\nS2 = %.3f",s_sqr);
if ( (abs(s_sqr) < 1.96 )) printf ("\nSigma_sqr ACCEPTED!\n");
else printf ("\nSigma REJECTED!\n");
/* X2 check */
sum =0;
for (j=0;j<N;j++) {/*count the nums in each domain*/
if (rand[j]<=0.25) n[0]++;
else if (rand[j]<=0.5) n[1]++;
else if (rand[j]<=0.75) n[2]++;
else if (rand[j]<=1) n[3]++;
else printf("ERROR!\n");
}
for (j=0;j<4;j++) sum += (n[j] - N/4)*(n[j] - N/4);
X2 = sum * 4 /N;
if (X2 < 7.81473) printf ("\nX2 ACCEPTED!\n");
else printf ("\nX2 REJECTED!\n");
/* independence check */
flag = 0;
for (K=1;K<51;K++){/*for every shift the p should be accepted*/
sum = 0;
for (j=0;j<N-K;j++) sum += rand[j]*rand[j+K];
p = (sum/(N-K)-u*u) * pow(N-K,0.5) / s_sqr;
if(abs(p)>1.96) {
printf("\nWhen K = %d, p = %.3f OVERLOAD!",K,p);
flag =1;
}
}
if (flag == 0) printf ("\nIndependence ACCEPTED!\n");
else printf("\nIndependence REJECTED!\n");
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -