How a Sudoku Generator Makes a Puzzle With One Answer : Inside SudokuGen

Check links

Open our SudokuGen web app, set Rank to 3, slide Blanks all the way to the right, type 2026 as the seed and press Generate. You get a 9×9 puzzle with 24 numbers and 57 blank squares. Type the same seed tomorrow, on another computer, and you get the same 24 numbers in the same squares. Ask our C# library for the same puzzle and it gives you that one too. And however you approach it, there is exactly one way to fill the 57 blanks.

Pixel art on a starry night sky: a 9×9 sudoku grid of cyan and purple numbered tiles with a few squares still empty, and three yellow tiles numbered 3, 2 and 6 falling into the empty squares of the top row

This article explains how that works, for curious players and for programmers. A computer can make sudoku puzzles all day, but a good generator has to be sure every time that the puzzle has one answer and only one. Our own open-source generator, SudokuGen, is the worked example, and every step below is what its code really does. First comes why one answer matters. Then we follow a puzzle as it is made: filling a complete grid, taking numbers away while the answer stays unique, and a final shuffle. After that come seeds, which let C# and JavaScript make the same puzzle, and last the limits: difficulty, and the time big puzzles take.

One puzzle, one answer

A sudoku is a 9×9 grid split into nine 3×3 boxes, and the goal is to fill every empty square so that each row, each column and each box holds the digits 1 to 9 once. One more rule is usually understood rather than printed. In the words of Gary McGuire, Bastian Tugemann and Gilles Civario, who settled how few numbers a sudoku can give, "any proper (valid) sudoku puzzle must have only one completion." A solver should be able to reason every number into place, and never reach a point where two answers are equally good.

Take away too many numbers and that rule breaks. The smallest way it can happen is shown below. Look for two rows in the same band, the same row of three boxes, that hold the same two digits in the same two columns, crossed over: here a 5 and a 2 in row 1, and a 2 and a 5 in row 2. Swap the four digits and you get a second complete grid that still follows every rule. So a puzzle that leaves all four of these squares blank has two solutions, even if all the other 77 numbers are given. Solvers call this a deadly pattern.

Two complete 9×9 sudoku grids side by side, identical except for four shaded squares in rows 1 and 2, columns 1 and 5: the left grid has 5 and 2 over 2 and 5, the right grid has them swapped, and both grids follow every rule
The grid SudokuGen makes from seed 2026. Swap the 2s and 5s in the four shaded squares and you get a second grid that is just as valid

McGuire and his colleagues call any group of squares like this an unavoidable set: its numbers can be rearranged into another valid grid, so every proper puzzle must give at least one of them. The grid above has twelve of the four-square kind alone, and the puzzle SudokuGen made from it gives at least one number in each of them. In the deadly pattern shown here, it is the 5 in row 2, column 5. Some solving techniques even rely on this: since a proper puzzle can't end in a deadly pattern, any move that would create one must be wrong.

How few numbers can a proper puzzle give? Seven are never enough: a puzzle with 7 numbers leaves out at least two digits completely, and those two can be swapped everywhere in any solution. Beyond that, the answer took computers. Puzzles with 17 numbers were long known, and nobody had found one with 16. In 2012 McGuire, Tugemann and Civario settled the question with an exhaustive computer search: no 16-clue sudoku exists. There were a lot of grids to search. Bertram Felgenhauer and Frazer Jarvis had counted the complete 9×9 grids in 2005: 6,670,903,752,021,072,936,960 of them, about 6.67 sextillion. Ed Russell and Jarvis then showed that only 5,472,730,538 are essentially different; the rest are the same grids with their rows, columns or digits rearranged. The team searched one grid from each group, about 800 processor-years of computing. As they put it, "had one existed, we would have found it."

Step 1: a complete grid

SudokuGen does not start from a puzzle. It starts from the answer: it first makes a complete, solved grid, then takes numbers away from it. A complete grid is easy to make at random, and every puzzle cut from it has at least that one answer.

