Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Sampling from a list of integers without repetition

$
0
0

Given a vector of integers

a = collect(1:10)

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

b = rand(a, 10)

does not guarantee that. This is easily demonstrated via

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

How can it be done? Thanks.

I found the answer on Slack: https://stackoverflow.com/questions/36047516/julia-generating-unique-random-integer-array

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.

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles