Tuesday, November 17, 2009

Random Number test

Write a C/C++ / Excel Program to test the random number using

1. Frequency Test

A. Chi-square test: Accept N random numbers, level of significance a, number of intervals n, set of observed values and set of estimated values.

B. Kolmogrov –Smirnov Test. Accept n random numbers, number of observations n, the set of observations Ri, where 1<=i<=n.

2. Run Test : Accept n random numbers and level of significance a to reject or accept the given set of random numbers

A. Runs Up and Runs Down Test: Accept the total number of runs a, to find the mean and variance of a

B. Runs Below and Runs Above mean test: Accept the number of individual observation above and below the mean as n1 and n2, the total number of runs as B to fine the mean and variance for B

3. Auto Corelation Test: Accept n random numbers, level of significance a, the lag m, the starting number i, M such that i+(M+1)m<=n.

4. Gap Test: Accept n random numbers, levels of significance a, find the number of gaps for all digits occurring in the random number sequence.

SOURCE CODE

#include
#include
#include
#include

double min, max;
int minm(int n,int a[20]);
int upp[20],low[20],noofclass,freq[20];
int findr(int min,int max);
int findfreq(int noofclass, int n, int b[20], int upp[20],int low[20]);

void chisquare();
void Kols();
void clc();
void gap();

struct list
{
int count,t,t1;
float Mi,Ma,t2;
}
I[10];

