# How to generate a sequence of numbers in julia

I found this link: https://stackoverflow.com/questions/21841295/how-do-you-generate-a-regular-non-integer-sequence-in-julia/21841396, so I was thinking to try it out. And then I was confused.

``````Julia has exited. Press Enter to start a new session.
Starting Julia...
_
_       _ _(_)_     |  A fresh approach to technical computing
(_)     | (_) (_)    |  Documentation: https://docs.julialang.org
_ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` |  |
| | |_| | | | (_| |  |  Version 0.7.0 (2018-08-08 06:46 UTC)
_/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> [0.1:0.1:1]
1-element Array{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},1}:
0.1:0.1:1.0
``````

My question: what is the length of the array
`[0.1:0.1:1]` in julia ?

If you want to create the array you can either `collect(0.1:0.1:1)` or:

``````julia> [0.1:0.1:1;]
10-element Array{Float64,1}:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
``````

but most often, there is no need to materialize the Array and working with the more efficient range object (the result of `0.1:0.1:1`) is then preferable.

5 Likes

I would go even further: you should almost never materialize the range. The right answer is therefore just simply `0.1:0.1:1` or maybe use the `range` function.

The reason I emphasize this, even though @kristoffer.carlssonās answer already mentions it, is that the misconception that you need to use `collect` appears to be extremely widespread. Almost everyone new to Julia seem sprinkle `collect` throughout their code, almost always for no good reason.

There are some legitimate uses for `collect`, otherwise Iād propose to change the name to something very ugly, indeed.

3 Likes