📄 随机数均匀性检验.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 uniform(int n,double p[],double a)
{
//函数作用是判断n个数是否满足均匀分布,否则满足则返回true,否则返回false;
//其中数组a[n]用来存储n个随机数,m表示将分成小区间数,xa为置信度。
int b[10] ; //用来存储每个落入每个区间的随机数的个数
//初始化
for(int i=0;i<10;i++)
b[i]=0;
for( i=0;i<n;i++)
{
int t=floor(p[i]*10); //第i个随机数落入第t+1个区间;
b[t]++;
}
float sum=0;
float np=n/10; // 的大小。
for(int j=0;j<10;j++)
{
sum+=(b[j]-np)*(b[j]-np);
// cout<<b[j]<<endl;
}
sum/=np;
// cout<<sum<<endl;
// 提前用数组存取在x[a][m-1]中
if(sum<=a)
return true;
else
return false;
}
void main()
{
int n=1000;
double p[1000];
rnds(p,n);
//置信水平都取0.95
if(uniform(n,p,16.919))
cout<<"pass the test"<<endl;
else
cout<<"fail to pass the test"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -