Runs Testing Using C# Simulation in Visual Studio Magazine

I wrote an article titled “Runs Testing Using C# Simulation” in the June 2022 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2022/06/17/runs-testing.aspx.

Suppose you observe a sequence of people entering a store and you record the color of shirt worn by each person. You see this sequence of 24 shirts:

0, 0, 3, 3, 2, 1, 1, 2, 0, 0, 3, 3, 2, 0, 0, 1, 1, 2, 2, 2, 2, 0, 1, 1

where 0 = white shirt, 1 = yellow, 2 = red, 3 = black. You’re interested in knowing if the pattern is random or not. The pattern doesn’t look very random, but how can you quantify that observation? This is an example of a runs test.

A run is a set of consecutive data points that are the same. The sequence above has 13 runs: (0 0), (3 3), (2), (1 1), (2), (0 0), (3 3), (2), (0 0), (1 1), (2 2 2 2), (0), (1 1).

I present a demo program that estimates the probability of seeing fewer than 13 runs in a random sequence in two different ways. The first technique uses the fact that over many iterations the number of runs is approximately Normal (bell-shaped curve) distributed. The statistical probability estimate is 0.0019 — only about 2 out of 1,000 — possible, but very unlikely.

The second technique uses a raw counting technique. In the 1,000,000 iterations there were 13 or fewer runs in a random sequence only 4,888 + 1,370 313 + 59 + 13 + 1 = 6,644 times which is an estimated probability of 0.0066 — only about 7 out of 1,000. Again, possible, but very unlikely.

It’s important to remember that the results of a runs test on a sequence can only suggest that a sequence is probably not generated randomly. Therefore, conclusions should be conservative and similar to, “It is unlikely the sequence was generated by a random process, not “The pattern isn’t random”.



Fractal artwork combines randomness with patterns. Here are two wonderful illustrations by artist Jorge Abalo (“batjorge”) that represent alien landscapes and life. Left: “Deep Bonds”. Right: “Tryptamine Trance”.


This entry was posted in Miscellaneous. Bookmark the permalink.