<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj" -->

---
title: How to generate a valid and fast Sudoku board from scratch?
description: A walkthrough of designing an algorithm to generate a valid, uniquely solvable Sudoku board from scratch, written from the perspective of a Masters student...
canonical: https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to generate a valid and fast Sudoku board from scratch? | daily.dev
og:description: A walkthrough of designing an algorithm to generate a valid, uniquely solvable Sudoku board from scratch, written from the perspective of a Masters student...
og:url: https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj
og:image: https://api.daily.dev/og/posts/kbzkLADlj.png
og:image:alt: How to generate a valid and fast Sudoku board from scratch?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to generate a valid and fast Sudoku board from scratch?

**[Yagiz Nizipli](https://daily.dev/sources/yagiz)** · 4 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of designing an algorithm to generate a valid, uniquely solvable Sudoku board from scratch, written from the perspective of a Masters student tackling a Software Engineering homework assignment. The approach is split into two steps: first generating a fully solved board by iterating through squares and randomly filling valid positions while backtracking on dead ends, then removing cells one at a time from that solution while verifying at each step that only one solution remains, to produce a playable puzzle with a unique answer.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.yagiz.co/sudoku-generating-valid-one>

## Questions this post answers

### How do you generate a Sudoku board that has a unique solution?

Generate a fully solved board first by iterating through each square and filling cells with random valid values, backtracking with a forbidden-positions list whenever a branch leads to a dead end. Then remove cells one at a time from that solved board, checking after each removal whether more than one solution exists; if so, undo the removal and mark that cell visited before moving on.

_Developers building puzzle generators can find more algorithm walkthroughs like this one on daily.dev._

### What algorithm can I use to fill a Sudoku grid with valid random values?

A breadth-first search style approach works: iterate through positions within each square, find all available valid positions for the current cell, filter them against previously placed values in the same iteration and against a forbidden-positions list, then randomly select from what remains. If no valid position exists, backtrack by adding the last placed value to the forbidden list and re-iterating the previous step.

_Anyone implementing backtracking search for grid puzzles can explore similar algorithm breakdowns on daily.dev._

## Similar posts on daily.dev

- [From constraint models to playable puzzle games](https://daily.dev/posts/from-constraint-models-to-playable-puzzle-games-f3qpshgb8) · Lobsters · 0 upvotes · 0 comments
- [How I Finally Learnt to Solve Coding Interview Questions](https://daily.dev/posts/how-i-finally-learnt-to-solve-coding-interview-questions-rxrmxxy2d) · DEV · 18 upvotes · 0 comments
- [Nonograms: a practical guide with interactive examples](https://daily.dev/posts/nonograms-a-practical-guide-with-interactive-examples-8nztxbal2) · Hacker News · 0 upvotes · 0 comments
- [Tupo](https://daily.dev/posts/tupo-rvnhxznra) · Dave Rupert · 0 upvotes · 0 comments

---

Tags: [#algorithms](https://daily.dev/tags/algorithms), [#computer-science](https://daily.dev/tags/computer-science)

[View this post on daily.dev](https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to generate a valid and fast Sudoku board from scratch?","url":"https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj"},"datePublished":"2026-09-13T19:57:41.810Z","dateModified":"2026-09-13T20:00:02.393Z","description":"A walkthrough of designing an algorithm to generate a valid, uniquely solvable Sudoku board from scratch, written from the perspective of a Masters student...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/8d1e2460f13bec2d706aec730cdb5655?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/8d1e2460f13bec2d706aec730cdb5655?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Yagiz Nizipli","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Yagiz Nizipli","logo":"https://media.daily.dev/image/upload/s--9Cscmaqk--/f_auto,q_auto/v1789329427/logos/yagiz?_a=BAMAMicg0","url":"https://daily.dev/sources/yagiz"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"algorithms,computer-science","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Yagiz Nizipli","item":"https://daily.dev/sources/yagiz"},{"@type":"ListItem","position":3,"name":"How to generate a valid and fast Sudoku board from scratch?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-generate-a-valid-and-fast-sudoku-board-from-scratch--kbzkladlj#faq","mainEntity":[{"@type":"Question","name":"How do you generate a Sudoku board that has a unique solution?","acceptedAnswer":{"@type":"Answer","text":"Generate a fully solved board first by iterating through each square and filling cells with random valid values, backtracking with a forbidden-positions list whenever a branch leads to a dead end. Then remove cells one at a time from that solved board, checking after each removal whether more than one solution exists; if so, undo the removal and mark that cell visited before moving on. Developers building puzzle generators can find more algorithm walkthroughs like this one on daily.dev."}},{"@type":"Question","name":"What algorithm can I use to fill a Sudoku grid with valid random values?","acceptedAnswer":{"@type":"Answer","text":"A breadth-first search style approach works: iterate through positions within each square, find all available valid positions for the current cell, filter them against previously placed values in the same iteration and against a forbidden-positions list, then randomly select from what remains. If no valid position exists, backtrack by adding the last placed value to the forbidden list and re-iterating the previous step. Anyone implementing backtracking search for grid puzzles can explore similar algorithm breakdowns on daily.dev."}}]}
```

