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

**URL:** <https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878>\
**Category:** New to Julia\
**Created:** [September 12, 2018, 8:21pm UTC](https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878 "2018-09-12T20:21:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [September 12, 2018, 8:21pm UTC](https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878/1 "2018-09-12T20:21:49Z")

</div>

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

```julia
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:

```julia
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

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

```

I obtain:

```julia
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 ?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 12, 2018, 8:25pm UTC](https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878/2 "2018-09-12T20:25:42Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 12, 2018, 8:26pm UTC](https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878/3 "2018-09-12T20:26:23Z")

</div>

> [@Noel\_Araujo](#):
>
> `rand(5,5) + 1im*zeros(5,5)`

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

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [September 13, 2018, 2:52pm UTC](https://discourse.julialang.org/t/difference-between-rand-complex-and-rand-float-1im-rand-float/14878/4 "2018-09-13T14:52:45Z")

</div>

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

Never mind
