# C++ Program for Kolmogorov-Smirnov(KS) Test for Uniformity

One of the most important features of the random numbers generated in a simulation study is that the random numbers must be uniformly distributed. One of the tests that can be used to test the uniformity of the random number in simulation study is the ‘Kolmogorov-Smirnov(KS) Test for Uniformity’.

## Algorithm for KS test for Uniformity:

Step1: Define the Hypothesis for Uniformity
Ho: Ri~U[0,1] The numbers are uniformly distributed
H1: Ri~U[0,1] The numbers are not uniformly distributed.

Step2: Arrange the data in increasing order
R1<=R2<=R3<=…………….<=Rn

Step3: Compute D+ and D-:
D+=max{(i/N)-Ri}, 1<=i<=N
D-=max{(Ri-((i-1)/N)}, 1<=i<=N

Step4: Compute calculated D:
D= max(D+, D-):

Step5: Determine the for the given critical value i.e Dα (D alpha)

Step 6:Check the if condition as below:
if D>D alpha, then reject the null hypothesis Ho.
else, accept the null hypothesis.

## C++ Code of KS test for Uniformity

``````#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
class KS
{
private:
float numbers[20];
float D,tabulatedD;
float Dplusmax,Dminusmax;
float Dplus[20],Dminus[20];
float ratio[20],ratiominus[20];
int i,j,n;

public:
void getdata() //to get the random numbers
{
cout<<"How many numbers?:"<<endl;
cin>>n;
cout<<"Enter "<<n<<" numbers"<<endl;
for(i=0;i<n;i++)
{
cout<<"Enter "<<i+1<<" number:"<<endl;
cin>>numbers[i];
}
}
float BubbleSort() // arrange the number in increasing order
{
int i,j;
float temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(numbers[j]>numbers[j+1])
{
temp=numbers[j];
numbers[j]=numbers[j+1];
numbers[j+1]=temp;
}
}
}
cout<<"The numbers in ascending order is:"<<endl;
for(i=0;i<n;i++)
{
cout<<setprecision(2)<<numbers[i]<<" ";
}

}
void calculate() // find D+, D-
{
for(i=0;i<n;i++)
{
int j;
j=i+1;
ratio[i]=(float)j/n;
ratiominus[i]=(float)i/n;
Dplus[i]=ratio[i]-numbers[i];
Dminus[i]=numbers[i]-ratiominus[i];

}
}
void display() // display the tabulated format and find D
{
cout<<endl;
cout<<endl;
cout<<setw(10)<<"i";
for(i=1;i<=n;i++)
{
cout<<setw(10)<<i;

}
cout<<endl;
cout<<setw(10)<<"R(i)";
for(i=0;i<n;i++)
{
cout<<setw(10)<<numbers[i];
}
cout<<endl;
cout<<setw(10)<<"i/n";

for(i=0;i<n;i++)
{
cout<<setw(10)<<setprecision(2)<<ratio[i];
}
cout<<endl;
cout<<setw(10)<<"D+";
for(i=0;i<n;i++)
{
cout<<setw(10)<<setprecision(2)<<Dplus[i];
}
cout<<endl;
cout<<setw(10)<<"D-";
for(i=0;i<n;i++)
{
cout<<setw(10)<<setprecision(2)<<Dminus[i];
}
cout<<endl;
Dplusmax=Dplus[0];
Dminusmax=Dminus[0];
for(i=1;i<n;i++)
{

if(Dplus[i]>Dplusmax)
{
Dplusmax=Dplus[i];
}
if(Dminus[i]>Dminusmax)
{
Dminusmax=Dminus[i];
}
}
cout<<"D+ max: "<<Dplusmax<<endl;
cout<<"D- max: "<<Dminusmax<<endl;
cout<<"D =max("<<Dplusmax<<", "<<Dminusmax<<") =";
if(Dplusmax>Dminusmax)
{
D=Dplusmax;
}
else
{
D=Dminusmax;
}
cout<<D;
cout<<endl;

}
void conclusion() // asking tabulated D and comparing it with D(calculated)
{
cout<<"Enter the tabulated value:"<<endl;
cin>>tabulatedD;

if(D<tabulatedD)
{
cout<<"The test is accepted."<<endl;
}
else
{
cout<<"The test is rejected."<<endl;
}
}
};

int main() //main function
{
KS ks1; //object of KS class
ks1.getdata(); //function calls
ks1.BubbleSort();
ks1.calculate();
ks1.display();
ks1.conclusion();
getch();
return(0);
}``````

## Output:

Feel free to comment if you are confused in any of the steps.