From the R library package “sudoku”:

generateSudoku package:sudoku R Documentation

Randomly Generate a Sudoku Puzzle Grid

Description:

```
Creates a 9x9 Sudoku grid suitable for use by 'playSudoku'.
```

Usage:

```
generateSudoku(Nblank=50, print.it=FALSE)
```

Arguments:

Nblank: Number of cells to blank out

print.it: Logical. If true, print result to screen.

Details:

```
The basic algorithm is to start with a 'primordial' Sudoku grid,
swap around some rows and columns, then blank out some cells.
```

Value:

```
A matrix, representing a 9x9 Sudoku grid.
```

Author(s):

```
Curt S. <removed_email_address@domain.invalid>, Henrik Bengtsson
<removed_email_address@domain.invalid>, and David Brahm <removed_email_address@domain.invalid>
```

References:

```
<URL: http://sudoku.com/>
```

Examples:

```
generateSudoku(print.it=TRUE)
```

Here’s the function definition for those who understand R. I might make

pseudo-code from this if I have the time:

generateSudoku <- function(Nblank=50, print.it=FALSE) {

z <-

c(1:9,4:9,1:3,7:9,1:6,2:9,1,5:9,1:4,8:9,1:7,3:9,1:2,6:9,1:5,9,1:8)

z <- matrix(sample(9)[z], 9,9)

for (i in 1:5) z <- z[replicate(3, sample(3)) + 3*rep(sample(0:2),*

each=3),

replicate(3, sample(3)) + 3rep(sample(0:2),

each=3)]

for (bi in seq(0,6,3)) for (bj in seq(0,6,3)) {

idx <- data.matrix(expand.grid(bi + 1:3, bj + 1:3))

z[idx[sample(1:9, Nblank%/%9), ]] <- 0

}

## Depopulate (if we had a test for uniqueness, we’d put it here):

while (sum(!z) < Nblank) z[matrix(sample(9,2), 1)] <- 0

if (print.it) printSudoku(z)

z

}

Meino Christian C. wrote:

–

M. Edward (Ed) Borasky

http://linuxcapacityplanning.com