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


use strict;
use warnings;
use feature 'say';
use List::Util 'shuffle';
 
sub random_ls {
    my($n) = @_;
    my(@cols,@symbols,@ls_sym);
 
    # build n-sized latin square
    my @ls = [0,];
    for my $i (1..$n-1) {
        @{$ls[$i]} = @{$ls[0]};
        splice(@{$ls[$_]}, $_, 0, $i) for 0..$i;
    }
 
    # shuffle rows and columns
    @cols = shuffle 0..$n-1;
    @ls = map [ @{$_}[@cols] ], @ls[shuffle 0..$n-1];
 
    # replace numbers with symbols
    @symbols = shuffle( ('A'..'Z')[0..$n-1] );
    push @ls_sym, [@symbols[@$_]] for @ls;
    @ls_sym
}
 
sub display {
    my $str;
    $str .= join(' ', @$_) . "\n" for @_;
    $str
}
 
say display random_ls($_) for 2..5, 5;