# Probability of Same Consecutive Digits Two / Three / Four Times

For work I have a device that generates a 'random' 8 digit code, with digits ranging from 0-9. The code can start with 0. For example:

01234567
76925951
93508862

I am looking for the probability of generating a code that has:

A) The same digit consecutively at least two times (like 84316635)
B) The same digit consecutively at least three times (like 44468941)
C) The same digit consecutively at least four times (like 23577776)

It doesn't matter if the consecutive digits appear more often than once, or in a longer string than required. It qualifies as soon as it meets the requirements at least once.

These are three separate questions, though obviously related. I can generate some code that gives me the answers, but I'm looking for the calculations without running the experiment. If possible, I'm looking for a single formula to answer all three questions, with variable X being number of consecutive same digits required.

A) The probability that no two consecutive digits are the same is $$0.9^7$$. (The first digit can be anything. The second digit can be anything except the first digit. The third digit can be anything except the second digit, and so on.) So the probability of at least one pair of consecutive digits is $$1 - 0.9^7$$.

B) There are six possible starting positions for the first occurrence of the digit that appears three times in a row. The probability of three digits the same in a row is $$0.01$$ (first digit is anything, second and third are the same with probabiliy $$0.1$$ each). After the three of a kind appears, we don't care what happens. So the probability is

$$\sum_{k=0}^5 0.9^k(0.01).$$

C) Same thing as above, except there are five places for the first occurrence of the digit that appears four times in a row, with probability $$0.001$$:

$$\sum_{k=0}^4 0.9^k(0.001).$$

• Haven't you counted some sequences more than once in B? For example, it looks like 99912999 gets counted twice, once for the 999 starting in position 1 and once for the 999 starting in position 6. Similarly for 99991234, once for the 999 starting in position 1 and once for the 999 starting in position 2. – awkward Dec 11 '18 at 20:58
• @awkward No. There are six terms in the answer to (B). 99912999 would only be included in the first one ($k=0$, probability $0.01$). The terms are broken out by the starting position of the first group of three. Whether there is more than one group of three (or even a group of 8!) is immaterial. – John Dec 11 '18 at 21:05
• Have you thought about applying your method in B to a shorter string, say 4 digits instead of 8? I assume you would run the sum in your answer for k = 0 to 1 instead of from 0 to 5, yielding a probability of 0.019. But by direct counting, there are 100 strings of length 4 with at least one run of 3 digits, yielding a probability of $100/10^4 = 0.01$ (90 of the form baaa, 90 like aaab, 10 like aaaa, so 100 in all) – awkward Dec 11 '18 at 21:38
• @awkward $90 + 90 + 10 = 190$, so the probability of $0.019$ is correct! – John Dec 11 '18 at 21:42
• Perfect. Well explained, and I appreciate the formula being added. Thanks, John! – YoupT Dec 12 '18 at 10:52