Sampling a random subset from an array

I suggest shuffling a copy of the array using the Fisher-Yates shuffle and taking a slice: function getRandomSubarray(arr, size) { var shuffled = arr.slice(0), i = arr.length, temp, index; while (i–) { index = Math.floor((i + 1) * Math.random()); temp = shuffled[index]; shuffled[index] = shuffled[i]; shuffled[i] = temp; } return shuffled.slice(0, size); } var x … Read more