Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
(def y (->> x shuffle (take k)))
allocate (sample(k))
do i=1,k
sample(i) = x(i)
end do
do i=k+1,n
call random_number(a)
j = 1 + int(i*a)
if (j .le. k) sample(j) = x(i)
end do
const idx = x.map((item, i) => i);
while (y.length < k) {
const i = parseInt(Math.random() * idx.length, 10);
y.push(x[[idx[i]]]);
idx.splice(i, 1);
}
$y = array_intersect_key($x, array_flip(array_rand($x, $k)));
function RandArr(Max: Integer): TIntegerDynArray;
var
i, j, temp: Integer;
begin
SetLength(Result, Max+1);
for i := Low(Result) to High(Result) do Result[i] := i;
i := Length(Result);
while i > 0 do
begin
Dec(i);
j := RandomRange(0,i);
temp := Result[i];
Result[i] := Result[j];
Result[j] := temp;
end;
end;
var
Idx: TIntegerDynArray;
begin
Idx := RandArr(High(X));
SetLength(Y, k);
for i := 0 to k-1 do Y[i] := X[Idx];
end.
y = x.sample(k)
y := x shuffled first: k.