Documentation Center

  • Trial Software
  • Product Updates


Run test for randomness


h = runstest(x)
h = runstest(x,v)
h = runstest(x,'ud')
h = runstest(...,param1,val1,param2,val2,...)
[h,p] = runstest(...)
[h,p,stats] = runstest(...)


h = runstest(x) performs a runs test on the sequence of observations in the vector x. This is a test of the null hypothesis that the values in x come in random order, against the alternative that they do not. The test is based on the number of runs of consecutive values above or below the mean of x. Too few runs indicate a tendency for high and low values to cluster. Too many runs indicate a tendency for high and low values to alternate. The test returns the logical value h = 1 if it rejects the null hypothesis at the 5% significance level, and h = 0 if it cannot. The test treats NaN values in x as missing values, and ignores them.

runstest uses a test statistic which is approximately normally distributed when the null hypothesis is true. It is the difference between the number of runs and its mean, divided by its standard deviation.

h = runstest(x,v) performs the test using runs above or below the value v. Values exactly equal to v are discarded. The default value of v is the mean of x.

h = runstest(x,'ud') performs a test for the number of runs up or down. This also tests the hypothesis that the values in x come in random order. Too few runs indicate a trend. Too many runs indicate an oscillation. Values exactly equal to the preceding value are discarded.

h = runstest(...,param1,val1,param2,val2,...) specifies additional parameters and their values. Valid parameter/value pairs are the following:

  • 'alpha' — A scalar giving the significance level of the test

  • 'method' — Either 'exact' to compute the p value using an exact algorithm, or 'approximate' to use a normal approximation. The default is 'exact', except for runs up/down when the length of is 51 or more. The 'exact' method is not available for runs up/down when the length of x is over 50.

  • 'tail' — Performs the test against one of the following alternative hypotheses:

    • 'both' — two-tailed test (sequence is not random)

    • 'right' — right-tailed test (like values separate for runs above/below, direction alternates for runs up/down)

    • 'left' — left-tailed test (like values cluster for runs above/below, values trend for runs up/down)

[h,p] = runstest(...) returns the p value of the test. The output p is computed from either the test statistic or the exact distribution of the number of runs, depending on the value of the 'method' parameter.

[h,p,stats] = runstest(...) returns a structure stats with the following fields:

  • nruns — The number of runs

  • n1 — The number of values above v

  • n0 — The number of values below v

  • z — The test statistic


x = randn(40,1);
[h,p] = runstest(x,median(x))
h =
p =

See Also


Was this topic helpful?