The guide
A runs test counts streaks, and for short sequences the exact count beats the normal curve
Too few runs means streaks, too many means a sequence that switches too often. The exact distribution of runs can be counted outright, and for short sequences it disagrees with the normal approximation.
- 10.9048 expected runs for 13 and 8 symbols; equal sizes would give 11.5
- 203,490 equally likely arrangements of 13 and 8 symbols
- 0.0283 exact two-sided p for 6 runs in the 21-element example
- 0.0195 normal two-sided p for the same 21 elements, no correction
- 1/231 exact two-sided p for 12 alternating symbols; the normal says 0.0025
Read the full guide Fold the guide
What a run is and what the test asks
A run is a maximal non-empty stretch of adjacent equal elements. Take the 21-element sequence of plus and minus signs ++++---+++-++++++----. It has 6 runs, of lengths 4, 3, 3, 1, 6 and 4.
The Wald-Wolfowitz runs test, named after the statisticians Abraham Wald and Jacob Wolfowitz, checks a randomness hypothesis for a two-valued sequence. Wald was a Hungarian-American mathematician who founded sequential analysis. The hypothesis under test is that every element is drawn independently from the same distribution. Under that hypothesis, the number of runs should land near an expected value. A count that is significantly higher or lower than expected rejects independence. Fewer runs than expected means like elements gather in long streaks. More runs than expected means the sequence switches back and forth more often than chance would.
The idea is older than the test. In 1923 the hydrologist Robert Horton studied a 176-year rainfall record from Padua, in Italy, and noticed that wet and dry years tended to come in groups. He worked out the chance of a group of like events under independence and proposed a quick criterion based on the number of groups, as a screen before any heavier analysis. A handbook of the US National Institute of Standards and Technology gives a plain example with twenty coin tosses: H H T T H T H H H H T H H T T T T T H H. It holds 11 heads, 9 tails and 9 runs.
The most common slip is to count the switches instead of the runs. The 21-element sequence has 5 switches and 6 runs, so the number of runs is always the number of switches plus one. The same count can be written as half of N plus 1 minus the sum of the products of neighbouring elements, when the two kinds are coded as plus one and minus one.
Why the expected number depends on both group sizes
Let n1 and n2 be the counts of the two kinds and N their total. The expected number of runs is 2 n1 n2 / N + 1. The variance is 2 n1 n2 (2 n1 n2 - N) / (N^2 (N - 1)). The NIST handbook and the Wikipedia article give the same two formulas, and the handbook calls the first one the expected number of runs.
Work it through for the 21-element sequence, which has 13 plus signs and 8 minus signs. The expected count is 2 x 13 x 8 / 21 + 1 = 10.9048. The variance is 4.41, so the standard deviation is 2.1. The Wikipedia article adds a shortcut: the variance also equals (mean - 1)(mean - 2) / (N - 1), which gives the same 4.41. A tempting shortcut assumes the two kinds are equally common. Then the expected number becomes N / 2 + 1, which is 11.5 here. That is a difference of 0.6 runs, which is more than a quarter of the standard deviation, and equal counts overstate the expectation for every lopsided sequence.
The correct formulas do not assume equal probabilities for the two kinds. They assume only that the elements are independent and identically distributed.
Lopsided sequences show why. With 2 of one kind and 18 of the other, the expected count is 4.6 runs, and no arrangement can have more than 5 runs. A rule of thumb built on equal counts would expect about 11 runs, which is impossible. For 13 and 8 the possible counts run from 2 to 17.
How the exact distribution is counted
Under independence, every arrangement of the n1 plus n2 elements is equally likely. The number of arrangements is the number of ways to choose which positions hold the first kind, a count of choosing k items from n without order, which Python exposes as math.comb. For 13 and 8 it is 203,490.
Sorting those arrangements by their number of runs gives the exact distribution. For an even count of 2k runs the number of arrangements is 2 C(n1 - 1, k - 1) C(n2 - 1, k - 1). For an odd count of 2k + 1 runs it is C(n1 - 1, k) C(n2 - 1, k - 1) plus C(n1 - 1, k - 1) C(n2 - 1, k). These formulas are derived for the calculator, and a brute-force count over every arrangement agrees with them for several small cases.
For 13 and 8, the counts of arrangements with 2, 3, 4, 5 and 6 runs are 2, 19, 168, 714 and 2,772. Together that is 3,675 arrangements with 6 runs or fewer, a chance of 0.01806. Exactly 6 runs has chance 0.01362.
A two-sided exact p-value needs a rule, and more than one is in use. The calculator adds the chances of every count that is no more likely than the observed one. Here that picks up 6 runs or fewer, and also 16 and 17 runs, which are rare at the other end. The total is 5,754 of 203,490, which is 137/4845, or 0.0283. Doubling the smaller tail gives 0.0361 instead. Both are exact, and the page names which is which.
An exact test keeps the chance of a false alarm at the chosen level, while an approximate test only comes close. When the test statistic is discrete, an exact test can be conservative: the real rejection rate can sit below the nominal level, because only certain p-values can occur.
For long sequences the counts become large. With 5,000 elements the arrangement counts have hundreds of digits, far beyond what ordinary numbers hold, so the calculator uses BigInt, which represents integers too large for the number type. The work runs in a Web Worker, which runs a script in a background thread separate from the main thread, so typing stays smooth. Digits are grouped with Intl.NumberFormat.
When the normal approximation can be trusted
The classic shortcut standardises the count: z = (R - mean) / sd, then reads a normal table. For the 21-element sequence z is -2.3356 and the two-sided normal p-value is 0.0195. The exact rule says 0.0283. The normal curve claims stronger evidence than the counting supports.
The normal tail is worked out from the complementary error function, which DLMF defines as one minus the error function. A continuity correction adjusts for the fact that R takes only whole values: it moves the count one half toward the mean before standardising. With it, z is -2.0975 and p is 0.0359, which lands close to the doubled tail of 0.0361 but not close to 0.0283.
Short sequences are worse. Take twelve symbols, six of each, alternating perfectly. That is 12 runs, the most possible, and the expected count is 7. The exact chance of exactly 12 runs is 1/462, or 0.002165, so the exact two-sided p-value is 1/231, which is 0.0043. The plain normal approximation gives 0.0025, about two fifths too small. The corrected version gives 0.0064, half as large again as the truth.
The NIST handbook states a large-sample version of the test where both n1 and n2 exceed 10, compared with a standard normal. At the 5 percent level an absolute value of z above 1.96 indicates non-randomness. For small samples the handbook points to tables of critical values. The exact distribution does the same job for any sizes and needs no table.
Long streaks alone are not proof either. A truly random sequence of enough length is likely to contain stretches of repeats, and a sequence can look random overall while some stretches do not. The test asks about the total number of runs, not about any single streak.
What splitting numbers at the median does and does not test
A run needs two kinds of element, so a list of numbers must first be turned into two kinds. The NIST handbook codes values above the median as positive and values below it as negative. For an even count, the median is the mean of the two middle values.
Values equal to the median belong to neither side, so the calculator drops them and says how many it dropped. Take 24 numbers: 41 74 80 30 41 39 80 42 60 80 69 99 80 77 90 53 85 94 56 59 45 61 99 10. The median is 65, which splits them into 12 above and 12 below. Their order makes 11 runs, against 13 expected. The exact chance of 11 runs or fewer is 0.2632 and the exact two-sided p-value is 0.5264, so nothing looks unusual.
The split throws away how far each value sits from the median. A value just above and a value far above count the same. The test therefore says nothing about the size of any change, and it cannot tell a slow drift from a single jump that crosses the median once.
What a significant result means is narrow. It says the order of the elements is not what independent draws from one distribution would give. It does not say why. NIST lists randomness among the assumptions behind a process that is in control, and a failed runs test is a prompt to look for a model with time in it.
This calculator does not test for a trend, for periodicity or for more than two kinds of symbol, and it does not look for dependence between two series. Those need other methods.
Sources
Every source consulted for this guide. Links checked November 11, 2026.
- Wald-Wolfowitz runs test Wikipedia
- 1.3.5.13. Runs Test for Detecting Non-randomness NIST/SEMATECH e-Handbook of Statistical Methods
- Continuity correction Wikipedia
- Exact test Wikipedia
- Statistical randomness Wikipedia
- Median Wikipedia
- Abraham Wald Wikipedia
- 7.2 Definitions (Error Functions) NIST Digital Library of Mathematical Functions
- math - Mathematical functions Python Software Foundation
- Web Workers API MDN Web Docs
- BigInt MDN Web Docs
- Intl.NumberFormat MDN Web Docs