How Sudoku Puzzles Are Generated (Algorithm Notes)

white and black checked pattern

Every Sudoku puzzle in a newspaper, an app, or a browser tab was generated by an algorithm — and the algorithm is more subtle than people expect. The naive approach (fill the grid, erase some cells) doesn’t work because most erasure patterns produce puzzles with multiple solutions, which means they aren’t real Sudoku puzzles. The actual algorithm has three stages, each with non-obvious constraints. Here’s how Sudoku puzzles are actually generated, why uniqueness is the central problem, and how difficulty rating works.

Key takeaways

  • Sudoku generation has three stages: fill a valid completed grid, remove cells, verify the puzzle has a unique solution.
  • The uniqueness constraint is what makes the algorithm non-trivial — most random removals produce multi-solution puzzles.
  • Difficulty rating measures which solving techniques are required to find the solution.
  • A typical “easy” Sudoku needs only basic techniques; “expert” puzzles require advanced patterns like X-wing or Swordfish.
  • Generating a single hard puzzle can require many failed attempts and significant computation.

Stage 1: Generate a valid completed grid

The first step is to produce a fully filled 9×9 grid that satisfies Sudoku’s three rules — every row contains 1-9 exactly once, every column contains 1-9 exactly once, and every 3×3 box contains 1-9 exactly once. There are many ways to do this; the standard approach is backtracking.

The backtracking algorithm: try to place a 1 in the first empty cell. If that’s consistent with the constraints, move to the next cell and try to place a 1, then a 2 if 1 fails, and so on. If you reach a cell where no value works, back up to the previous cell and try the next value. Continue until the grid is full.

For Sudoku, the search space is enormous but the backtracking is fast because constraints fail quickly — most candidate placements get rejected immediately. A well-implemented backtracker can produce a random valid grid in milliseconds.

Some generators take a shortcut: start with one of the known canonical solutions (there are 6.67 × 10^21 valid Sudoku grids), then randomly permute it via symmetry operations (swap two rows within the same band, swap two columns within the same stack, relabel digits, transpose). These operations preserve the validity of the grid while randomizing the appearance.

Stage 2: Remove cells (carefully)

The next step is to remove cells until the puzzle has the desired number of clues. A standard Sudoku puzzle has between 17 and 40 clues remaining; the rest of the cells are blank for the player to solve.

The naive approach — pick random cells and erase them until you’ve reached the target count — produces puzzles that almost always have multiple solutions. A puzzle with multiple solutions isn’t a real Sudoku; it’s an unfair puzzle, because the solver can’t deduce one correct answer.

The actual algorithm is: pick a cell to remove, remove it, and then check whether the resulting puzzle still has a unique solution. If yes, the removal stands. If no, restore the cell and try a different one. Repeat until you’ve removed enough cells or no further removals preserve uniqueness.

The uniqueness check is the expensive part. The standard approach is to run a full solver on the puzzle after each removal — but instead of stopping at the first solution found, continue searching to see if a second solution exists. If two solutions exist, the puzzle is multi-solution; restore the cell. If only one solution exists, the removal is safe.

Why uniqueness matters

A Sudoku puzzle with multiple solutions can be technically solved (some choice of values fills the grid validly), but the solver has no way to deduce which solution is correct from the given clues. That means at some point in the solve, the player has to guess — and any guess might be the right one.

True Sudoku puzzles are guess-free. Every cell can be deduced from the existing clues using logical inference. The uniqueness constraint is what guarantees this. Mathematical proof: if a puzzle has a unique solution, then for every empty cell there must exist a chain of deductions that determines its value, because if multiple values were consistent with the constraints, the puzzle would have multiple solutions.

This is why the minimum number of clues for a valid Sudoku is 17. McGuire, Tugemann, and Civario proved in 2012, through extensive computation, that no valid Sudoku exists with 16 or fewer clues. Many 17-clue puzzles exist (around 49,000 unique ones are known), but they’re rare and tend to be very hard.

Stage 3: Rate the difficulty

The third stage is rating the puzzle’s difficulty. Cell-count is a rough proxy (fewer clues usually means harder), but the real measure is which solving techniques are required. A puzzle that can be solved with only “naked singles” and “hidden singles” is easy. A puzzle that requires “naked pairs,” “pointing pairs,” and “box-line reductions” is medium. A puzzle that needs “X-wing,” “Swordfish,” “XY-wing,” or chain-based deductions is hard.

Most generators have a difficulty-rating module that attempts to solve the puzzle using only “easy” techniques first. If those succeed, the puzzle is rated easy. If they fail, the rater tries adding medium techniques. If those succeed, the puzzle is rated medium. And so on through the difficulty levels.

This means generating a hard Sudoku is much more expensive than generating an easy one. Easy puzzles can be produced in milliseconds. Hard puzzles often require generating many candidates and discarding the ones that turn out to be solvable with easy techniques — you want the puzzle that specifically needs the advanced techniques.

The technique hierarchy

The standard solving techniques, in roughly increasing complexity:

  • Naked Single: A cell with only one possible value. Place it.
  • Hidden Single: A value that can only go in one cell of a row, column, or box. Place it.
  • Naked Pair / Triple: Two (or three) cells in the same unit that can only contain the same two (or three) values. Other cells in that unit can’t contain those values.
  • Pointing Pair / Box-Line Reduction: A value that can only go in two or three cells of a box, all in the same row or column. The value can be eliminated from other cells in that row or column.
  • X-Wing: A value that appears in only two rows, each in the same two columns. The value can be eliminated from those columns elsewhere.
  • Swordfish: The 3×3 generalization of X-Wing.
  • XY-Wing, XYZ-Wing: Chain-based eliminations involving specific patterns of bivalue cells.
  • Forcing Chains, Coloring: Long-chain logical deductions, often considered the boundary between solvable-by-logic and requires-guessing.