For a 9×9 or larger grid, it begins with the boxes on the diagonal, which on a 9×9 are the top left, center and bottom right ones. They share no row and no column, so each one can get a random arrangement of 1 to 9 without any risk of a clash. Its solver fills in the rest. The solver is a backtracking search: it picks a blank square, tries a number, moves on, and steps back to try another number when it reaches a square where nothing fits. Two habits keep it fast. It usually works on the square with the fewest options left, and it places every number that is forced before it guesses anything. A number is forced when it is the only one that fits a square, or when a row, column or box has only one square left for it. To make the grid random, the solver tries numbers in a shuffled order, with a different order for each row.

Two 9×9 grids. Left: only the three diagonal boxes are filled, shaded yellow. Right: the same grid completed, with the numbers the search added in blue
Seed 2026, step 1: three random boxes on the diagonal, then the search fills in the blue numbers. The final shuffle (step 3) rearranges this grid before the web app shows it

For seed 2026, the search placed the other 54 numbers without a single wrong guess. That is typical for a 9×9: of the first 1,000 seeds, 930 filled their grid without stepping back even once.

Step 2: taking numbers away

Next, SudokuGen puts all 81 squares in a random order and visits them one at a time. It blanks each square, then checks whether the puzzle still has only one answer. If it does, the square stays blank. If not, the number goes back, and that square keeps its number for good. It stops when it reaches the number of blanks you asked for, or when it has visited every square.

The check is the clever part. The obvious way to test a puzzle is to count all its solutions and see whether the count is one, but SudokuGen never needs a full count. The puzzle had one answer a moment ago, so if blanking this square lets in a second answer, that answer must have a different number in this square. So SudokuGen tries each other number that fits there, one that isn't already in the square's row, column or box, and asks the solver whether the rest of the grid can still be completed. The first time it can, the check stops: a second answer exists, and the number goes back. Only when no other number works does the square stay blank.

Left: a 9×9 puzzle in progress with 32 numbers, one blank square in row 2, column 8 shaded yellow. Right: a complete grid with a 3 in that square; the given numbers are black, the solver's numbers blue, and the 14 other squares that differ from the real answer are shaded red
Seed 2026, step 2: the 49th square visited held a 6. With it blank, a 3 fits too, and the solver completes a second grid that differs from the real answer there and in the 14 red squares, so the 6 goes back. Shown before the final shuffle

For seed 2026, the first 48 squares SudokuGen visited could all be blanked; the 49th could not. The second grid it found agrees with every number still on the board, and the 15 squares where it differs are a bigger cousin of the deadly pattern.

The same solver checks puzzles that come from elsewhere. To solve a puzzle, SudokuGen first finds one answer, trying the numbers in order. Then it searches again with the order rotated, so that in every square the number from the first answer is tried last. That second search reaches any other answer before it can reach the first one. If it arrives back at the first answer, there is no other, and the puzzle is proper. Otherwise SudokuGen reports that the puzzle has more than one solution.

The Blanks setting

You ask for a number of blanks, and SudokuGen gives you that many if it can. With seed 2026, Blanks 45 gives exactly 45. Ask for more than a grid allows and you get fewer: a 9×9 runs out of squares it can blank at around 56 or 57. In the web app, Blanks at the far right asks for every square, which is the same as asking the library for as many blanks as possible.

For each seed, the squares are visited in one fixed order, so the same seed makes puzzles that fit inside each other. The 57-blank puzzle from seed 2026 is the 45-blank puzzle with 12 more numbers taken away, and both have the same answer.

