# \`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:** 1\
**Showing post:** 11

<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.)

---

_[View the full topic](https://discourse.julialang.org/t/rand-vs-rand/2079)._
