DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

Whatever Length-3 Coin Pattern You Pick, I Have a Reply That Beats It — and My Worst Case Is 2/3

You pick a pattern of three coin flips. Then I pick one, after seeing yours. We flip a fair coin until one of the two patterns appears, and whoever's pattern lands first wins.

There is no pattern you can choose that leaves me under a two-to-one favourite.

👉 Live, every probability solved exactly in your browser: https://dev48v.infy.uk/game/day79-penney-ante.html

Nothing here is simulated and nothing is a float

This is the part that matters more than the game. Each pair of patterns is solved as an absorbing Markov chain over rationals, with BigInt numerator and denominator. So "I win two thirds of the time" is 2n/3n, not 0.6667.

And every answer is cross-checked against Conway's leading-numbers algorithm, which shares no code with the chain — a closed form that computes the same odds by a completely different route. Two implementations that have to agree is worth considerably more than one that is asserted to be correct.

your pick my reply my odds
HHH THH 7/8
HHT THH 3/4
HTH HHT 2/3
HTT HHT 2/3
THH TTH 2/3
THT TTH 2/3
TTH HTT 3/4
TTT HTT 7/8

Eight rows, eight wins for the second player. The relation is non-transitive, which is why "pick the best pattern" is not a thing you can do.

The intuition that decides it for most people is simply wrong

Ask someone why THH beats HHT and they will reach for waiting time: THH must show up sooner on average.

It does not.

HHT, HTT, THH and TTH all have a mean waiting time of exactly 8 flips. Identical. And yet THH beats HHT three times in four.

Which pattern appears soonest on its own says nothing about which appears first when two are racing. Those are different questions, and the second one is the one the game asks. The race is decided by overlap structure: when HHT is one flip from completing, the flips already on the table can be finishing THH instead.

That is the whole trick, and it survives being explained. You can tell someone the strategy and still beat them.

The shape of the game changes with the length, and not how I expected

I assumed the second player's edge would grow smoothly with pattern length. It does not.

At length 2 there is no advantage at all. The second player's best reply wins exactly 1/2 — a dead-even game. Penney's ante needs length 3 to exist.

At length 4 the relation becomes genuinely cyclic. There are 42 triples where A beats B, B beats C, and C beats A. At length 3 there are none — the length-3 relation, non-transitive as it is, contains no 3-cycles at all.

So the game is not "the same idea, more of it" as patterns get longer. Length 2 is degenerate, length 3 is non-transitive but acyclic, and length 4 is properly cyclic.

Why exact rationals rather than a simulation

A simulation of this game would have been three lines and it would have been fine — you would get 0.667 and 0.875 and move on.

But then "the second player always wins" is an observation about a sample, and "42 cyclic triples at length 4" is a claim you cannot make at all, because near-tied pairs would flip sign with the seed. Cycles are exactly where a Monte Carlo answer stops being trustworthy: a 0.501 and a 0.499 are indistinguishable to a sampler and are the difference between a cycle existing and not.

With rationals, 1/2 is 1/2. The length-2 result is not "about half".

What this does not cover

Fair coins only — a biased coin changes every number here. Two players, one pair at a time; the multi-player version is a different problem. Patterns of one fixed length per game, so no HHH-versus-TT. And the length-4 enumeration is complete but the code is exponential in the pattern length, so length 6 was not run.

17 in-page checks and 224 verifier assertions, including the Conway cross-check on every pair. 0 failures.

Pure vanilla JS, one file, no build step, no dependencies.

Top comments (0)