# How do i generate random numbers that are a valid sudoku board?

Page 1 of 1

## 8 Replies - 5249 Views - Last Post: 14 December 2012 - 12:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303804&amp;s=1cf259f8b0a6e91e30b27346c241c320&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 meghanxx8

Reputation: 0
• Posts: 2
• Joined: 12-December 12

# How do i generate random numbers that are a valid sudoku board?

Posted 12 December 2012 - 09:53 PM

I have the board set up, but I dont know how to place random, but also valid numbers in the board.

```void display_board()
{
const int SCREEN_NUM_ROW = 20;

const char GONE_FILL = 'X';

string screen_board[SCREEN_NUM_ROW];
screen_board[0] =  "   | A   B   C | D   E   F | G   H   I |";
screen_board[1] =  " ---------------------------------------";
screen_board[2] =  " 1 |   /   /   |   /   /   |   /   /   |";
screen_board[3] =  "   -------------------------------------";
screen_board[4] =  " 2 |   /   /   |   /   /   |   /   /   |";
screen_board[5] =  "   -------------------------------------";
screen_board[6] =  " 3 |   /   /   |   /   /   |   /   /   |";
screen_board[7] =  " ---___________-___________-___________-";
screen_board[8] =  " 4 |   /   /   |   /   /   |   /   /   |";
screen_board[9] =  "   -------------------------------------";
screen_board[10] = " 5 |   /   /   |   /   /   |   /   /   |";
screen_board[11] = "   -------------------------------------";
screen_board[12] = " 6 |   /   /   |   /   /   |   /   /   |";
screen_board[13] = " ---___________-___________-___________-";
screen_board[14] = " 7 |   /   /   |   /   /   |   /   /   |";
screen_board[15] = "   -------------------------------------";
screen_board[16] = " 8 |   /   /   |   /   /   |   /   /   |";
screen_board[17] = "   -------------------------------------";
screen_board[18] = " 9 |   /   /   |   /   /   |   /   /   |";
screen_board[19] = " ---___________-___________-___________-";
screen_board[20] = "                                        ";

```

that's my board.

don't mind the "GONE_FILL .. " etc. .. that's not supposed to be there (:

Is This A Good Question/Topic? 0

## Replies To: How do i generate random numbers that are a valid sudoku board?

### #2 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 12 December 2012 - 10:06 PM

There is a rand() function in <cstdlib> but it generates numbers from a wide range and as you need to receive only numbers between 1 and 9 including those two , you can apply the modulus operator like this:
rand() % 9 + 1 .
rand() -> provides a random number
rand() % 9 -> provides a random number between 0 and 8
rand() % 9 + 1 -> provides a random number between 1 and 9

I hope this clears it up .

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 12 December 2012 - 10:42 PM

There are two easy ways to do this, and a hard way:

1) Easy way: Get a large file of either solved Sudoku boards, or of known good puzzles, and randomly choose the current puzzle, from that list.

This is way easy because it eliminates the considerable problem of a puzzle which has more than 1 solution. Some puzzles have thousands of correct solutions! This may not may not be a problem with your program.

2) Easy way: Get one of the programs made for this already. You can find them linked or listed, on the Sudoku Programers forum.

Trust me, these guys have forgotten more about programming Sudoku, than you or I will probably ever learn.

3) Hard way: put in random numbers, but use the following tests to create a "probably" good puzzle:

Each row and each column, AND each "house" must have a sum of 9+8+7+6+5+4+3+2+1. So any single test that you make on a board being considered that fails that test, is bogus, AND you know no further tests on that board are needed.

Save that board, and when you have say 10,000 of them, run a fast solver over them and set the solver to tell you which ones, have a single solution.

That fast solver that will tell you if the solution is unique, is also linked at the Sudoku Programers forum.

The link for that forum is:
http://www.setbb.com...p?mforum=sudoku

There is also a Sukodu players forum, with a lot of the same people, and much busier, so you might ask there, as well.

One note of caution: more of the puzzles on these forums tend to be hard or very hard. Too hard for most human solvers to enjoy working with. These aren't the type you see in the Sunday newspaper, generally.

This post has been edited by Adak: 12 December 2012 - 10:42 PM

### #4 meghanxx8

Reputation: 0
• Posts: 2
• Joined: 12-December 12

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 13 December 2012 - 01:08 AM

TwoOfDiamonds, on 12 December 2012 - 10:06 PM, said:

There is a rand() function in <cstdlib> but it generates numbers from a wide range and as you need to receive only numbers between 1 and 9 including those two , you can apply the modulus operator like this:
rand() % 9 + 1 .
rand() -> provides a random number
rand() % 9 -> provides a random number between 0 and 8
rand() % 9 + 1 -> provides a random number between 1 and 9

I hope this clears it up .

Thanks! but how would i randomize where on the board the numbers appear without duplicating?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 13 December 2012 - 04:49 AM

Right there, you have your very first question for the Sudoku Programmers forum! <smile!>

LOL!

Seriously, I believe that kind of random placement of random numbers, is going to play hell with finding a puzzle in a short amount of time. SHORT of course, depends on whether you're making up one puzzle, or 50,000 puzzles, of course.

I like to knock off the random stuff as much as possible. Start with 27 squares (1/3rd of a board), and keep in mind that every row, every column and every house, must sum up to 45, and every row col and house, have to have one of every digit.

So, you could put ALL the digits on every row, making sure you had one of each for every column, at the same time. Now swap the squares values around, until you find the right combination.

Keeping a running sum of each row, col, and house, will help save a lot of time.

I've written Sudoku solvers, but not Sudoku creators, so beyond this, I'd have to tinker a good deal to see what made sense - which I don't have time to do right now.

But the guys on the Sudoku Programming Forum have written several creators, and probably can rattle off a far better answer than I can, in zip time.

Have fun!

This post has been edited by Adak: 13 December 2012 - 04:50 AM

### #6 baavgai

• Dreaming Coder

Reputation: 6574
• Posts: 13,906
• Joined: 16-October 07

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 13 December 2012 - 05:29 AM

I don't know about summing. If you're doing truly random, it probably won't work.

There are three checks that need to be made on any given digit. Is it the only one in the row, only one in the column, and only one in the box. That's pretty much it.

Rather than doing completely random, do random available. That is, pick a digit from a list of valid values. If there are no valid values, then you have reached a dead end.

### #7 azeem abbas

Reputation: -2
• Posts: 9
• Joined: 13-December 12

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 14 December 2012 - 05:42 AM

the rand function does not provide random number more randomly but using srand with it will generate random number more accurately.this is my blog which explains how to generate random number.
http://softwares-mar...umber-in-c.html

### #8 GWatt

Reputation: 305
• Posts: 3,100
• Joined: 01-December 05

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 14 December 2012 - 12:04 PM

Start with a valid board. You can pick two numbers at random and swap all of those numbers on the board. Repeat until happy. The trick is removing numbers so that there is only one solution.

edit: i should add, that of all the methods listed in this thread, mine is the only one that is incapable of generating an invalid sudoku arrangement. all of the others will involve a guess and check when attempting to fill the board, as well as a fair amount of back tracking.

This post has been edited by GWatt: 14 December 2012 - 12:22 PM

### #9 baavgai

• Dreaming Coder

Reputation: 6574
• Posts: 13,906
• Joined: 16-October 07

## Re: How do i generate random numbers that are a valid sudoku board?

Posted 14 December 2012 - 12:39 PM

To create a valid sudoku board, start with a valid sudoku board... Yep, that logically cuts down on the work.