Sudoku
Algorithm
Puzzle Generation
Programming
Closed Discussion

Sudoku generator algorithm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

python
1import random
2
3SIZE = 9
4DIGITS = list(range(1, 10))
5
6
7def valid(board, row, col, value):
8    if any(board[row][c] == value for c in range(SIZE)):
9        return False
10    if any(board[r][col] == value for r in range(SIZE)):
11        return False
12
13    box_row = (row // 3) * 3
14    box_col = (col // 3) * 3
15    for r in range(box_row, box_row + 3):
16        for c in range(box_col, box_col + 3):
17            if board[r][c] == value:
18                return False
19    return True
20
21
22def fill_board(board):
23    for row in range(SIZE):
24        for col in range(SIZE):
25            if board[row][col] == 0:
26                choices = DIGITS[:]
27                random.shuffle(choices)
28                for value in choices:
29                    if valid(board, row, col, value):
30                        board[row][col] = value
31                        if fill_board(board):
32                            return True
33                        board[row][col] = 0
34                return False
35    return True

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.

python
1def count_solutions(board, limit=2):
2    count = 0
3
4    def solve():
5        nonlocal count
6        if count >= limit:
7            return
8
9        for row in range(SIZE):
10            for col in range(SIZE):
11                if board[row][col] == 0:
12                    for value in DIGITS:
13                        if valid(board, row, col, value):
14                            board[row][col] = value
15                            solve()
16                            board[row][col] = 0
17                    return
18        count += 1
19
20    solve()
21    return count
22
23
24def generate_puzzle(clues=30):
25    board = [[0] * SIZE for _ in range(SIZE)]
26    fill_board(board)
27
28    cells = [(r, c) for r in range(SIZE) for c in range(SIZE)]
29    random.shuffle(cells)
30
31    for row, col in cells:
32        saved = board[row][col]
33        board[row][col] = 0
34
35        remaining = sum(value != 0 for line in board for value in line)
36        if remaining < clues or count_solutions(board) != 1:
37            board[row][col] = saved
38
39    return board

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.

Course illustration
Course illustration

All Rights Reserved.