# \`rand()\` vs. \`rand.()\`?

**URL:** <https://discourse.julialang.org/t/rand-vs-rand/2079>\
**Category:** General Usage\
**Tags:** question, proposal\
**Created:** [February 13, 2017, 1:47pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079 "2017-02-13T13:47:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 13, 2017, 1:47pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/1 "2017-02-13T13:47:47Z")

</div>

Consider the following example:

```julia
x = zeros(10)
x .= rand()

```

All components of `x` will have the same (random) value. Now suppose I do instead:

```julia
x = zeros(10)
x .= rand.()

```

Since `rand.()` is dotted, I’d expect it to apply to each element of `x` separatedly. In this case, I should obtain ten distinct random values. However the result is the same as above (a single random value fills the whole vector).

Even though `rand.()` has no arguments, it still should “apply to each element separatedly”. At least that’s what my intuition tells me.

Can someone explain this behavior?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 13, 2017, 1:55pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/2 "2017-02-13T13:55:24Z")

</div>

`x .= a` just fills `x` with `a`’s when they are scalars, regardless of how you got `a`.

`rand.()` gives you a scalar. The dot has no effect here, as there is nothing to broadcast over, so it is the same as `rand()`.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 13, 2017, 1:56pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/3 "2017-02-13T13:56:29Z")

</div>

@Tamas_Papp The behavior I suggest could be useful. Don’t you agree?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 13, 2017, 1:56pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/4 "2017-02-13T13:56:50Z")

</div>

It’s already fixed on master.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 13, 2017, 1:57pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/5 "2017-02-13T13:57:17Z")

</div>

@yuyichao What do you mean? What’s the new behavior?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 13, 2017, 1:58pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/6 "2017-02-13T13:58:29Z")

</div>

```julia
julia> x = zeros(10);

julia> x .= rand.();

julia> x
10-element Array{Float64,1}:
 0.344676
 0.0245599
 0.276739
 0.500142
 0.256044
 0.176647
 0.406575
 0.547504
 0.913086
 0.170801

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 13, 2017, 2:00pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/7 "2017-02-13T14:00:39Z")

</div>

[https://github.com/JuliaLang/julia/pull/19799](https://github.com/JuliaLang/julia/pull/19799)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 13, 2017, 2:01pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/8 "2017-02-13T14:01:47Z")

</div>

@yuyichao Nice. So it should be on 0.6.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [February 13, 2017, 5:50pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/9 "2017-02-13T17:50:56Z")

</div>

Actually it seems @Tamas_Papp approach makes more sense.

The `.=` means do the operation in broadcasting way.  
Now, broadcasting is operation which its input is array (Or arrays).  
It shouldn’t be driven by `rand()` function.

Seems useful to have it but no coherent as a language.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 13, 2017, 5:58pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/10 "2017-02-13T17:58:58Z")

</div>

@RoyiAvital This is fine if you do `x .= rand()`. However, the point is that if you use a dot in `rand.()` (as in `x .= rand.()`), it should be broadcasted too.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 13, 2017, 7:56pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/11 "2017-02-13T19:56:37Z")

</div>

`x .= rand()` is equivalent to `broadcast!(identity, x, rand())`, so `rand` is called only once.

`x .= rand.()` is equivalent to `broadcast!(rand, x)`. So (in 0.6), `rand()` is called once for each element of `x`.

The nice thing about the latter is that you can do vectorized operations with random numbers without requiring a temporary array to fill with `rand(n)` or `rand!` first. For example, `x .+= y .* rand.()` occurs entirely in-place in a single loop, adding a random fraction of each element of `y` to each element of `x`. (Or equivalently, you can do `@. x += y * rand()` in 0.6, allowing you to omit all of the dots.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 13, 2017, 8:08pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/12 "2017-02-13T20:08:19Z")

</div>

> [@RoyiAvital](#):
>
> Actually it seems @Tamas_Papp approach makes more sense.

Just to be clear, I was not making a normative statement, just describing how I think `0.5.0` works. I think the broadcasting semantics of `0.6` are very nice, including the example above by @stevengj and also the broadcasting on `Nullable`s; I am looking forward to both (and the inevitable corner cases which will be surprising 🙂).

---

<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 17, 2017, 4:43pm UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/13 "2017-06-17T16:43:00Z")

</div>

Is there a syntax equivalent to `x .= rand.()` on Julia 0.6 that works on Julia 0.5 as well?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 18, 2017, 1:48am UTC](https://discourse.julialang.org/t/rand-vs-rand/2079/15 "2017-06-18T01:48:20Z")

</div>

`rand!(x)`?
