How this calculation works
A run is a maximal consecutive block of the same category. The sequence 0,0,1,1,0 has three runs: one block of zeros, one of ones and a final zero block. This calculator tests whether the observed number of runs is unusually far from its conditional expectation when every ordering of the recorded category counts is equally likely. Both unusually few and unusually many runs can contribute to its declared two-sided p-value.
E[R] = 1 + 2n₀n₁/(n₀+n₁); count equally likely binary placements conditional on n₀ and n₁.A worked example
For 0,0,0,1,1,1 there are two observed runs. With three zeros and three ones, the expected run count is four. Of the twenty possible binary placements with those counts, two have two runs and two have six runs. Both extremes are at least two runs from the expectation, so this page’s two-sided conditional p-value is 4/20 = 0.2.
Good to know
Requires both categories and a total length from 2 through 200. The null is exchangeable binary ordering conditional on counts; it is not a complete test of every property of randomness. This is not the NIST SP800-22 large-bitstream runs implementation. If numerical measurements are categorized, threshold and tie handling must be chosen in advance. Sorted or rearranged input cannot diagnose the original ordering.
Reference: NIST — runs and ordering-based nonrandomness (opens in a new tab) ↗
Conditional counts isolate the ordering question
Three zeros and three ones can form a clustered sequence or a nearly alternating sequence. A frequency-only calculation cannot distinguish them because their category totals agree. Conditioning on those totals makes every placement of the ones among the available positions the reference outcome. Exact integer combinatorial counts produce the probability of each attainable run count without relying on a normal approximation.
The two-sided rule here uses absolute distance from the expected run count. Other conventions may double a smaller one-sided tail and need not give the same result for a discrete, asymmetric distribution. The complete distribution table makes the chosen rule auditable. Report the rule with the statistic instead of assuming that every result labelled two-sided uses the same set of extreme outcomes.
The expected value calculator summarizes a supplied probability distribution; the runs table here constructs an exact distribution from binary ordering counts.
An ordering diagnostic cannot certify a generator
Few runs can suggest clustering or persistence, while many runs can suggest excessive alternation. Those are specific ordering patterns, not an exhaustive description of dependence. A sequence can pass this test and still contain predictable structure, biased frequencies or other weaknesses. Cryptographic generator assessment requires a broader design and cannot be reduced to one short binary sample or one non-significant p-value.
Thresholding continuous measurements creates a new binary sequence whose interpretation depends on that threshold. Decide how ties are assigned and how missing observations are treated before testing. This calculator accepts explicit zeros and ones so that it never silently reclassifies your measurements. Do not sort values first: sorting can manufacture long runs even when the original acquisition order had no such pattern.
A few questions, answered
Why reject a sequence containing only ones?
With one category absent, there is no meaningful two-category ordering contrast. The reference conditional distribution is degenerate.
Must the counts of zeros and ones be equal?
No. The exact distribution conditions on the actual counts. Both categories must occur, but their totals can differ.