# \`rand() + I\` vs \`rand() .+ I\`

**URL:** https://discourse.julialang.org/t/rand-i-vs-rand-i/41985
**Category:** General Usage
**Created:** [June 24, 2020, 2:40pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985 "2020-06-24T14:40:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)
#### Post date: [June 24, 2020, 2:40pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/1 "2020-06-24T14:40:48Z")

</div>

What is the difference between `rand() + I` and `rand() .+ I`? (`I` is from `using LinearAlgebra`).

The first one works, but second on throws error.

```julia
julia> rand() + I
1.04893935660533

julia> rand() .+ I
ERROR: MethodError: no method matching length(::UniformScaling{Bool})
Closest candidates are:
  length(::Core.SimpleVector) at essentials.jl:596
  length(::Base.MethodList) at reflection.jl:852
  length(::Core.MethodTable) at reflection.jl:938
  ...
Stacktrace:
 [1] _similar_for(::UnitRange{Int64}, ::Type{Bool}, ::UniformScaling{Bool}, ::Base.HasLength) at ./array.jl:576
 [2] _collect(::UnitRange{Int64}, ::UniformScaling{Bool}, ::Base.HasEltype, ::Base.HasLength) at ./array.jl:609
 [3] collect(::UniformScaling{Bool}) at ./array.jl:603
 [4] broadcastable(::UniformScaling{Bool}) at ./broadcast.jl:665
 [5] broadcasted(::Function, ::Float64, ::UniformScaling{Bool}) at ./broadcast.jl:1236
 [6] top-level scope at REPL[20]:1

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [June 24, 2020, 2:55pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/2 "2020-06-24T14:55:24Z")

</div>

Regarding the dot notation, you can take a look at the manual here:  
[https://docs.julialang.org/en/v1/manual/mathematical-operations/index.html](https://docs.julialang.org/en/v1/manual/mathematical-operations/index.html)

I also like this blog post, which explains why the dot notation is so nice:

> **[More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)**
>
> More Dots: Syntactic Loop Fusion in Julia | After a lengthy design process (https://github.com/JuliaLang/julia/issues/8450) and preliminary foundations in Julia 0.5 (/blog/2016-10-11-julia-0.5-highlights#vectorized\_function\_calls), Julia 0.6 includes...

---

<div class="post-metadata">

### Author: ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)
#### Post date: [June 24, 2020, 3:30pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/3 "2020-06-24T15:30:21Z")

</div>

Hi @tk3369, thanks for the blog post. I is really useful.

But still I am unable to find why that specific error is occurring for he second version. It happens because I think there is some ambiguity about the size of `I`. But I would also assume that it could be easily found as in the case of non-dot version.

---

<div class="post-metadata">

### Author: ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)
#### Post date: [June 24, 2020, 4:13pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/4 "2020-06-24T16:13:29Z")

</div>

For normal usage, the `I` operator adapts its size to the context where it is used in. Take this as an example:

```julia
julia> A = rand(1:10, 2, 2) #get a simple matrix
2×2 Array{Int64,2}:
 6 5
 8 6

julia> [A; I] #extend the marix with the identity Matrix
4×2 Array{Int64,2}:
 6 5
 8 6
 1 0
 0 1

```

Here you can see, how the size of the identity operator `I` automatically matches the dimension of the matrix `A`.  
In your case, you try to add a random number to each element of `I`. That fails because `I` has no idea about it’s size. To fix this, you just need to add the dimension like this:

```julia
julia> rand().+I(2) #size 2
2×2 Array{Float64,2}:
 1.04718 0.0471772
 0.0471772 1.04718

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [June 24, 2020, 4:35pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/5 "2020-06-24T16:35:00Z")

</div>

Note that `rand() .+ I(n)` adds the same random number to all elements. If you want to add a different random number to each element of `I(n)` broadcast also the call to `rand()`:

```julia
julia> using LinearAlgebra

julia> rand.() .+ I(2)
2×2 Array{Float64,2}:
 1.9842 0.167312
 0.722555 1.82408

```

---

<div class="post-metadata">

### Author: ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)
#### Post date: [June 25, 2020, 3:08pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/6 "2020-06-25T15:08:40Z")

</div>

Hi @roble,  
Is there any specific reason why the `dot ` version cannot find the size, but the normal version can?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 25, 2020, 3:11pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/7 "2020-06-25T15:11:19Z")

</div>

The normal version isn’t finding a size. `I+rand()` increments all diagonal elements by the same amount, so you don’t need a size. you are just calling `rand` once.

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [June 25, 2020, 3:15pm UTC](https://discourse.julialang.org/t/rand-i-vs-rand-i/41985/8 "2020-06-25T15:15:02Z")

</div>

That’s just because `I` is being adapted to the size of the other input in the addition, which is just a number. When broadcasting, both inputs must have a **definite** size.
