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


with javascript_semantics
string aleph = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 
function ls(integer n)
    if n>length(aleph) then ?9/0 end if -- too big...
    atom t1 = time()+1
    sequence tn = tagset(n),     -- {1..n}
             vcs = repeat(tn,n), -- valid for cols
             res = {}
    integer clashes = 0
    while length(res)<n do
        sequence rn = {},               -- next row
                 vr = tagset(n),        -- valid for row (ie all)
                 vc = deep_copy(vcs)    -- copy (in case of clash)
        bool clash = false
        for c=1 to n do
            sequence v = {}
            for k=1 to n do
                -- collect all still valid options
                if vr[k] and vc[c][k] then v &= k end if
            end for
            if v={} then
                clash = true
                exit
            end if
            integer z = v[rand(length(v))]
            rn &= z
            vr[z] = 0           -- no longer valid
            vc[c][z] = 0        --      ""
        end for
        if not clash then
            res = append(res,rn)
            vcs = vc
        else
            clashes += 1
            if time()>t1 then
                printf(1,"rows completed:%d/%d, clashes:%d\n",
                         {length(res),n,clashes})
                t1 = time()+1
            end if
        end if
    end while
    for i=1 to n do
        string line = ""
        for j=1 to n do
            line &= aleph[res[i][j]]
        end for
        res[i] = line
    end for
    return res
end function
 
procedure latin_square(integer n)
    atom t0 = time()
    string res = join(ls(n),"\n"),
           e = elapsed(time()-t0)
    printf(1,"Latin square of order %d (%s):\n%s\n",{n,e,res})
end procedure
latin_square(3)
latin_square(5)
latin_square(5)
latin_square(10)
if platform()!=JS then
    latin_square(42)
end if
