← Bulls and Cows against the computer

Bulls and Cows Strategy: How Many Guesses It Takes and How to Beat the Computer

The game has only 5,040 codes, so most figures here are exact: every secret was enumerated with the page's own code. The Hard and Expert levels were unrolled into complete game trees with the page's own code — 2,000 trees for Hard (10,080,000 games) and 400 for Expert (2,016,000 games), each tree with its own random choices — and play at random among the possible codes was simulated 400,000 times per figure.
The short answer

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.1% against Hard and 63.3% against Expert. Hard, at 5.32 guesses, is the fastest level here, ahead of Expert at 5.38; and the code you hide makes no difference: every code takes each level the same number of guesses on average, within sampling noise.

What exactly is played here

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.

What the first answer tells you

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 guessCodes leftHow often
1 cow1,44028.57%
2 cows1,26025.00%
1 bull + 1 cow72014.29%
1 bull4809.52%
nothing3607.14%
3 cows2645.24%
1 bull + 2 cows2164.29%
2 bulls1803.57%
2 bulls + 1 cow721.43%
3 bulls240.48%
4 cows90.18%
1 bull + 3 cows80.16%
2 bulls + 2 cows60.12%
4 bullsthe code itself0.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 second guess: 4567 or something possible?

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 0123Codes leftAfter 4567After a possible codeExampleBest of all 5,040 guesses
1 cow1,4402952551456254
2 cows1,2603222041435202
1 bull + 1 cow7201841170245108
1 bull4809880045680
nothing3606565456765
3 cows26410350123445
1 bull + 2 cows2168447023440
2 bulls1804634014527

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.

How many guesses each method takes

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.

MethodAverage guessesWorst caseDone in 5 or fewerWins vs Easyvs Hardvs Expert
Any possible code, picked at random5.47949.8%64.6%61.1%63.3%
The smallest possible number each time5.56947.3%61.8%58.3%60.3%
0123, 4567, then any possible code at random5.58945.2%62.2%58.5%60.7%
0123, 4567, then the smallest possible number5.62944.2%60.9%57.1%59.2%
Fewest codes left on average, possible codes only5.32856.0%69.7%66.6%69.1%
Fewest codes left on average, any guess5.27760.9%72.6%69.8%72.6%
Most information (entropy), any guess5.24862.7%73.2%70.5%73.4%

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.

  • Consistency is the whole basic method. A guess that contradicts an earlier answer cannot win on the spot, and a player who never makes one is only a quarter of a guess behind the best possible play.
  • Do not go through the numbers in order. Always taking the smallest possible number costs 0.09 of a guess against picking one at random, because neighbouring numbers share digits and places and split the remainder worse. When several codes fit, prefer the one that looks least like your earlier guesses.
  • The fixed opening costs about 0.1 of a guess and two to three points of win rate. It is easier on the head, not better.
  • Nine guesses is the limit for consistent play here. With the smallest possible number it happens for five codes out of 5,040 (5293, 9204, 9214, 9241, 9431), and eight or nine guesses for 2.7% of codes.

How each computer level guesses

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.

LevelHow it choosesAverage guessesWorst case seenGames measured
Easya random possible code5.479400,000
Hardthe possible code that leaves the fewest candidates on average5.32810,080,000
Expertthe guess with the smallest worst-case group (Knuth's minimax), probes allowed5.3882,016,000

And how the games are spread by length:

Your code is broken on guessEasyHardExpert
3 or earlier2.4%2.4%1.0%
412.1%12.8%10.6%
535.2%40.8%42.2%
637.0%38.2%42.2%
712.1%5.7%4.2%
8 or later1.1%0.06%0.003%

The surprise is in the averages: Expert is slower than Hard, 5.38 against 5.32. Minimax insures against the worst answer and pays for the insurance in ordinary games: Expert needed eight guesses in only 63 of 2,016,000 games, against 6,000 of 10,080,000 for Hard, but it finishes in four or fewer less often — 11.5% of games against 15.2%. 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 examines all 5,040 guesses at every turn, but whenever several score equally — which happens on almost every turn — it draws one of them at random, so its games vary as well.

Which code to hide: it does not matter

Against Easy this was always so: it picks at random among the possible codes, so every secret takes it the same 5.47 guesses. Hard and Expert rate their guesses, and many guesses often get exactly the same rating; when that happens, both draw one of the tied guesses at random. No code is reached earlier or later than any other, and the measurements confirm it.

First digit of your codeExpert, average guessesHard, average guesses
05.395.32
15.385.32
25.385.32
35.395.32
45.385.32
55.385.32
65.395.31
75.385.31
85.365.32
95.375.32

Per first digit that is 201,600 games on Expert and 1,008,000 on Hard. The spread from digit to digit is a few hundredths of a guess, the size of the sampling noise, and there is no trend from 0 to 9. The same holds for single codes and for patterns people like to hide:

Your codeExpert needsHard needsGames per level (Expert / Hard)
01235.445.30400 / 2,000
98705.445.31400 / 2,000
98075.415.28400 / 2,000
begins with 015.375.3222,400 / 112,000
begins with 985.365.3222,400 / 112,000
digits in ascending order5.385.3284,000 / 420,000
digits in descending order5.385.3284,000 / 420,000
any code (all 5,040)5.385.322,016,000 / 10,080,000
  • The spread is pure chance. Across all 5,040 codes the averages differ by 0.039 guesses on Expert (400 games per code) and by 0.019 on Hard (2,000 games per code), exactly what random variation alone predicts for samples of that size.
  • The single codes in the table are within that noise. With 400 games a code’s Expert average is uncertain by about ±0.04, so 5.44 against 5.38 is not a weakness of 0123 or 9870.
  • Hide whatever you like, or press Random. Your chances depend only on how quickly you yourself break the computer’s code: 61.1% against Hard and 63.3% against Expert if you just play consistently, about 70% with the better methods above.

Earlier versions of this page took the first of several equal guesses in numerical order, which made small codes such as 0123 much easier for Hard and Expert to find than codes such as 9870. Ties are now drawn at random, and the figures above were measured after that change.

Questions and answers

How many guesses does it take to solve Bulls and Cows?

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.

What is the best first guess in Bulls and Cows?

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.

Is 0123 then 4567 a good opening?

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.

What is the best strategy in Bulls and Cows?

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.

How do you beat the computer at Bulls and Cows?

On this page you guess first and win ties, so simply never contradicting an earlier answer wins 61–65% of duels on every level. To do better, choose among the possible codes the one that splits the remaining codes most evenly: played strictly, that wins about 67–70%. Which code you hide does not matter — the computer finds every code equally fast — and the hardest level to beat is Hard, not Expert.

How many combinations are there in Bulls and Cows?

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.

What is the difference between Bulls and Cows and Mastermind?

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.

Play Bulls and Cows against the computer →
Where to go from here
To cite this page: “Bulls and Cows Strategy: How Many Guesses It Takes and How to Beat the Computer”, You vs Computer, https://youvscomputer.com/games/bulls-and-cows-strategy.html