Difference between rand(Complex) and rand(Float) + 1im*rand(Float)?

I noted a difference in rand when applying QR. When I run:

using LinearAlgebra
a = rand(5,5) + 1im*zeros(5,5)
display(qr(a))

I get Q and R matrices with zeros for imaginary numbers - I’m going to show only the Q matrix, but the same occurs for R matrix:

LinearAlgebra.QRCompactWY{Complex{Float64},Array{Complex{Float64},2}}
Q factor:
5×5 LinearAlgebra.QRCompactWYQ{Complex{Float64},Array{Complex{Float64},2}}:
 -0.306991+0.0im  -0.0269743+0.0im     0.44535+0.0im   0.507313+0.0im  -0.670318+0.0im
 -0.513317+0.0im   -0.331366+0.0im    0.443593+0.0im  -0.653912+0.0im  0.0482431+0.0im
 -0.253514+0.0im   -0.195796+0.0im   -0.735177+0.0im  -0.240831+0.0im  -0.546726+0.0im
 -0.452319+0.0im    0.879683+0.0im  -0.0607519+0.0im  -0.129452+0.0im  0.0334178+0.0im
 -0.611065+0.0im   -0.278013+0.0im   -0.246398+0.0im   0.490179+0.0im   0.498318+0.0im

When I create a complex random matrix, I get complex numbers as expected

a = rand(Complex{Float64},5,5) 
display(qr(a))

I obtain:

LinearAlgebra.QRCompactWY{Complex{Float64},Array{Complex{Float64},2}}
Q factor:
5×5 LinearAlgebra.QRCompactWYQ{Complex{Float64},Array{Complex{Float64},2}}:
 -0.417663-0.390527im  0.0434441+0.219283im        0.50579-0.292637im      0.449124+0.237358im   0.0737883-0.134603im
 -0.241653-0.195952im  -0.273828+0.504695im      -0.138368-0.260033im     -0.614793-0.162831im    0.271654-0.0919992im
 -0.283323-0.158954im  0.0356911-0.0875481im     0.0755045+0.54749im     -0.0344772-0.414637im   -0.142673-0.621779im
 -0.350583-0.354448im   0.476321-0.497379im   -0.000282387+0.00840499im   -0.331509-0.0444532im   0.189458+0.359654im
 -0.387173-0.26883im   -0.364405+0.0658085im     -0.469138+0.214872im      0.181638+0.13959im    -0.448918+0.347044im

Does anybody knows what’s is going on ?

In your first example you have an array with complex values (Matrix{Complex{Float64}}) with all imaginary parts 0, so the result is real. In your second example you have a complex valued matrix with non-zero imaginary parts, so the results is complex. I don’t see how this is related to rand?

is probably a typo, should likely be rand(5,5) even for the imaginary part.

My typo :stuck_out_tongue_winking_eye:, and a misunderstood of concept where this code has been applied.

Never mind