Fill a n*n grid with n elements, one element per row, per column and one per diagonals I'm trying to think of an algorithm which can fill a n*n matrix with n elements. They need to be placed in a specific way, which is


*

*one, and only one element in every row

*one, and only one element in every column

*one, and only one element in both diagonals


The algorithm also needs to be "random".
I would love if someone could point me in the right direction, because so far not even google gave me relevant answers regarding my question.
 A: For $n\geq 6$, choose a square on a diagonal and place an element. If there is no no element on the other diagonal choose a square at random from the other diagonal that is not on the first. (There are at least five).
Then choose unoccupied rows at random so long as there at least two such and place an element on a non diagonal square chosen at random from columns that do not meet the chosen row in a diagonal square, nor already contain an element. This is always possible since any row contains at most two diagonal squares and the number of free rows is the same as the number of free columns.
For the last two rows, if there is an available square place an element. If not, there must at least two already placed non diagonal elements in columns that don't meet the row in a diagonal square (since at least four elements are already placed, at most two of which are diagonal). Choose such an element at random and shift it on its own row to an unoccupied column that meets the free row in a diagonal square but is not then on a diagonal square. (There must be at least one such free column, otherwise there would have been an available square in the first place, and at least one such move is possible because any column contains at most two diagonal squares, one of which is spoken for.) Then place the new element in the column that has been vacated. This gives a fill as specified.
If $n=0$ or $n=1$ then the specification is automatically met.
If $n=2$ then any placed element prohibits any others, so the arrangement is not possible.
If $n=3$ then there can be only one diagonal element at the centre and this also prohibits any others, so the arrangement is not possible.
If $n=4$ then the only way to place the diagonal elements is at (0,0) and (0,2) or some symmetrical arrangement thereof, when the other two can only be at (the positions symmetrical to) (0,3) and (3,2). So it's possible but randomness is limited to choosing the symmetry.
If $n=5$ then the cases can be split according as there are one or two diagonal elements.
If there is only one it must be at (2,2). The other elements can then be at any positions symmetrical to one of:
(1,0),(0,1),(4,3) and (3,4)
(1,0),(4,1),(3,4) and (0,4)
If there are two, they must be at positions symmetrical to (0,0) and (3,1). The other elements must then be at the positions symmetrical to one of:
(1,4),(2,3) and (4,2)
(1,2),(2,4) and (4,3)
So choose a random one from the above.
