Frequency Test
Let's say we have the following binary string.
It is obviously not random since there are no ones in the string. Therefore, we must check that there are roughly an equal number of zeros and ones in the string.
For a binary string of length , we would expect the number of 1s in the string to be .
- (we observed 20 zeros out of 20 bits)
- (we expected 10 zeros out of 20 bits)
- (we observed 0 ones out of 20 bits)
- (we expected 10 ones out of 20 bits)
We can calculate the with 1 degree of freedom as follows.
The p-value for a chi-squared of 20 with 1 degree of freedom is 0.0000, which is less than 0.01, indicating that this sequence of numbers was probably not randomly generated.
This test, known as the frequency test, is the most basic check whether a number was randomly generated. However, it fails on the following binary string that was obviously not randomly generated.
Notes
A z test can be used instead of a chi-squared test for the frequency test and will give the same result. However, the chi-squared test is much more flexible and can be used as a one-size-fits-all tool for all randomness tests.