File Exchange

image thumbnail

Sudoku Generator

version (18.3 KB) by Bradley Knockel
This code will try to produce the hardest Sudoku it can.


Updated 12 Jul 2013

View License

MATLAB files
• SudokuSolver.m - use this with MATLAB* to solve Sudokus
• SudokuMaker.m - use this with MATLAB* to make Sudokus
• publisher.m - use this with MATLAB* to make .png files of your Sudokus so that they can be printed and published

• numbers.png - file used by publisher.m
• suma.m - a function called by SudokuMaker.m
• other .m files - needed by both SudokuSolver.m and SudokuMaker.m

*Octave, a free imitator of MATLAB, can also "work"

my goal
The sole purpose of this code is not to make some flashy software or a tool for giving a single hint on how to solve a Sudoku but to make the best code for making the most difficult Sudokus. My initial goal was to make the ABSOLUTE hardest Sudokus, but I soon only cared to make the quality so high (people can spend many years on this task), so you may not be able to make the world's hardest Sudoku, but they will get very close. Also, do not get upset if SudokuSolver.m tells you that a Sudoku that is claimed to be very difficult by the author is easy. This can be especially frustrating when you yourself cannot solve it. This happens because VERY difficult Sudokus are the target audience of my solver. I must say that there is one thing I did that was not intended to make the most difficult Sudokus: I added a feature to allow the created Sudokus to have various symmetries, even though these symmetries restrict the Sudoku maker from making the hardest Sudokus.

Because I value GOOD algorithms, whenever I had to choose between speed and quality, I chose quality (don't worry... it's still plenty quick). For example, when resorting to guessing the number to some squares to solve a Sudoku, my code will try advanced algorithms instead of simple ones to test each guess. This slows the code down, but it allows for a more meaningful interpretation of the "number of guesses" value and the "depth of thread" value. See SudokuSolver.m for an explanation of these variables.

I made some very hard Sudokus. I have put the seeds that generated them in SudokuMaker.m. Good luck trying to solve them! Just don't use my code to do it because that will take, at most, a few seconds.

This code is under the BSD license, so you can mostly do what you want with it. But! If you by blind luck use this code to create and publish the world's hardest Sudoku, please give me, Bradley Knockel, some credit (money is also good!).

I've done my best to make the code readable, so, if you are curious how Sudokus can be generated and solved with computers, feel free to look at it.

a possible future for this code
*** see readme.html for this section ***

more info
• Release date of this version: July 2013
• Version of MATLAB I used: R2006b
• If you find bugs or have questions, leave me a message at

Many thanks to...

Comments and Ratings (2)

Jane Han


Anders (view profile)


• Made a simple record option to save output to file (useful for creating large batches)
• In SudokuSolver.m, you can now easily rerun a Sudoku many times (useful for testing the difficulty of very hard Sudokus)
• Documentation is updated

-added a "number of guesses" value in addition to "depth of thread"
-sped up code
-cleaned up code
-improved documentation

• →
• added a screenshot
• publisher.m can now easily change filename

Since rand('state',seed) is deprecated in newer versions of MATLAB, suma.m now uses a try/catch block that first attempts the new RandStream approach. Also, suma.m now terminates when TimeLimits(1) is met (instead of trying again).

Besides minor editing of comments, I replaced length(y) with numel(y) in su.m because of how the length() function in later versions of MATLAB acts on 3-dimensional arrays.

I removed my own BSD license file since adds one of their own.

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor