How is it possible to make a matrix with special random elements?

Viewed 69

is there any possibility in Julia to make a matrix with special random elements? for example, a matrix which each row has random elements but every elements should repeat at least one time:

n = zeros(Int,3, 5)

    for i in indices(n, 1)

        for j in indices(n, 2)

            n[i,j]=rand(0:3)

        end

    end
 n=
3×5 Array{Int64,2}:
 1  2  1  1  2
 3  3  2  2  0
 3  2  1  0  0

but in second row, there is not 1 . would you please help me how this matrix is made? Thanks.

1 Answers

You can use this function:

using Random

function randfill!(m::AbstractMatrix, s::AbstractVector)
    n1 = length(s)
    n2 = size(m, 2)
    @assert n2 >= n1
    for i in 1:size(m,1)
        m[i, 1:n1] .= s
        for j in n1+1:n2
            m[i,j] = rand(s)
        end
        shuffle!(view(m, i, :))
    end
    m
end
Related