Most casual Sudoku players use the first four techniques. Tournament solvers use all of them, plus several more exotic patterns. For an introduction to the easier techniques, see our how to solve Sudoku guide, and our advanced Sudoku techniques piece covers the chain-based ones.

Why hard puzzles are computationally expensive to generate

To produce a puzzle that requires X-Wing or Swordfish, the generator must produce a puzzle that can’t be solved by simpler techniques. The straightforward approach is rejection sampling — generate puzzles randomly, rate them, keep only the ones at the desired difficulty.

Easy puzzles are common in the space of all valid puzzles; hard puzzles are rare. Generating a single “expert” Sudoku by rejection sampling can require evaluating thousands of candidate puzzles, most of which get rejected for being too easy. This is why Sudoku apps often have a noticeable delay when you request a hard puzzle — the generator is grinding through many failures behind the scenes.

Some advanced generators take a smarter approach. They start with techniques in mind — pick the structure of an X-Wing pattern, then build a puzzle around it that requires the player to find that exact X-Wing to progress. This is faster but harder to implement correctly.

Symmetry preferences

Most published Sudoku puzzles have visual symmetry — the pattern of clues is symmetric around the center, or rotationally symmetric. This isn’t required for the puzzle to be valid, but it’s an aesthetic preference. Generators that produce symmetric puzzles add an additional constraint: when removing a cell, also remove its symmetric counterpart.

This constraint reduces the puzzle space significantly, which is why symmetric puzzles are slightly harder to generate at the same clue count. It also tends to produce slightly easier puzzles on average, because the symmetry pattern provides more clues than an asymmetric puzzle with the same count.

What this means for the player

Several practical observations follow from the generation algorithm:

  • Every valid published puzzle has exactly one solution. If you find yourself needing to guess, you’re probably missing a technique you haven’t learned yet.
  • The difficulty rating tells you which techniques you’ll need. If a puzzle is rated “expert,” expect to need chain-based techniques, not just naked singles.
  • Cell count is a rough proxy for difficulty. Most easy puzzles have 35+ clues; most hard ones have 25 or fewer. But the relationship isn’t perfect — a 27-clue puzzle might be solvable by easy techniques while a 32-clue puzzle requires advanced ones.
  • 17-clue puzzles are very hard. The minimum-clue puzzles tend to require expert techniques to solve. They exist but they’re not casual.

The historical context

The number puzzle that became Sudoku originated in the United States in 1979 (Howard Garns’s “Number Place”) and was popularized in Japan in the 1980s. Algorithmic generation became standard in the 2000s when Sudoku exploded in worldwide newspapers. Most newspaper Sudoku from the 2005 boom onward was algorithmically generated and verified, not hand-crafted.

The mathematical study of Sudoku — counting valid grids, proving the 17-clue minimum, characterizing difficulty — is an active research area that has produced several notable results over the last two decades. The 17-clue minimum proof alone consumed millions of CPU-hours of computation.

Frequently asked questions

How many possible Sudoku grids exist?

Approximately 6.67 × 10^21 valid completed Sudoku grids exist. Bertram Felgenhauer and Frazer Jarvis calculated this number in 2005. After accounting for symmetries (rotations, reflections, relabelings that produce equivalent puzzles), the number reduces to about 5.5 billion essentially distinct grids.

What’s the minimum number of clues in a Sudoku?

Seventeen. McGuire, Tugemann, and Civario proved in 2012, through exhaustive computation, that no valid Sudoku puzzle exists with 16 or fewer clues. Many 17-clue puzzles exist and they tend to be quite difficult.

Are all Sudoku puzzles solvable with logic alone?

True Sudoku puzzles, by definition, have a unique solution deducible by logical inference. If you can’t solve a puzzle without guessing, either the puzzle is invalid (multi-solution, which isn’t a real Sudoku) or you haven’t learned the technique it requires.

How are Sudoku difficulty ratings calculated?

By identifying which solving techniques the puzzle requires. Easy puzzles can be solved with only naked and hidden singles. Medium puzzles add naked pairs and pointing pairs. Hard puzzles add X-Wing and Swordfish. Expert puzzles add chain-based techniques. The hardest technique needed sets the rating.

Why does generating a hard Sudoku take longer than an easy one?

Because the generator typically uses rejection sampling — generate candidates randomly and discard the ones that don’t meet the difficulty target. Hard puzzles are rare in the random space, so many candidates get rejected before one is kept. Easy puzzles are common, so the first generated puzzle usually works.

The takeaway

Sudoku puzzles are generated in three stages: fill a valid grid, remove cells one at a time while preserving uniqueness, and rate the result by the techniques it requires. The uniqueness constraint is what separates real Sudoku from invalid puzzles, and it’s why naive generators fail. The mathematics behind the algorithm is more elegant than the puzzle’s surface simplicity suggests. For a different kind of constrained-mathematics game, the Chrome Dino game has its own algorithmic core — randomness within designed constraints, the same fundamental shape Sudoku generation depends on.

Related guides

🔌 Connect any AI assistant to dinogame.gg — we run an MCP server: https://dinogame.gg/mcp