# generate valid sudoku board python

```base  = 3
side  = base*base

# pattern for a baseline valid solution
def pattern(r,c): return (base*(r%base)+r//base+c)%side

# randomize rows, columns and numbers (of valid base pattern)
from random import sample
def shuffle(s): return sample(s,len(s))
rBase = range(base)
rows  = [ g*base + r for g in shuffle(rBase) for r in shuffle(rBase) ]
cols  = [ g*base + c for g in shuffle(rBase) for c in shuffle(rBase) ]
nums  = shuffle(range(1,base*base+1))

# produce board using randomized baseline pattern
board = [ [nums[pattern(r,c)] for c in cols] for r in rows ]

for line in board: print(line)

[6, 2, 5, 8, 4, 3, 7, 9, 1]
[7, 9, 1, 2, 6, 5, 4, 8, 3]
[4, 8, 3, 9, 7, 1, 6, 2, 5]
[8, 1, 4, 5, 9, 7, 2, 3, 6]
[2, 3, 6, 1, 8, 4, 9, 5, 7]
[9, 5, 7, 3, 2, 6, 8, 1, 4]
[5, 6, 9, 4, 3, 2, 1, 7, 8]
[3, 4, 2, 7, 1, 8, 5, 6, 9]
[1, 7, 8, 6, 5, 9, 3, 4, 2]
```

Here is what the above code is Doing:
1. It defines a function pattern(r,c) that returns the number in the rth row and cth column of a valid sudoku board.
2. It defines a function shuffle(s) that returns a shuffled version of the list s.
3. It defines a list rows that contains all of the rows of the board in a random order.
4. It defines a list cols that contains all of the columns of the board in a random order.
5. It defines a list nums that contains all of the numbers of the board in a random order.
6. It defines a list board that contains the board with the numbers in the rows and columns in the order specified by rows and cols.
7. It prints the board.