"As many as possible" does not mean as few numbers as possible. Each square gets only one visit, so the result depends on the order. Across 2,000 seeds, the 9×9 puzzles SudokuGen made with as many blanks as possible kept between 21 and 28 numbers, most often 23 to 25: close to the "around 25 clues" that, according to McGuire's team, newspapers and magazines usually give, and well above 17. What you do get is a minimal puzzle: take away any one of the remaining numbers and a second answer appears. A number that had to stay when its square was visited still has to stay at the end, because taking more numbers away can only add answers, never remove them. On the small 4×4 grid, where the smallest possible puzzle has 4 numbers, SudokuGen reaches that minimum for 1,391 of the first 2,000 seeds.

One more rule keeps big grids quick. Each number tried in a check gets a budget of ten search steps for every square of the grid: 810 steps on a 9×9, 2,560 on a 16×16 and 6,250 on a 25×25. If the solver runs out before it can tell, SudokuGen plays safe and puts the number back. That can never let a second answer in; it can only leave a few more numbers than strictly needed. On 9×9 grids it happened just once in our tests of 1,000 seeds, to a number that had to go back anyway, but with as many blanks as possible, about 16 of the 256 checks on a 16×16 run out of steps, and about 270 of the 625 on a 25×25. So on big grids a minimal puzzle is no longer guaranteed, although the 16×16 puzzles from seeds 1 and 2 turned out minimal anyway.

The exhaustive option removes the budget, so every check runs until it knows. On big grids that can take much longer: on our desktop computer, an exhaustive 25×25 with as many blanks as possible was still running after 25 minutes, where the normal run takes a few seconds. It does not always mean more blanks, either. A square blanked early can force others to stay later, so the same seed can end with a few more blanks or a few fewer. For a 16×16 with seed 1, the normal run blanked 164 squares and the exhaustive run 161; with seed 2, it was 164 and 165. On 9×9 grids the two gave identical puzzles for all 300 seeds we tried.

Step 3: the final shuffle

Before it hands the puzzle over, SudokuGen shuffles it with moves that can never break a rule. It reorders the three bands of boxes and the rows inside each band, does the same for the stacks and the columns, renames the digits, and sometimes flips the whole grid across its diagonal. The fill always starts from the diagonal boxes and follows the solver's habits, and that could leave a pattern in where numbers sit. The shuffle hides it.

Two 9×9 puzzles with 24 numbers each. The three yellow boxes, on the diagonal before the shuffle, sit in the top right, middle left and bottom middle after it, and a line below lists how the digits were renamed, 1 to 5, 2 to 3 and so on
Seed 2026, step 3: the puzzle before and after the final shuffle. The yellow boxes are the three the fill started from. The right side is what the web app shows for Rank 3, Blanks 81 and Seed 2026

A shuffled puzzle is the same puzzle in disguise. Every move takes rows to rows, columns to columns and boxes to boxes, so every step of logic in one puzzle has a matching step in the other. The shuffled puzzle has the same number of blanks, still exactly one answer, and the same difficulty. The library offers the same shuffle on its own, to turn any puzzle into a fresh-looking twin.

These are the same moves mathematicians use to call two grids essentially the same. Russell and Jarvis count 3,359,232 ways to rearrange a grid's rows, columns, bands and stacks, with or without a flip, and there are 362,880 ways to rename nine digits. Together that makes about 1.2 trillion versions of a single grid, and almost all grids have no symmetry that would make two versions identical. That is how 6.67 sextillion grids shrink to 5.47 billion essentially different ones, and SudokuGen's shuffle picks one of those trillion-odd versions at random.

Seeds: the same puzzle in C# and JavaScript

A computer's random numbers are not really random. A pseudo-random generator turns a starting number, the seed, into a long sequence that only looks random, and the same seed always gives the same sequence. Every random choice SudokuGen makes comes from one such generator, drawn in a fixed order: the diagonal boxes, the order of numbers for each row, the order of the squares to blank, and the shuffle. So the same seed, size, number of blanks and exhaustive option always give the same puzzle.

