← Bulls and Cows against the computer

Bulls and Cows Strategy: How Many Guesses It Takes and Which Number to Hide

The game has only 5,040 codes, so most figures here are exact: every secret was enumerated with the page's own code. The Expert level was unrolled into full game trees for 400 different first guesses (2,016,000 games), the Hard level played 40,000 real games, 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.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%.

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.2%63.7%
The smallest possible number each time5.56947.3%61.8%58.4%60.7%
0123, 4567, then any possible code at random5.58945.2%62.2%58.6%61.1%
0123, 4567, then the smallest possible number5.62944.2%60.9%57.3%59.6%
Fewest codes left on average, possible codes only5.32856.0%69.7%66.7%69.5%
Fewest codes left on average, any guess5.27760.9%72.6%70.0%73.0%
Most information (entropy), any guess5.24862.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.

  • 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.32840,000
Expertthe guess with the smallest worst-case group (Knuth's minimax), probes allowed5.3972,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.6%10.3%
535.2%40.8%41.6%
637.0%38.4%42.6%
712.1%5.8%4.5%
8 or later1.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.

Which code to hide

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 codeExpert, average guessesHard, average guesses
05.024.95
15.145.03
25.225.12
35.295.21
45.345.26
55.415.35
65.485.45
75.565.52
85.675.63
95.805.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 codeExpert needsYou win vs ExpertHard needsYou win vs Hard
01233.2413.8%3.4721.4%
begins with 014.5938.5%4.5038.7%
random5.3963.7%5.3261.2%
begins with 95.8076.6%5.7373.4%
begins with 986.0081.9%5.9078.0%
98706.1684.7%5.9880.7%
98076.2086.1%6.0082.0%
  • The worst code is 0123. Hard named it with its second guess in 42% of 3,000 games, and Expert broke it within four guesses in 86% of cases.
  • The best codes begin with 98. The five hardest for Expert were 9807, 9870, 9817, 9871 and 9860, all at 6.1 to 6.2 guesses; 42 codes out of 5,040 hold out for six or more on average.
  • Order matters as well as size. Codes with digits in ascending order fall to Expert in 4.96 guesses on average, codes in descending order in 5.72.
  • The Random button is the middle of the road. It gives the 61–64% of the previous sections; choosing the code yourself is worth about twenty points more.

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).

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 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.

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 Which Number to Hide”, You vs Computer, https://youvscomputer.com/games/bulls-and-cows-strategy.html