# How to broadcast rand(lb:ub)?

**URL:** <https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546>\
**Category:** General Usage\
**Created:** [June 10, 2026, 5:22pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546 "2026-06-10T17:22:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 10, 2026, 5:22pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/1 "2026-06-10T17:22:19Z")

</div>

As from the title, how do I broadcast rand(lowerbound:upperbound) ?

e.g.

```julia-auto
x = [1,2,3]
a = 2 .* x .+ rand.() # ok
a = @. 2x + rand() # ok
a = @. 2x + rand(1:2) # DimensionMismatch error

```

BDW, I am aware of `a = 2 .* x .+ rand(1:2,length(x))`, but is there a more elegant solution (I would like to keep the `@.` macro) ?

---

<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:** [June 10, 2026, 5:31pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/2 "2026-06-10T17:31:39Z")

</div>

Put it in a container, like a 1-element tuple, to treat `1:2` as a “scalar” for broadcasting:

```julia-auto
julia> @. 2x + rand((1:2,))
3-element Vector{Int64}:
 3
 6
 7

```

> [@sylvaticus](#):
>
> I am aware of `a = 2 .* x .+ rand(1:2,length(x))`, but is there a more elegant solution

(This is not only less “elegant” but it also allocates an additional temporary array.)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 10, 2026, 7:19pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/3 "2026-06-10T19:19:16Z")

</div>

That’s a bit odd: why here the `Ref()` trick doesn’t work ?

e.g.:

```julia-auto
a = @. 2x + rand(Ref(1:2)) # error
foo(x,y) = y[1] + x * y[2]
foo.([0,10,100],Ref([100,1])) # fine here

```

---

<div class="post-metadata">

**Author:** ![aryavorskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aryavorskiy/32/43466_2.png) [@aryavorskiy](https://discourse.julialang.org/u/aryavorskiy)\
**Post date:** [June 10, 2026, 7:27pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/4 "2026-06-10T19:27:59Z")

</div>

Because Ref is also broadcast ed in a macro. Try one of

```julia-auto
a = 2x .+ rand.(Ref(1:2))
a = @. 2x + rand($(1:2))

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 10, 2026, 7:28pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/5 "2026-06-10T19:28:27Z")

</div>

`@. Ref(x)` is also broadcasting `Ref`, defeating the whole point of it.

---

<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:** [June 10, 2026, 7:46pm UTC](https://discourse.julialang.org/t/how-to-broadcast-rand-lb-ub/137546/6 "2026-06-10T19:46:51Z")

</div>

This works:

```julia-auto
julia> @. 2x + rand($Ref(1:2))
3-element Vector{Int64}:
 3
 6
 8

```

(Splicing a function call with `$` prevents it from being broadcasted by `@.`.)

> [@aryavorskiy](#):
>
> `a = 2x .+ rand.(Ref(1:2))`

This allocates a temporary array for `2x`.

> [@aryavorskiy](#):
>
> `a = @. 2x + rand($(1:2))`

This doesn’t work: `$(1:2)` is equivalent to `1:2` in the `@.`.
