A Latin square of size n is an arrangement of n symbols in an n-by-n square in such a way that each row and column has each symbol appearing exactly once.

A randomised Latin square generates random configurations of the symbols for any given n.

Example n=4 randomised Latin square

0 2 3 1
2 1 0 3
3 0 1 2
1 3 2 0
Task

 Create a function/routine/procedure/method/... that given n generates a randomised Latin square of size n.
 Use the function to generate and show here, two randomly generated squares of size 5.
Note
 
Strict Uniformity in the random generation is a hard problem and not a requirement of the task.

Reference

 Wikipedia: Latin square
 OEIS: A002860


F _transpose(matrix)
   assert(matrix.len == matrix[0].len)
   V r = [[0] * matrix.len] * matrix.len
   L(i) 0 .< matrix.len
      L(j) 0 .< matrix.len
         r[i][j] = matrix[j][i]
   R r
 
F _shuffle_transpose_shuffle(matrix)
   V square = copy(matrix)
   random:shuffle(&square)
   V trans = _transpose(square)
   random:shuffle(&trans)
   R trans
 
F _rls(&symbols)
   V n = symbols.len
   I n == 1
      R [symbols]
   E
      V sym = random:choice(symbols)
      symbols.remove(sym)
      V square = _rls(&symbols)
      square.append(copy(square[0]))
      L(i) 0 .< n
         square[i].insert(i, sym)
      R square
 
F rls(n)
   V symbols = Array(0 .< n)
   V square = _rls(&symbols)
   R _shuffle_transpose_shuffle(square)
 
F _check_rows(square)
   V set_row0 = Set(square[0])
   R all(square.map(row -> row.len == Set(row).len & Set(row) == @set_row0))
 
F _check(square)
   V transpose = _transpose(square)
   assert(_check_rows(square) & _check_rows(transpose), ‘Not a Latin square’)
 
L(i) [3, 3, 5, 5]
   V square = rls(i)
   print(square.map(row -> row.join(‘ ’)).join("\n"))
   _check(square)
   print()