This question already has an answer here:

There is a popular game called Mastermind in which one player guesses another player's secret sequence - it could be a word, some colors, or numbers. The guesser says a possible sequence, and they get back the number of correct numbers in the right spot and the number of correct numbers in the wrong spot. Here we will be playing Mastermind with 4-digit numbers.

Here is an example game with the answer "4560".

You start with 1234 and get the response "-1"; that means one digit is correct but in the wrong location. You guess 3465 and get "-2 +1"; that means two digits are in the wrong location and one digit is in the correct location. The other digit isn't part of the number at all.

The game continues:

1234: -1
3465: -2 +1
4503: -1 +2
4560 +4 (4 tries and win!)

You are very smart, so as soon as you get enough information you'll be able to guess the number instantly, but the problem is that you are the unluckiest person alive, especially at guessing numbers. So, in the worst case scenario, how many guesses do you need to find your opponent's number?

Keep in mind that 4-digit numbers can't start with 0!


marked as duplicate by Deusovi, DrunkWolf, Engineer Toast, manshu, kaine Jan 19 '16 at 15:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 7
    $\begingroup$ A genius person... some may even call you a master mind. $\endgroup$ – Ian MacDonald Jan 18 '16 at 20:21
  • $\begingroup$ :)) sorry for the way of expressing it. $\endgroup$ – Oray Jan 18 '16 at 20:22
  • 1
    $\begingroup$ that's a joke because you've just described the popular board game "mastermind" $\endgroup$ – Slepz Jan 18 '16 at 20:24
  • $\begingroup$ i didnt know the name of the game, to be honest i had no idea there was such game that everbody knows :p we were playing it with numbers though, not colors. $\endgroup$ – Oray Jan 18 '16 at 20:29
  • 5
    $\begingroup$ Read the answers at puzzling.stackexchange.com/questions/546/… . Most of them are applicable here as well. Your particular version has been mentioned by @JoeZ. I have not flagged because I am not 100% sure, but I have a strong feeling this would qualify as a duplicate question. $\endgroup$ – ghosts_in_the_code Jan 19 '16 at 11:11

The answer is


Using a script to simulate games where one picks randomly but draws perfect conclusions (excludes all possible configurations that can be eliminated based on previous guesses) given in javascript here as determined over runs of 10 million.

Comparing this to the normal version with 6 pegs we see that the average solve time with random guessing increases from 4.638 to 6.05 even though the amount of initial possibilities goes from 1296 to 9000.

  • $\begingroup$ sorry but this is bit applicable to my question because there are 10 different colors/numbers for this case. $\endgroup$ – Oray Jan 19 '16 at 14:33
  • $\begingroup$ @Oray true, I'll write a "random-smart" solver later an see how long that takes in worst case $\endgroup$ – DrunkWolf Jan 19 '16 at 16:25
  • $\begingroup$ @Oray ok so i wrote a 'random-smart' solver here and i've let it run up to a million runs. Haven't gotten a run that took more then 11 guesses. What it does is it generates all possible configuration, guesses randomly from between them, and eliminates all configurations that are no longer possible before guessing again. It averages steadily around 6 tries, and i don't believe it will go past 11 (or maybe 12) $\endgroup$ – DrunkWolf Jan 19 '16 at 18:18
  • $\begingroup$ yes the answer is 11 :) but there is a good explanation rather than running a program :p $\endgroup$ – Oray Jan 19 '16 at 18:20
  • $\begingroup$ @Oray writing a program is usually a lot faster though :) And it's nice to see that a very simple strategy that's also very doable by hand produces adequate results. $\endgroup$ – DrunkWolf Jan 19 '16 at 18:23

Let's assume the number is 1234

First guess will be 7890, response 0 (we know 7890 aren't included)

Second guess will be 5612, response -2 (two of 5612 are included, don't know where)

Third guess will be 3456, response -2 (two of 3,4,5,6 are included. Since we know from guess 1 that 7,8,9 and 0 don't feature, the other two digits must be 1 and 2. This means that keeping 5 and 6 from the second guess was incorrect, and going forward we need 1,2,3 and 4 in some order)

Fourth guess - 4,3,2,1, response is -4 (from guess 2, we now know that 1 and 2 are not the last 2 digits)

Fifth - 2,1,4,3, response is -4

Sixth - 1,2,3,4, response is 4 and we win.

This assumes that no digits in the answer can be repeated and that there isn't a worse way to mix things up between steps 4 and 5.

  • $\begingroup$ this is good start but in the worst case, you cannot guess 7890 at first. it is actually a good start for this game $\endgroup$ – Oray Jan 19 '16 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.