# How to generate a sequence of numbers in julia

**URL:** <https://discourse.julialang.org/t/how-to-generate-a-sequence-of-numbers-in-julia/19864>\
**Category:** General Usage\
**Created:** [January 21, 2019, 9:39am UTC](https://discourse.julialang.org/t/how-to-generate-a-sequence-of-numbers-in-julia/19864 "2019-01-21T09:39:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 21, 2019, 9:39am UTC](https://discourse.julialang.org/t/how-to-generate-a-sequence-of-numbers-in-julia/19864/1 "2019-01-21T09:39:39Z")

</div>

I found this link: [How do you generate a regular non-integer sequence in julia? - Stack Overflow](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
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 ?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 21, 2019, 9:44am UTC](https://discourse.julialang.org/t/how-to-generate-a-sequence-of-numbers-in-julia/19864/2 "2019-01-21T09:44:54Z")

</div>

Note that the answer in the link is quite old.

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 21, 2019, 11:07am UTC](https://discourse.julialang.org/t/how-to-generate-a-sequence-of-numbers-in-julia/19864/3 "2019-01-21T11:07:46Z")

</div>

> [@kristoffer.carlsson](#):
>
> 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.

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.
