Runs Up Down Test is a general test in statistics which tests the independence of random sample. In this test, we count how much runs there are depending on whether a sequence is following a larger or smaller number. The higher the number of runs the higher the independence is. The lower the number of runs the lower the independence. The numbers are considered as not totally random for very low independence.

Let’s consider the random sequence below which is generated with C built-in function.

`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`

Here, 41 follows by a larger number and 67 follows by a smaller.

So, 41 is a up run.

Then, 67 and 34 both follows smaleer number.

So, 67, 34 is a down run.

Similarly we can define each of the run.

For N number of sample there can be at most N-1 run and at least 1 run.

An up run is a sequence of numbers each of which is succeeded by a larger number.

A down run is a sequence of numbers each of which is succeeded by a smaller number.

Here, is a counting process of Runs for another sample:

The formula to calculate the value Z or Randomness is also given. So, now we need to calculate it with C++.

Below line generates the first random number. We cannot put it in the loop. Because there need to have at least two random number to compare a run. So, we run the loop 1 times less than the total random numbers.

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

Then we used a loop to generate the sequence of random number.

```
for(int i=1;i<N;i++){
Random2 = rand()%Range;
if(Random2>Random1 && upflag == 0){
Run++;
upflag = 1;
}
if(Random2<Random1 && upflag == 1){
Run++;
upflag = 0;
}
Random1 = Random2;
}
```

Here, we have compared each number with the previous random number.

If the current number is greater than previous one than it is an UP run.

Otherwise, it is a DOWN run.

If the UP run is started already then the up flag is on so it won’t count a new run although it is a up run.

If the DOWN run is started already the up flag is off so it won’t count a new run although it is a down run.

After counting the run we will assign the current random number to previous random number and compare again similarly.

We will count the total number of runs by this way in a variable RUN.

After calculating the number of runs we will calculate the value of Z (Randomness) using above formula.

```
Mean = (2*N-1)/3;
SD = sqrt((16*N-29)/90);
Randomness = (Run-Mean)/SD;
```

Here, Mean is meu, SD is root of sigma and Randomness is Z_{0}.

If the randomness is between -Z to +Z then the randomness will be accepted. This fragment is below:

```
if(Randomness>-Z && Randomness<Z)cout<<"Randomness is Accepted!"<<endl;
else cout<<"Randomness is Rejected!"<<endl;
```

I have wrote the random numbers in a file for further necessary. It is best practice but not mandatory.

## Runs Up Down Code in C++

```
#include<bits/stdc++.h>
using namespace std;
int main(){
double N;
cout<<"Number of Sample: ";
cin>>N;
ofstream myfile;
myfile.open("RunsUpDownTest.txt");
int Random1, Random2, Run, Range, upflag;
upflag = 0;
Range = 100;
Run = 0;
Random1 = rand()%Range;
myfile<<Random1<<" ";
for(int i=1;i<N;i++){
Random2 = rand()%Range;
myfile<<Random2<<" ";
if(Random2>Random1 && upflag == 0){
Run++;
upflag = 1;
}
if(Random2<Random1 && upflag == 1){
Run++;
upflag = 0;
}
Random1 = Random2;
}
double Mean, SD, Randomness, Z;
Mean = (2*N-1)/3;
SD = sqrt((16*N-29)/90);
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: 50
Acceptance Region Z: 1.96
```

Sample Output:

`Randomness is Accepted!`

Note that, Runs Up Down test will be accepted in most of the cases of sequence generated with C built-in function. If the number of sample is too low then it can be rejected but that is a rare case.

Hope you got the point of RUNS UP DOWN TEST. Comment your feelings below.