Random array without repetition
Dyalog APL, 1 byte
?
Just a builtin. Try it here.
JavaScript (ES6), 68 66 bytes
n=>r=>G=(s=new Set)=>s.size<n?G(s.add(Math.random()*r+1|0)):[...s]
Called as F(N)(R)()
, where F
is the function assignment, and N
/R
are the values.
You asked for shorter than 73 bytes in Js ;)
EDIT: The answer by @C5H8NNaO4 works within the fact that the rules don't specify the values must be uniform across 1..R
. Given that, here's a version works in 63 bytes (called as F(R)(N)
):
r=>G=(n,s=[])=>n--?G((s[n]=n+1,n),s):s.sort(a=>new Date/a%1-.5)
Octave, 22 19 9 bytes
@randperm
randperm(r,n)
does exactly what is requested. Note that this does not work (at least not in oldder versions) in Matlab.