📄 随机数独立性检验.cpp
字号:
#include <iostream>
#include <time.h>
#include<cmath>
using namespace std;
void rnds(double p[],int n)
{ //生成n个[0 1]间的均匀随机数。
int i,m;
unsigned long randSeed;
randSeed=time(0);//取系统时间为当前种子
double x0=randSeed+0.0;
double *r=&x0;
double s,u,v;
s=65536.0; u=25741.0; v=13849.0;
for (i=0; i<=n-1; i++)
{
*r=u*(*r)+v;
m=(int)(*r/s);
*r=*r-m*s;
p[i]=*r/s;
}
return;
}
bool Independence(int n,double p[],double a)
{
double c[10000];
double t[10000];
int j;
for(int i=0;i<n;i++)
{
c[i]=0;
t[i]=0;
}
for( i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
int k=(i+2+j)%n;
c[j]+=p[i]*p[k];
}
}
for( j=0;j<n;j++)
{
c[j]=c[j]/n;
t[j]=(c[j]-1/4)/sqrt(13/144/n);
//cout<<t[j]<<endl;
if(fabs(t[j])>a) return false;
}
return true;
}
void main()
{
int n=10000;
double p[10000];
rnds(p,n);
//置信水平都取0.95
if(Independence(n,p,1.65))
cout<<"pass the test"<<endl;
else
cout<<"fail to pass the test"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -