# Efficient way of creating a vector of length L with unique elements

**URL:** <https://discourse.julialang.org/t/efficient-way-of-creating-a-vector-of-length-l-with-unique-elements/98388>\
**Category:** Performance\
**Tags:** vector, random\
**Created:** [May 5, 2023, 3:38pm UTC](https://discourse.julialang.org/t/efficient-way-of-creating-a-vector-of-length-l-with-unique-elements/98388 "2023-05-05T15:38:28Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [May 5, 2023, 4:33pm UTC](https://discourse.julialang.org/t/efficient-way-of-creating-a-vector-of-length-l-with-unique-elements/98388/2 "2023-05-05T16:33:42Z")

</div>

> [@dm17017](#):
>
> I am looking to fill a vector of a desired length up with randomly chosen, unique elements.

This is called “random sampling without replacement” and is implemented e.g. by [`StatsBase.sample`](https://juliastats.org/StatsBase.jl/stable/sampling/#StatsBase.sample).

See also [Sampling from a list of integers without repetition](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395) and [Sampling without replacement](https://discourse.julialang.org/t/sampling-without-replacement/1073).

---

_[View the full topic](https://discourse.julialang.org/t/efficient-way-of-creating-a-vector-of-length-l-with-unique-elements/98388)._