Getting the same puzzle in two languages takes more care. The C# library uses the seeded generator built into .NET, which Microsoft describes as "a modified version of Donald E. Knuth's subtractive random number generator algorithm." JavaScript has no seeded generator built in: as MDN notes about Math.random, its seed "cannot be chosen or reset by the user." So the JavaScript library carries its own copy of the .NET generator, step for step, down to the way C# whole-number arithmetic wraps around.

Shared test data keeps the two in step. The C# library, the reference version, wrote out the numbers its generator draws for a list of seeds, and the puzzles, solutions and shuffles it makes from them. The tests in both languages must reproduce every one of them exactly. We also checked seed 2026 for this article: in C#, Sudoku.Create(3, 81, 2026), and in JavaScript, Sudoku.create(3, 81, { seed: 2026 }), both return the puzzle in the pictures. Microsoft does not promise that the seeded generator will stay the same across major versions of .NET; if it ever changes, those tests will show it at once.

Limits: difficulty and speed

SudokuGen does not rate how hard a puzzle is. It counts blanks, and that is all. Our game Shaolin Sudoku names its levels by blanks, 30 for Easy, 45 for Medium and up to 60 for Hard, and the web app shows no difficulty names at all.

Blanks are only a rough guide, because what makes a sudoku hard is the hardest step of logic it needs. The two basic steps are the two kinds of forced number from step 1: the naked single, a square where only one number fits, and the hidden single, a number with one square left in its row, column or box. Most puzzles rated easy or mild fall to these two alone. Harder ones need techniques such as pointing, pairs and chains. So a puzzle with few numbers can still be easy, and one with more numbers can be hard. In a 2014 study of more than 1,700 puzzles, each solved by many people online, Radek Pelánek found that the number of givens matched solving times only weakly, with correlations of 0.25 and 0.27 on two websites' data. Ratings built from the techniques a puzzle needs, such as the one in the program Sudoku Explainer, did far better, at 0.70 and 0.86.

SudokuGen's solver knows several of these techniques. It places naked and hidden singles, uses pointing and claiming to rule numbers out, and spots naked pairs. But it uses them to search faster, not to grade, and once they run out it simply guesses, which a computer can afford and a person usually can't. Rating a puzzle means solving it the way a person does, simplest technique first and without guessing, and noting the hardest step. That is a different job, and SudokuGen doesn't do it.

Speed is the other limit: the time grows far faster than the grid. With as many blanks as possible, on our desktop computer, a 9×9 takes about a millisecond, a 16×16 about a tenth of a second and a 25×25 a few seconds. Three things multiply. A bigger grid has more squares to visit: 81, then 256, then 625. Each check has more numbers to try: up to 8, 15 or 24 others. And each try searches a much bigger grid, which is far more likely to go deep before it can tell. Counted in search steps, those checks take about 2,400 steps on a 9×9, about 118,000 on a 16×16 and about 1.8 million on a 25×25, and each step on a 25×25 looks at 625 squares instead of 81.

Most of that time goes into the last few blanks. A 25×25 with 300 blanks takes about 20 milliseconds; the next 40 or 50, where nearly every check has to search deep, take the seconds. That is why our web app creates 16×16 and 25×25 puzzles in the background, so the page stays responsive.

Try it yourself

A sudoku generator makes a puzzle with one answer by working backward from the answer: it fills a complete grid, then takes numbers away and refuses every removal that would let a second answer in. The rest, the budget, the shuffle and the seed, is about doing that quickly and the same way every time. To see it for yourself, open the web app, set Rank to 3 and Blanks to the far right, type 2026 as the seed and press Generate, and you will see the puzzle from this article. Then move Blanks down, press Generate again and watch the numbers come back in the same squares.

Play in your browser

To use SudokuGen in your own C#, Unity or JavaScript project, or from the command line, read our SudokuGen article. Its code, tests and shared test data are on GitHub (opens in a new tab).

Built a generator of your own, or found a puzzle SudokuGen handles badly? Tell us

If you like this article,

Share