Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia : generating unique random integer array

I am trying to create 10 element array of unique random integers. However I am unable to create array with unique values. Is there in Julia something like Pythons sample function ?

numbers = zeros(Array(Int64, 10))
rand!(1:100, numbers)

Thanks.

like image 770
M.Puk Avatar asked Mar 16 '16 21:03

M.Puk


2 Answers

If performance is not an issue (i.e. the sample range isn't too large, or the sample count is close to the sample range), and if you don't want to use an additional package for whatever reason, try:

a = randperm(100)[1:10]

like image 166
reschu Avatar answered Nov 11 '22 14:11

reschu


There is a sample function in StatsBase:

using StatsBase
a = sample(1:100, 10, replace = false)

This will draw a sample of length 10 from 1:100 without replacement.

like image 31
amrods Avatar answered Nov 11 '22 12:11

amrods