How to generate Sudoku boards with unique solutions
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A Sudoku generator is harder to build than a Sudoku solver because you are optimizing for two outcomes at once: a valid puzzle and exactly one final solution. The standard approach is to generate a complete solved grid, remove clues one at a time, and test uniqueness after each removal. This article shows a practical design that produces varied boards while keeping generation time predictable.
Core Sections
Use Two Solvers with Different Jobs
A maintainable generator usually has two related solvers:
- A randomized solver that fills an empty board to create a complete valid solution.
- A counting solver that returns how many solutions a partially filled puzzle has, stopping as soon as it finds more than one.
Keeping these responsibilities separate makes the code easier to reason about and optimize.
Step 1: Build a Fully Solved Grid
Generation starts with an empty nine by nine board. The randomized solver tries candidates one through nine in shuffled order, which gives different completed grids across runs.
Passing an explicit seed is useful for tests because it makes outputs reproducible.
Step 2: Enforce Uniqueness While Removing Clues
After you have one solved grid, remove values cell by cell. For each tentative removal, count solutions. Keep the removal only when the count is exactly one.
The limit=2 cutoff is critical for speed. You do not need the exact number once you know there is more than one solution.
Difficulty Is Not the Same as Uniqueness
A puzzle with one solution can still be trivial or very hard. Clue count alone is an imperfect proxy. If your product needs difficulty labels, add a rating pass that emulates human techniques such as naked singles, hidden singles, and pairs before allowing trial-and-error.
A practical pipeline is:
- Generate and uniqueness-check puzzle.
- Run a deterministic difficulty rater.
- Keep only puzzles that fit desired bands such as easy, medium, hard.
This separation keeps generator logic clean and avoids guessing based on clue count.
Operational Considerations
Uniqueness checking is the expensive part, so most teams pre-generate puzzles in the background and store them. That gives stable response times for players and avoids heavy CPU spikes at request time.
Good production checks include:
- verify every puzzle has one solution before publishing
- store the solved board for answer validation
- track generation time metrics for tuning
- run periodic integrity checks on stored puzzles
Common Pitfalls
- Removing many clues in one pass and checking uniqueness only at the end, which often creates multiple solutions.
- Reusing mutable board references between recursion branches, causing subtle state corruption.
- Assuming fewer clues always means harder gameplay, which leads to inaccurate difficulty labels.
- Counting all possible solutions instead of stopping at two, which wastes significant CPU time.
- Skipping seeded test cases, making generator regressions hard to reproduce.
Summary
- Build a complete solved board first with randomized backtracking.
- Remove clues incrementally and keep each removal only if solution count remains one.
- Use a dedicated counting solver with an early stop at two solutions.
- Treat uniqueness and difficulty as separate concerns in your pipeline.
- Pre-generate and validate puzzles for predictable production behavior.
Related reading
- How to generate the power-set of a given List?
- How to get a specific sequence like this?
- How to get access of individual trees of a xgboost model in python /R
- How to get all algebraic associative operations on a finite set by efficient algorithm?
- How to get all possible 2N combinations of a list’s elements, of any length
- How to get all possible combinations from two arrays?
- How to get all the possible 3 letter permutations?
- How to get better at solving Dynamic programming problems

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.