?? 隨機數均勻性檢驗.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 + -