# How to generate a $9 \times 9$ matrix like Sudoku ？

i want to generate a matrix that the number in each row, colum contains 1-9,and not repeated.
my code always fall into no solution. here is my code.

orix = RandomSample[Range[1, 9]];
oriy = Join[{orix[[1]]},
RandomSample[DeleteCases[Range[1, 9], orix[[1]]]]];
matrix = ConstantArray[0, {9, 9}];
matrix[[1]] = oriy;
matrix[[;; , 1]] = orix;
matrix // MatrixForm
Do[matrix[[x, y]] =
RandomChoice[
Range[0, 9] /.
Thread[Flatten[{matrix[[x]], matrix[[;; , y]]}] -> Nothing]], {x,
2, 9}, {y, 2, 9}]; matrix // MatrixForm


## 1 Answer

Thanks mr Moo ,Namjoshi ,and mikado the eligible matrix is much more than i thought. we can get a example matrix by:

matrix = ConstantArray[0, {9, 9}];
k = RotateRight[Range[9], 1];
Do[k = RotateLeft[k, 1];matrix[[i]] = k, {i, 9}]
matrix // MatrixForm


the other matrix can be got by changing the position of two numbers forexample /.{1->9,9->1}

• I do not see how this answers your question. Sudoku matrices are divided in 9 smaller matrices and they should have all numbers different. – yarchik Jul 26 at 13:57