# Sampling from a list of integers without repetition

**URL:** <https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395>\
**Category:** New to Julia\
**Created:** [June 14, 2020, 6:10pm UTC](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395 "2020-06-14T18:10:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 14, 2020, 6:10pm UTC](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395/1 "2020-06-14T18:10:15Z")

</div>

Given a vector of integers

```julia
a = collect(1:10)

```

I would like to randomly sample 5 integers with _guaranteed_ non-repetition. As far as I know,

```julia
b = rand(a, 10)

```

does not guarantee that. This is easily demonstrated via

```julia
for i in 1:100
    println(rand(1:10, 8))
end

```

How can it be done? Thanks.

I found the answer on Slack: [Julia : generating unique random integer array - Stack Overflow](https://stackoverflow.com/questions/36047516/julia-generating-unique-random-integer-array)

```julia
StatsBase.rand(using StatsBase
a = sample(1:100, 10, replace = false)

```

Given that `rand` has 173 functions in its dispatch table, that adding a `replace` argument` in several of them could prove very useful.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 14, 2020, 6:48pm UTC](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395/2 "2020-06-14T18:48:34Z")

</div>

`sample` is the correct method (and terminology) here. `rand` is for random number generation, and if it guaranteed unique values then the sequence would no longer be random.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 15, 2020, 7:39am UTC](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395/3 "2020-06-15T07:39:04Z")

</div>

> [@erlebach](#):
>
> sample 5 integers with _guaranteed_ non-repetition

To add to @tbeason’s excellent answer: the technical term for this is _sampling without replacement_, while `rand` is [IID](https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables) (as approximated by a pseudorandom process).

Because of this, adding such an option to `rand` would not be the right place for this functionality. If you know the term, `sample` is also more discoverable.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 15, 2020, 10:53am UTC](https://discourse.julialang.org/t/sampling-from-a-list-of-integers-without-repetition/41395/4 "2020-06-15T10:53:19Z")

</div>

tpThank you all!
