Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript get n random elements from array

// Shuffle array
const shuffled = array.sort(() => 0.5 - Math.random());

// Get sub-array of first n elements after shuffled
let selected = shuffled.slice(0, n);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #random #elements #array
ADD COMMENT
Topic
Name
3+1 =