With four different digits there are 5,040 codes, and a player who simply never contradicts the earlier answers breaks one in 5.47 guesses on average; the popular opening 0123, 4567 is slower, 5.58. On this page you guess first and win ties, so that plain method already wins 64.6% of duels against Easy, 61.2% against Hard and 63.7% against Expert. The code you hide matters even more: Hard and Expert settle ties in favour of the smallest number, so they find 0123 in about three and a half guesses and need about six for 9870 — your win rate against Expert goes from 14% to 85%.
Both sides hide four different digits from 0 to 9, a leading zero is allowed, repeats are not. That gives 10×9×8×7 = 5,040 codes. The computer's code is drawn at random with equal chances for all of them, so no guess of yours is more likely than another before the first answer.
You and the computer take turns, you first. The two searches do not affect each other: your guesses tell the computer nothing and its guesses tell you nothing. The duel is therefore a comparison of two numbers — how many guesses you needed and how many it needed — and an equal count is your win, because you finished your turn first.
That tie rule is worth a lot. Two players of identical strength need the same number of guesses in 29.1% of games, so the side that moves first wins not 50% but 64.6%. You start every duel here with that head start.
Practical consequence: you do not have to be faster than the computer, only not slower. Against a level that averages 5.3 to 5.5 guesses, finishing in five wins about 85% of the time and finishing in six still wins a little under half.
Before the first answer all guesses are equal, so take any, for instance 0123. The answer splits the 5,040 codes into 14 groups of very different size, and you are left with one of them:
| Answer to the first guess | Codes left | How often |
|---|---|---|
| 1 cow | 1,440 | 28.57% |
| 2 cows | 1,260 | 25.00% |
| 1 bull + 1 cow | 720 | 14.29% |
| 1 bull | 480 | 9.52% |
| nothing | 360 | 7.14% |
| 3 cows | 264 | 5.24% |
| 1 bull + 2 cows | 216 | 4.29% |
| 2 bulls | 180 | 3.57% |
| 2 bulls + 1 cow | 72 | 1.43% |
| 3 bulls | 24 | 0.48% |
| 4 cows | 9 | 0.18% |
| 1 bull + 3 cows | 8 | 0.16% |
| 2 bulls + 2 cows | 6 | 0.12% |
| 4 bulls | the code itself | 0.02% |
On average 931 codes survive the first guess. More than half of all games begin with “1 cow” or “2 cows”, the two least helpful answers; an answer with nothing in it is better than both, and anything with two bulls or three cows is a present.
The table is the same for every first guess: only the digits are renamed. It also shows why the count of bulls and cows together is the first thing to read: it says how many of your four digits are in the code at all, and that alone separates 360 codes from 1,440.
The usual advice is to open 0123, 4567 whatever the first answer was, to “see all the digits”. The alternative is a second guess that could itself be the code: it keeps as many digits of the first guess as the answer named, bulls in place and cows moved, and fills the rest with new digits. Here is how many codes are left on average after each:
| First answer to 0123 | Codes left | After 4567 | After a possible code | Example | Best of all 5,040 guesses |
|---|---|---|---|---|---|
| 1 cow | 1,440 | 295 | 255 | 1456 | 254 |
| 2 cows | 1,260 | 322 | 204 | 1435 | 202 |
| 1 bull + 1 cow | 720 | 184 | 117 | 0245 | 108 |
| 1 bull | 480 | 98 | 80 | 0456 | 80 |
| nothing | 360 | 65 | 65 | 4567 | 65 |
| 3 cows | 264 | 103 | 50 | 1234 | 45 |
| 1 bull + 2 cows | 216 | 84 | 47 | 0234 | 40 |
| 2 bulls | 180 | 46 | 34 | 0145 | 27 |
Over all codes, 0123 followed by 4567 leaves 216 candidates on average, and 0123 followed by any possible code leaves 159. The very best second guess, chosen among all 5,040 for each answer, leaves 156: after one answer, “any code that is still possible” is already within 2% of the best there is, and 4567 is the right second guess only after an answer of nothing.
The column “after a possible code” is the average over every possible second guess, not a hand-picked one; for the big groups they are all practically equal. The fixed opening pays for its convenience at the end of the game as well: 5.58 guesses on average against 5.47 for consistent play from the second guess on.
If you do open 0123, 4567: the two totals show how many of 8 and 9 are in the code. Bulls and cows from both answers add up to four in 33.3% of games (neither 8 nor 9), to three in 53.3% (one of them) and to two in 13.3% (both). The pair of answers splits the codes into 63 groups, the largest of 504.
The first four rows are methods a person can follow with pencil and paper; the last three need a computer and are here for comparison. The win columns are for the duel on this page, with your own code picked by the Random button.
| Method | Average guesses | Worst case | Done in 5 or fewer | Wins vs Easy | vs Hard | vs Expert |
|---|---|---|---|---|---|---|
| Any possible code, picked at random | 5.47 | 9 | 49.8% | 64.6% | 61.2% | 63.7% |
| The smallest possible number each time | 5.56 | 9 | 47.3% | 61.8% | 58.4% | 60.7% |
| 0123, 4567, then any possible code at random | 5.58 | 9 | 45.2% | 62.2% | 58.6% | 61.1% |
| 0123, 4567, then the smallest possible number | 5.62 | 9 | 44.2% | 60.9% | 57.3% | 59.6% |
| Fewest codes left on average, possible codes only | 5.32 | 8 | 56.0% | 69.7% | 66.7% | 69.5% |
| Fewest codes left on average, any guess | 5.27 | 7 | 60.9% | 72.6% | 70.0% | 73.0% |
| Most information (entropy), any guess | 5.24 | 8 | 62.7% | 73.2% | 70.7% | 73.8% |
The rows with “at random” in them come from 400,000 simulated games each; the others are exact, counted over all 5,040 codes with 0123 as the first guess. For reference, the best average that any method can reach is known from the literature to be 5.213 guesses, with a worst case of seven; it was not recomputed here.
All three levels make a random first guess and never name a code that contradicts your answers, unless it is a deliberate probe on Expert. They differ in what they choose among the codes that are left.
| Level | How it chooses | Average guesses | Worst case seen | Games measured |
|---|---|---|---|---|
| Easy | a random possible code | 5.47 | 9 | 400,000 |
| Hard | the possible code that leaves the fewest candidates on average | 5.32 | 8 | 40,000 |
| Expert | the guess with the smallest worst-case group (Knuth's minimax), probes allowed | 5.39 | 7 | 2,016,000 |
And how the games are spread by length:
| Your code is broken on guess | Easy | Hard | Expert |
|---|---|---|---|
| 3 or earlier | 2.4% | 2.4% | 1.0% |
| 4 | 12.1% | 12.6% | 10.3% |
| 5 | 35.2% | 40.8% | 41.6% |
| 6 | 37.0% | 38.4% | 42.6% |
| 7 | 12.1% | 5.8% | 4.5% |
| 8 or later | 1.1% | 0.06% | never |
The surprise is in the averages: Expert is slower than Hard, 5.39 against 5.32. Minimax insures against the worst answer and pays for the insurance in ordinary games: Expert never needed more than seven guesses in any of the 2,016,000 games, but it finishes in four or fewer less often than Hard does. In a duel decided by who is faster on average, that makes Hard the toughest opponent on this page — every method in the table above wins less often against Hard than against Expert.
Easy plays exactly the first method of the table above, so a duel against it between equals is decided by your right to move first. Hard looks only at possible codes and, while more than 1,000 are left, at a random sample of them, so its second guess varies from game to game. Expert's play is fully fixed once its first guess is made.
Against Easy it makes no difference: it picks at random among possible codes, so every secret takes it the same 5.47 guesses. Hard and Expert are another matter. When several guesses score equally, both take the first one in their list, and the list runs in numerical order from 0123 to 9876. Ties are frequent, so their guesses lean towards small numbers, and a code that looks like a small number is found sooner.
| First digit of your code | Expert, average guesses | Hard, average guesses |
|---|---|---|
| 0 | 5.02 | 4.95 |
| 1 | 5.14 | 5.03 |
| 2 | 5.22 | 5.12 |
| 3 | 5.29 | 5.21 |
| 4 | 5.34 | 5.26 |
| 5 | 5.41 | 5.35 |
| 6 | 5.48 | 5.45 |
| 7 | 5.56 | 5.52 |
| 8 | 5.67 | 5.63 |
| 9 | 5.80 | 5.73 |
The later digits pull the same way. Here is what that does to the duel if you yourself play any possible code at random:
| Your code | Expert needs | You win vs Expert | Hard needs | You win vs Hard |
|---|---|---|---|---|
| 0123 | 3.24 | 13.8% | 3.47 | 21.4% |
| begins with 01 | 4.59 | 38.5% | 4.50 | 38.7% |
| random | 5.39 | 63.7% | 5.32 | 61.2% |
| begins with 9 | 5.80 | 76.6% | 5.73 | 73.4% |
| begins with 98 | 6.00 | 81.9% | 5.90 | 78.0% |
| 9870 | 6.16 | 84.7% | 5.98 | 80.7% |
| 9807 | 6.20 | 86.1% | 6.00 | 82.0% |
The Expert figures are averages over 400 different first guesses of the computer for each code, the Hard figures by first digit come from about 4,000 games per digit, and the single codes on Hard from 3,000 games each (codes beginning with 98 and 01: about 470 games each).
With four different digits, five or six. A player who only names codes that fit all earlier answers needs 5.47 guesses on average and finishes within seven in 99% of games; the best possible play averages 5.213 and never needs more than seven.
There is none: with no repeated digits every first guess is the same guess with the digits renamed. Any of them leaves 931 codes on average: 1,440 after “1 cow”, the most common answer, and 360 after an answer of nothing.
It is convenient but not the best. After 0123 and 4567 an average of 216 codes are left; after 0123 and any code that fits the first answer, 159. Over the whole game the fixed opening costs about a tenth of a guess: 5.58 on average against 5.47.
Only name codes that agree with every answer so far, and among those prefer the one least like your earlier guesses. That alone averages about 5.5 guesses. The computer-only refinement — choosing the guess that leaves the fewest codes on average — brings it to 5.2–5.3.
On this page you guess first and win ties, so consistent play wins 61–65% of duels on every level. Then choose your own code: Hard and Expert find small numbers first, so hide something that begins with 98, such as 9870. That raises the win rate to about 80% against Hard and 85% against Expert.
With four different digits and a leading zero allowed, as here: 10×9×8×7 = 5,040. Versions that forbid a leading zero have 4,536, and versions with repeated digits 10,000.
Mastermind uses six colours in four places and allows repeats, 1,296 codes in all; Bulls and Cows uses ten digits without repeats, 5,040 codes. The answers work the same way: a black peg is a bull and a white peg is a cow. Mastermind can always be solved in five guesses, Bulls and Cows in seven.