
Runs Above Below Test in C++ | Independence Test of Random Number
What is Runs Above Below Test?
Runs above below test is an statistical test which counts the number of runs above the mean and below the mean. If the number of runs above mean and below means are balanced along with having sufficient number of runs then Runs test is accepted.
Properties of Runs Above Below Test:
- The sequence above the mean is an above run.
- The sequence below the mean is a below run.
- If the number of runs is too low than sample runs above below test will be rejected.
- If the number of runs above mean and below mean are not balanced runs above below test will be rejected.
- The number of runs is the sum of above run and below run always.
- The maximum number of runs can be up to N.
- The minimum number of runs can be 1.
Sample Runs Above Below Test
To perform a sample Runs test we need some sample. Lets consider the sample with 30 numbers in range 1-50:
22 21 41 31 13 15 45 26 29 5 4 20 48 25 34 14 44 41 2 47 1 33 40 7 18 37 24 32 35 10
Here, the numbers are between range 1-50. So, the mean is = 25.5
First two numbers are below mean. So, 22 and 21 forms a below run.
Then, 41 and 31 forms an above run.
And similarly for others.

In the example above,
Number of sample N = 30
Number of Runs b = 19
Elements in above n1 = 15
Elements in below n2 = 15
So, from the formula above we can calculate the value of Randomness Z.
Here, we found Z = 1.3
After calculating Z we will compare it to the acceptance limit.
Runs Above Below Test in C++
To perform Runs above below test I have used the random numbers generated from C built-in function. In this case we will need the number of sample as well as the range.
First of all we will calculate the average by the line:
Average = (Range - 1)/2;
Then we have generated N random numbers and kept them in file for further use.
Random = rand()%Range; myfile<<Random<<" ";
We compared whether the number is above mean. If it is above mean count it in n1. If the run just started that means above flag is 0 (previous run was down run) count a new run.
if(Random>Average){ n1++; if(aboveflag == 0){ Run++; aboveflag = 1; } }
If the number is below the mean count it in n2. If the run is just started that means above flag is 1 (previous run was up run) count a new run. If it is the first number also count a run because the flag is defined 0 by default not by a below run.
if(Random<Average){ n2++; if(aboveflag == 1 || (aboveflag == 0 && i ==0)){ Run++; aboveflag = 0; } }
Then calculate the Randomness using the formula above:
Mean = (2*n1*n2/N) + 1/2; SD = sqrt(2*n1*n2*(2*n1*n2-N)/(N*N*(N-1))); Randomness = (Run-Mean)/SD;
Compare the randomness with acceptance level and show the output.
C++ Code for Runs Above Below Test
#include<bits/stdc++.h> using namespace std; int main(){ double N; cout<<"Number of Sample: "; cin>>N; ofstream myfile; myfile.open("RunsAboveBelowTest.txt"); double Average, Run, n1, n2; int aboveflag, Random, Range; Range = 100; aboveflag = 0; Average = (Range - 1)/2; Run = 0; n1 = 0; n2 = 0; for(int i=0;i<N;i++){ Random = rand()%Range; myfile<<Random<<" "; if(Random>Average){ n1++; if(aboveflag == 0){ Run++; aboveflag = 1; } } if(Random<Average){ n2++; if(aboveflag == 1 || (aboveflag == 0 && i ==0)){ Run++; aboveflag = 0; } } } double Mean, SD, Randomness, Z; Mean = (2*n1*n2/N) + 1/2; SD = sqrt(2*n1*n2*(2*n1*n2-N)/(N*N*(N-1))); Randomness = (Run-Mean)/SD; cout<<"Acceptance Region Z: "; cin>>Z; if(Randomness>-Z && Randomness<Z)cout<<"Randomness is Accepted!"<<endl; else cout<<"Randomness is Rejected!"<<endl; myfile.close(); return 0; }
Sample Input:
Number of Sample: 100
Acceptance Region Z: 1.96
Sample Output:
Randomness is Accepted!
Note that, built-in rand() function in C generated sequences are accepted almost each case. If you want to have a rejected independence you should use input from outside.
Hope you got everything in Runs Above Below Test.