Sudoku generator algorithm
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 really two algorithms combined: one to build a complete valid board and another to remove numbers while preserving a single solution. If either part is weak, the result is a broken puzzle, a duplicate pattern, or a grid that is technically valid but not interesting to solve.
Step 1: Generate a Full Valid Board
The most common approach is randomized backtracking. Start with an empty 9 x 9 grid, try digits in random order, and recurse until the board is full. If a choice causes a contradiction, undo it and try the next value.
This works well because Sudoku constraints are local and easy to check. The algorithm is simple, deterministic once you fix the random seed, and reliable enough for production puzzle generation.
At the end of fill_board, you have a solved Sudoku grid. That is not yet a puzzle; it is only the answer key.
Step 2: Remove Clues While Preserving Uniqueness
Puzzle generation starts by removing entries from the solved board. After each removal, you need to check whether the board still has exactly one solution. If the puzzle now has two or more solutions, restore that value and try another cell.
The easiest way to test uniqueness is to run a solver that counts solutions but stops once it finds more than one.
This generator is simple but correct: it produces a solved board, removes values, and enforces uniqueness after each attempted removal.
Difficulty Is Harder Than Clue Count
Many beginners assume fewer clues always means a harder puzzle. That is not reliable. Some puzzles with 28 clues are easy because the next move is obvious, while some puzzles with 32 clues require more advanced reasoning.
If you care about human difficulty, you need a rating system based on solving techniques. A practical generator often removes clues first, then runs a human-style solver that measures whether the puzzle can be completed using strategies such as singles, hidden singles, pairs, or more advanced deductions. That is separate from uniqueness and should be treated as a second validation stage.
Symmetry is another design choice. Many published Sudoku puzzles remove clues in rotationally symmetric pairs because the board looks cleaner. Symmetry is not required for correctness, but it often improves presentation.
Common Pitfalls
Generating a solved board is not the same as generating a good puzzle. You still need clue removal and uniqueness checks.
Removing numbers without counting solutions often creates puzzles with multiple valid answers. That breaks the core expectation of Sudoku.
Using clue count as the only difficulty metric produces poor difficulty labels. Human solving complexity matters more than raw clue count.
Random generation without validation can make many boards look similar. Shuffle candidate digits and cell order to improve variety.
Summary
- A Sudoku generator usually uses randomized backtracking to create a complete valid board.
- Puzzle creation happens afterward by removing clues one at a time.
- After each removal, a solver should confirm that exactly one solution remains.
- Difficulty is a separate concern from uniqueness and usually needs a human-style rating pass.
Related reading
- Suffix Array Algorithm
- Suffix array nlogn creation
- Suffix tree and Tries. What is the difference?
- Suggest an algorithm graph - possibly NP-Complete
- Suggest websites to practice C/C algorithms/puzzles
- Suggested algorithms/methods for laying out labels on an image
- Suggestions to learn distributed algorithms involving multi-processes for a beginner
- Sum-subset with a fixed subset size

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.