3.19 How to select N random values from a set of numbers?

Given the set \(v{1,2,3,5,6,7,11,12,20,21}\) how to select say \(m=5\) random numbers from it?

Mathematica

method 1

a = {1, 2, 3, 5, 6, 7, 11, 12, 20, 21}; 
m = 5; 
b = Table[0, {m}]; 
Do[k = RandomInteger[{1, Length[a]}]; 
  b[[i]] = a[[k]]; 
  a = Delete[a, k], 
  {i, 1, m} 
  ]; 
b
 

{6, 21, 3, 5, 11}
 

method 2 (Version 9)

RandomSample[a, m]
 

{1, 6, 11, 7, 20}  *)
 

 

Matlab

A = [1,2,3,5,6,7,11,12,20,21]; 
m = 5; 
B = zeros(m,1); 
for i = 1:m 
    k = randi(length(A),1); 
    B(i) = A(k); 
    A(k) = []; 
end 
B
 

B = 
     2 
    20 
     7 
    11 
     1