void main()
{
int op, flag=1;
clrscr();
while(flag==1)
{
printf("\n1.Chisquare Test");
printf("\n2.Kolmogrov-smirnov test");
printf("\n3.combined linear congruential");
printf("\n4.Gap test");
printf("\n5.Exit");
printf("\n Enter the above option");
scanf("%d",&op);
if (op>7)
{
printf("give the correct option");
getch();
exit(0);
}
switch(op)
{
case 1: chisquare ();
break;
case 2: Kols();
break;
case 3: clc();
break;
case 4: gap();
break;
case 5:exit(0);
}
}
getch();
}
void chisquare()
{
int i,N,n,j;
float r[50],dif,ci,mi,ma,E,T=0,zo=0,zalp;
randomize();
printf("enter how many random numbers from 0 to 99\n\n");
scanf("%d",&N);
if(N<0)
{
printf("given value of N<0");
getch();
exit(0);
}
for(i=0;ir[i]=rand()%100/(float)100;
printf("%f",r[i]);
}
mi=r[0];
ma=r[0];
for(i=0;i{
if(mi>r[i])
ma=r[i];
}
printf("\nenter the num of interval");
scanf("%d",&n);
printf("\n enter the value of z(alpha,n-1)");
scanf("%f",&zalp);

E=N/n;
if(E<5)
{
printf("give another num of interval");
getch();
exit(0);
}

dif=ma-mi;
ci=dif/n;
I[0].Mi=mi;
I[0].Ma=mi+ci;
for(i=0;iI[i+1].Mi=I[j].Ma;
I[i+1].Ma=I[i+1].Mi+ci;
}
I[i].Ma=I[i].Ma+0.1;
for(j=0;jfor(i=0;i{
if ((I[i].Mi<=r[j])&&(r[j]{
I[i].count=I[i].count+1;
}
}
for(i=0;i{
I[i].t=I[i].count-E;
I[i].t1=I[i].t*I[i].t;
I[i].t2=I[i].t/(float)E;
T=T+I[i].t1;
zo=zo+I[i].t2;

}
printf("\n\ninterval");
printf("\tobservation");
printf("\tequally");
printf("\t\tO-E");
printf("o-e2/e");
for(i=0;i{
printf("\n%f%f",I[i].Mi,I[i].Ma);
printf("\t%d",I[i].count);
printf("t%f",E);
printf("\t%d",I[i].t);
printf("\t%d",I[i].t1);
printf("\t%f",I[i].t2);

}
printf("\n\nzo %f",zo);
if(zo{
printf("\n\n Null hypothesis of uniform distribution is not rejected");
}
else
{
printf("\n\n Null hypothesis of uniform distribution is rejected");
}

}

void Kols()
{
int i,j,n;
float r[25],temp,d1[25],d2[25],dp,dm,d;
printf("enter n random nubers from 0 to 99\n\n");
scanf("%d",&n);
if(n<0)
{
printf("given value of n<0");
getch();
exit(0);
}
randomize();
for(i=1;i<=n;i++)
{
r[i]=rand()%100/100.0;
printf("%f",r[i]);
}
/* for(i=1;i<=n;i++)
{
printf("%f",r[i]);
} */
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
if(r[i]>r[j])
{
temp=r[i];
r[i]=r[j];
r[j]=temp;
}
}
}
printf("\n\n the ascending order of random number\n\n");
for(i=1;i<=n;i++)
{
printf("%f",r[i]);
}
for(i=1;i<=n;i++)
{
d1[i]=(i/n)-r[i];
d2[i]=r[i]-((i-1)/n);
if(d1[i]<0)
{
d1[i]=0.0;
}
if(d2[i]<0)
{
d2[i]=0.0;
}
}
printf("\n\n");
for(i=1;i<=n;i++)
{
printf("%f",d1[i]);
}
for(i=1;i<=n;i++)
{
printf("%f",d2[i]);
}
dp=d1[1];
dm=d2[1];
for(i=1;i<=n;i++)
{
if(dp {
dp=d1[i];
}
if(dm {
dm=d2[i];
}
}
printf("\n\n The value of dplus is %f and dminus is %f",dp,dm);
if(dp>dm)
{
d=dp;
}
else
d=dm;
printf("\n the max of dp and dm is %f",d);
}
void clc()
{
int i,no_of_g,n,j,a[10];
double R[10],X[10],x[10][10],temp=0,m[10];
clrscr();
printf("\n enter the no of generator:");
scanf("%d",&no_of_g);
printf("\nenter the nos random num to be generated");
scanf("%d",&n);
if(n<0 && no_of_g<0)
{
printf("\neither n<0 or no_of_g<0");
getch();
exit(0);
}
for(i=0;i {
printf("enter the value of a,m,x[i][0]");
scanf("%d%lf%lf",&a[i],&m[i],&x[i][0]);
for(j=0;j {
x[i][j+1]=fmod(a[i]*x[i][j],m[i]);
}
}
printf("\nindividual values\n");
for(i=0;i {
for(j=0;j {
printf("%lf\t",x[j][i]);
}
printf("\n");
}
for(i=0;i {
for(j=1;j temp=temp+pow((-1),j-1)*x[j][i];
X[i]=fmod(temp,(m[0]-1));
printf("\nthe X[%d] is %lf",i,X[i]);

if(X[i]>0)
{
R[i]=(float)X[i]/(float)m[0];
}
else
if(X[i]==0)
{
R[i]=((float)m[0]-1.0)/(float)m[0];
}
printf("\nthe R[%d] is %lf",i,R[i]);
}

}


void gap()
{
int n,i,j,flag=0,ctr=0,gap[20],m=0;
double FX[20],N1[20];
float Relfreq[20],cumul[20],Dalpha;
int a[110];
clrscr();
printf("enter how many random numbers you want to generate\n\n");
scanf("%d",&n);
printf("\n Enter Dalpha:");
scanf("%f",&Dalpha);
randomize();
printf("\n");
for(i=0;i {
a[i]=random(10);
printf("%d\t",a[i]);
}
printf("\n");
for(i=0;i<10;i++)
{
flag=1;
ctr=0;
for(j=0;j {
if(a[j]==i)
{
if(flag==1)
{
flag=2;
}
else
{
gap[m]=ctr;
m++;
ctr=0;
}
}
else
{
if(flag==2)
{
ctr=ctr+1;
}
}
}
}
printf("NO OF GAPS\n\n");
for(i=0;i {
printf("%d\t",gap[i]);
}
minm(m,gap);
findr(min,max);
printf("\n\n");
findfreq(noofclass,m,gap,upp,low);
printf("\n\nGAPLEN\tFREQUENCY\tRELATIVEFREQ\tCUMULATIVEFREQ\tF(X)\t|F(X)-Sn(X)|\n");
for(i=0;i {
Relfreq[i]=((float)freq[i]/100.0);
if(i==0)
{
cumul[i]=Relfreq[i];
}
else
{
cumul[i]=cumul[i-1]+Relfreq[i];
}
FX[i]=1-pow(0.9,(double)(upp[i]+1));
N1[i]=fabs(FX[i]-cumul[i]);
printf("%d%d\t\t%d\t\t%.2f\t\t%.2f\t%.2f\t%.2f\n",low[i],upp[i],Relfreq[i],cumul[i],FX[i],N1[i]);
}
printf("\n\n");
noofclass=i;
max=N1[0];
for(i=0;i {
if(N1[i]>max)
{
max=N1[1];
}
}
if(max {
printf("\n %.2f%d NULL HYPOTHESIS IS NOT REJECTED",max,noofclass);
}
else
{
printf("\n%.2f%d NULL HYPOTHESIS IS REJECTED",max,i);
}
}
int minm(int n,int a[15])
{
int i,j,temp=0;
min=0;
max=0;
for(i=0;i {
for(j=i+1;j {
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
min=a[0];
max=a[n-1];
return min,max;
}
int findr(int min,int max)
{
int a,i;
if(max<=10)
{
a=1;
}
if(max>10&&max<20)
{
a=2;
}
if(max>20&&max<30)
{
a=3;
}
if(a>30)
{
a=5;
}
low[0]=0;
upp[0]=a;
noofclass=1;
for(i=1;i {
low[i]=upp[i-1]+1;
upp[i]=low[i]+a;
noofclass++;
if(upp[i]>=max)
{
break;
}
}
min=min;
return low[20],upp[20];
}
int findfreq(int noofclass,int n,int b[15],int upp[20],int low[20])
{
int i,j,ctr=0;
for(j=0;j {
// ctr=0;
for(i=0;i {
if(b[i]<=upp[j]&&b[i]>=low[j])
{
ctr=ctr+1;
}
}
freq[j]=ctr;
}
return freq[20];
}

No comments:

Post a Comment