Bulls & Cows strategy
Open with any four different digits (1234 is as good as anything), then choose each next guess from the score you just got. Do that and you will crack a 4-digit code in about 5 guesses. Our solver checked all 4,536 possible codes: it averages 5.18 guesses and never needs more than 7.
New to the game? Read the rules and a worked example first. Every number on this page uses the rules played here: all digits different and the code can’t start with 0.
The numbers by code length
| Digits | Possible codes | Solver average | Solver at most | Random fitting guess |
|---|---|---|---|---|
| 3 | 648 | 4.93 | 6 | ≈5.2 |
| 4 | 4,536 | 5.18 | 7 | ≈5.4 |
| 5 | 27,216 | ≈5.7 | – | ≈5.9 |
- Possible codes: 9 choices for the first digit (1–9), then 9, 8, 7 and 6 for the next ones. So 9 × 9 × 8 × 7 = 4,536 for 4 digits. A blind first guess is right 1 time in 4,536.
- Solver: a program that picks the guess leaving the fewest codes on average. The 3- and 4-digit figures come from playing every possible code; the 5-digit figures from 1,000 random games, so there is no proven maximum.
- Random fitting guess: always guessing a random code that agrees with every score so far. No clever opening, just careful bookkeeping, and it is only a quarter of a guess behind the solver.
≈ means a simulated average, not an exact count.
Longer codes take surprisingly few extra guesses, because every score also tells you more. Five digits have six times as many codes as four, yet need only about half a guess more.
The best first guess
Any four different digits. Before your first guess every code is equally likely, so the only thing that makes openings differ is the 0, which can’t start the secret code. That splits openings into two groups, and they are almost identical:
| First guess | Codes left on average | Codes left at worst |
|---|---|---|
| 1234 (no 0) | 830 | 1,260 |
| 1023 (with a 0) | 827 | 1,320 |
We start with 1234: its worst case is smaller, and our solver’s full games average 5.18 guesses from 1234 against 5.22 from 1023. The difference is tiny. Don’t spend time on the opening; spend it on the second guess.
What your first score tells you
All 14 scores 1234 can get, how often each comes up, and how many codes are still possible afterwards:
| Score for 1234 | How often | Codes left |
|---|---|---|
| 0 bulls, 1 cow | 27.8% | 1,260 |
| 0 bulls, 2 cows | 25.5% | 1,155 |
| 1 bull, 1 cow | 14.6% | 660 |
| 1 bull, 0 cows | 9.3% | 420 |
| 0 bulls, 0 cows | 6.6% | 300 |
| 0 bulls, 3 cows | 5.6% | 253 |
| 1 bull, 2 cows | 4.6% | 207 |
| 2 bulls, 0 cows | 3.6% | 165 |
| 2 bulls, 1 cow | 1.5% | 69 |
| 3 bulls, 0 cows | 0.5% | 23 |
| 0 bulls, 4 cows | 0.2% | 9 |
| 1 bull, 3 cows | 0.2% | 8 |
| 2 bulls, 2 cows | 0.1% | 6 |
| 4 bulls (solved) | 0.02% | 1 |
More than half of all games open with one or two cows and no bulls. And 3 bulls with 1 cow never happens: if three digits are in place and all four are in the code, the last one has only one spot left.
Choose the second guess from the score
Many players always open 1234 then 5678. That leaves 192 possible codes on average. Picking the second guess after seeing the first score leaves 135, about 30% fewer. What to do with each kind of score:
- 0 bulls, 0 cows (1 game in 15). Good news: cross out 1, 2, 3 and 4. The code is made from 0, 5, 6, 7, 8 and 9, and only 300 codes are left. Guess four of those six digits next, such as 5067.
- One or two cows (most games). Keep one or two of your digits, moving them or leaving them in place, and bring in two or three new ones. Each score then answers two questions at once: which of the old digits are in, and whether the new ones are.
- Three or four digits found. You know almost all the digits, so the job is positions. Swap two digits at a time: a change in bulls tells you exactly which digits moved into or out of their spots.
- Later on, guess codes that could still be right. Restricting yourself to codes that fit every score costs only a few hundredths of a guess (our solver: 5.25 against 5.22), and every such guess might win on the spot.
A worked game
The secret is 4271 (the same one as in the rules example). The solver cracks it in four guesses:
- 207 codes left12341 bull, 2 cows
Three of 1, 2, 3, 4 are in the code, one of them in its right spot.
- 48 codes left13560 bulls, 1 cow
Only one of 1, 3, 5, 6 is in the code. Since three of 1–4 are in, that one must be the 1 or the 3, so 5 and 6 are out, and 2 and 4 are definitely in. The fourth digit is 0, 7, 8 or 9.
- 1 code left42172 bulls, 2 cows
All four digits are right and two are in place. Only one code fits all three scores.
- Solved42714 bulls
Cracked in 4. About two games in three end in 5 guesses or fewer for the solver.
How often each game length happens
Our solver played all 4,536 possible 4-digit codes, always opening with 1234:
| Guesses needed | Codes | Share |
|---|---|---|
| 1 or 2 | 6 | 0.1% |
| 3 | 62 | 1.4% |
| 4 | 601 | 13.2% |
| 5 | 2,353 | 51.9% |
| 6 | 1,479 | 32.6% |
| 7 | 35 | 0.8% |
So if your friend cracks your code in 4, they played well or got lucky; in 3, mostly lucky. For the classic version where a code may start with 0 (5,040 codes), Tetsuro Tanaka proved in 1996 that the best possible average is 5.213 guesses (An optimal MOO strategy).
Tips for playing against a friend
- Write everything down. The in-game digit tracker lets you cross out digits that can’t be in the code and mark the ones that must be. Forgetting an early score wastes more guesses than a weak opening ever will.
- Change one thing at a time when you’re close. Swapping two digits or replacing one makes the change in bulls and cows easy to read.
- Watch your opponent’s board. If they are one guess from solving, a risky guess that could win now beats a safe one that only narrows things down.
- Avoid obvious secrets. Every code is equally hard for a solver, but a friend may try 1234 or 9876 just in case.
How the computer plays
- Easy only remembers its last two scores, so it sometimes repeats mistakes.
- Medium guesses a random code that fits every score so far: about 5.4 guesses on average with 4 digits.
- Hard picks the guess that leaves the fewest codes in the worst case. Beat it and you are playing like a solver.
Questions
What is the best first guess in Bulls and Cows?
Any four different digits. Every opening is almost equally good: 1234 leaves 830 possible codes on average and 1,260 at worst. Openings with a 0 in them leave 827 on average but 1,320 at worst. What matters is choosing the second guess after you see the first score.
How many guesses does it take to win Bulls and Cows?
With 4 digits, a computer solver needs 5.18 guesses on average and never more than 7. Good human players usually take 5 to 7. With 3 digits the solver averages 4.93 guesses and never needs more than 6.
How many possible codes are there?
With all digits different and no leading 0: 648 codes with 3 digits (9 × 9 × 8), 4,536 with 4 digits (9 × 9 × 8 × 7) and 27,216 with 5 digits (9 × 9 × 8 × 7 × 6).
What does 0 bulls and 0 cows mean?
None of the four digits you guessed is in the code, so you can cross all four out. It happens about 1 time in 15 on the first guess, and after 1234 it leaves only 300 possible codes, all made from 0, 5, 6, 7, 8 and 9.
Is Bulls and Cows easier with 3 digits?
Only a little. There are far fewer codes (648 instead of 4,536), but each score tells you less. A solver needs 4.93 guesses on average with 3 digits and 5.18 with 4 digits.
Can you get 3 bulls and 1 cow?
No. If three digits are in the right spots and all four digits are in the code, the last digit can only go in the one spot left, so it would be a bull too.
How we worked this out: we scored every possible guess against every possible code under the rules played here, then let a solver play every 3- and 4-digit code to the end. It picks the guess that leaves the fewest codes on average (with 3 digits, among guesses that still guarantee a win within 6), and may use guesses that can’t be the answer. 5-digit and “random fitting guess” figures are averages over simulated games.