# Usage of \`@.\`

**URL:** <https://discourse.julialang.org/t/usage-of/91086>\
**Category:** General Usage\
**Tags:** broadcasting\
**Created:** [December 1, 2022, 10:07am UTC](https://discourse.julialang.org/t/usage-of/91086 "2022-12-01T10:07:39Z")\
**Posts on this page:** 4\
**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:** [December 1, 2022, 10:07am UTC](https://discourse.julialang.org/t/usage-of/91086/1 "2022-12-01T10:07:39Z")

</div>

Hi,

I am trying to generate random vector with `{-1, +1}` on length `n`.

The following code works

```julia
y = 2 .* (rand(n) .> 0.50) .- 1

```

When I try to use `@.`, it throws error

```julia
y = @. 2 * (rand(n) > 0.50) - 1

```

```julia
ERROR: MethodError: no method matching isless(::Float64, ::Vector{Float64})
Closest candidates are:
  isless(::T, ::T) where T<:Union{Float16, Float32, Float64} at float.jl:424
  isless(::AbstractVector, ::AbstractVector) at abstractarray.jl:2612
  isless(::AbstractFloat, ::AbstractFloat) at operators.jl:184
  ...
Stacktrace:
  [1] <(x::Float64, y::Vector{Float64})
    @ Base ./operators.jl:356
  [2] >(x::Vector{Float64}, y::Float64)
    @ Base ./operators.jl:382
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:643 [inlined]
  [5] _getindex
    @ ./broadcast.jl:667 [inlined]
  [6] _getindex
    @ ./broadcast.jl:666 [inlined]
  [7] _broadcast_getindex
    @ ./broadcast.jl:642 [inlined]
  [8] _getindex
    @ ./broadcast.jl:666 [inlined]
  [9] _broadcast_getindex
    @ ./broadcast.jl:642 [inlined]
 [10] getindex
    @ ./broadcast.jl:597 [inlined]
 [11] copy
    @ ./broadcast.jl:875 [inlined]
 [12] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(-), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(*), Tuple{Int64, Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(>), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(rand), Tuple{Int64}}, Float64}}}}, Int64}})
    @ Base.Broadcast ./broadcast.jl:860
 [13] top-level scope
    @ REPL[51]:1

```

But, if I create the random vector first and then use `@.`, it works again

```julia
u = rand(n)
y = @. 2 * (u > 0.50) - 1

```

I could like to know why I cannot use `rand()` with `@.`?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [December 1, 2022, 10:14am UTC](https://discourse.julialang.org/t/usage-of/91086/2 "2022-12-01T10:14:09Z")

</div>

`@. rand(n) > 0.5` is equivalent to `rand.(2) .> 0.5` and not `rand(2) .> 0.5`. The former is equivalent to

```julia
julia> for i in 2
           rand(i) > 0.5
       end

```

Perhaps you want something like

```julia
julia> @. (i -> 2 * (rand() > 0.50) - 1)(1:4)
4-element Vector{Int64}:
 -1
 -1
 -1
 -1

```

or, more clearly,

```julia
julia> [2 * (rand() > 0.50) - 1 for i in 1:4]
4-element Vector{Int64}:
  1
 -1
  1
  1

```

---

<div class="post-metadata">

**Author:** ![Evey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evey/32/214967_2.png) [@Evey](https://discourse.julialang.org/u/Evey)\
**Post date:** [December 1, 2022, 11:07am UTC](https://discourse.julialang.org/t/usage-of/91086/3 "2022-12-01T11:07:48Z")

</div>

You could also solve this particular problem as:

```julia
julia> rand([-1, 1], 6)
6-element Vector{Int64}:
 -1
  1
  1
  1
 -1
  1

```

The version, `rand(v, n)`, will pick random elements from `v` to fill up a vector of length `n`, so you can use it to generate more complicated sequences as well.

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [December 1, 2022, 11:20am UTC](https://discourse.julialang.org/t/usage-of/91086/4 "2022-12-01T11:20:04Z")

</div>

Since you only need two values, generating random `Bool`s (instead of `Float64`s like in your original code) is faster:

```julia
julia> @btime 2 .* (rand(100_000) .> .5) .- 1;
  256.643 μs (4 allocations: 1.53 MiB)

julia> @btime rand($([-1,1]), 100_000);
  668.837 μs (2 allocations: 781.30 KiB)

julia> @btime rand($(-1:2:1), 100_000);
  647.847 μs (2 allocations: 781.30 KiB)

julia> @btime 2 .* rand(Bool, 100_000) .- 1;
  79.452 μs (4 allocations: 879.03 KiB)

```
