Chi Square Test is a statistical test to justify the acceptability as uniformity of the random sample. That means evenly you have chosen the sample.

If you choose a sample of some people from the world then the sample will be evenly distributed if the sample belongs to each region as expected according to their population. Such as Asia is the most populated continent so you have to choose more sample from Asia. Similarly Antarctica is the least populated continent. So, there will be a little sample from there.

## Chi Square Test of Random Sample

Chi Square Test is an statistical test to find whether there is uniformity among the data sample. For, some specific rules the value of Chi Square Test is accepted for some values around 1.

To perform a Chi Square Test we need a data sample. We can generate the sample data using a random function in case of no sample on hand. If you have a sample to test then use that.

Here, I will test the uniformity of random samples for 100 numbers in range 0-99. The random numbers will be generated using built-in C function rand(). rand() function generates pseudo random numbers. So, for a specific range sample will be same each time.

So, our sample will be,

`41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 91 4 2 53 92 82 21 16 18 95 47 26 71 38 69 12 67 99 35 94 3 11 22 33 73 64 41 11 53 68 47 44 62 57 37 59 23 41 29 78 16 35 90 42 88 6 40 42 64 48 46 5 90 29 70 50 6 1 93 48 29 23 84 54 56 40 66 76 31 8 44 39 26 23 37 38 18 82 29 41`

We generated one by one random number and wrote that in a file. To generate the random numbers we used the line below.

```
RandomNumber = rand()%Range;
```

After generating random number we need to divide them into some range. Here, I have divided them into 5 range. They are:

```
00 - 19
20 - 39
40 - 59
60 - 79
80 -99
```

So, we need to observe how much numbers appear in each of these range. For example, 41 appears in third range, 67 appears in fourth range. Similarly we need to calculate for all random numbers.

There is a simple way to define in which range the number belong. It is to divide the random number by the range variation of each range. That is 20 in this case.

We can find the range variation by dividing total range with no of range.

If we divide by the range variation 20 the integer result will decide the index of the range starting from 0.

We find the range variation with this line:

```
RangeDiff = Range/RangeNumber;
```

So, we can count the random number as `Observed[RandomNumber/RangeDiff]`

increases by 1. That is done in this line.

```
Observed[RandomNumber/RangeDiff]++;
```

And then we write the random number in the file for further necessary.

```
myfile<<RandomNumber<<" ";
```

Now think about how much values there should be in each range. The expected number of sample in each range should be 20. Because there are 100 sample in 5 different range. We can calculate that by this line.

```
Expected[i]=N/RangeNumber;
```

where, i = 0, 1, 2, ………………. RangeNumber.

Now we need to calculate Chi Square. To calculate Chi Square you need to follow the steps below.

- Find the difference between expected and observed value in each range.
- Square the difference.
- Divide the square value with expected value.
- Sum up all the values.

You have got the Chi Square.

For the above numbers we can form a table like this.

Range | Expected | Observed | Expected – Observed | (Expected-Observed)^{2} | (Expected-Observed)^{2} /Expected |

00-19 | 20 | 17 | 3 | 9 | 0.45 |

20-39 | 20 | 25 | -5 | 25 | 1.25 |

40-59 | 20 | 25 | -5 | 25 | 1.25 |

60-79 | 20 | 18 | 2 | 4 | 0.2 |

80-99 | 20 | 15 | 5 | 25 | 1.25 |

Chi Square = | 4.4 |

As, we have divided the range into 5 small range. So, the degree of freedom is = 5 – 1 = 4

For, degree of freedom 4 the acceptance region in 99.5% probability is 0.207 to 14.860.

As, our value is between this so the test is accepted.

## Chi Square Test Code in C++

```
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cout<<"Number of Sample: ";
cin>>N;
ofstream myfile;
myfile.open("UniformityTestChiSquare.txt");
int RandomNumber, RangeNumber, Range, RangeDiff;
RangeNumber = 5;
Range = 100;
RangeDiff = Range/RangeNumber;
int Observed[RangeNumber];
for(int i=0;i<RangeNumber;i++)Observed[i]=0;
for(int i=0;i<N;i++){
RandomNumber = rand()%Range;
Observed[RandomNumber/RangeDiff]++;
myfile<<RandomNumber<<" ";
}
int Expected[RangeNumber];
double ChiSQ = 0;
for(int i=0;i<RangeNumber;i++){
Expected[i]=N/RangeNumber;
ChiSQ = ChiSQ + pow((Expected[i]-Observed[i]),2)/Expected[i];
}
if(ChiSQ>0.207 && ChiSQ<14.860)cout<<"Randomness is Accepted!"<<endl;
else cout<<"Randomness is Rejected!"<<endl;
myfile.close();
return 0;
}
```

Sample Input:

`Number of Sample: 100`

Sample Output:

`Randomness is Accepted!`

Note that, Random numbers generated with C built-in function will be accepted for almost any number of samples. If the number of sample is too low then it can be rejected.

Hope you have understood well. So, begin coding now.