Number Guessing Game

Guess the secret number. Hot/cold hints guide you in.

About this tool

Number Guessing Game

Number Guessing Game hides a secret number and gives hot or cold hints as you guess, with adjustable range and difficulty.

How to use it

  1. Set the range and difficulty.
  2. Guess a number.
  3. Use the hot and cold hints to narrow it down.

The optimal strategy is binary search

If the hints tell you higher or lower, always guess the midpoint of the remaining range. That halves the possibilities every time, so a range of 1 to 100 is guaranteed within seven guesses and 1 to 1,000 within ten.

The number of guesses needed is the base-two logarithm of the range size. Every doubling of the range costs you only one extra guess, which is why binary search scales so well and why it underlies so much of computing.

Worked example

Guessing within 1 to 100:

  • First guess50, halving to 50 possibilities
  • Second25 or 75, halving again
  • Worst case7 guesses

Result: Seven guesses covers 128 possibilities, which is why 100 always falls within it.

When it helps

  • A quick puzzle for a spare minute.
  • Teaching binary search intuitively.
  • Practising mental halving.
  • Showing a child how a good strategy beats guessing.

Common mistakes

  • Guessing sequentially from one. That is linear search and takes up to a hundred guesses instead of seven.
  • Ignoring the hint from a previous guess and re-guessing inside an eliminated range.
  • Not halving exactly. Guessing near the middle is fine, but drifting toward the edges costs guesses.
Number Guessing Game interface preview
Screenshot of the live Number Guessing Game interface.