jsは配列から異なるランダム要素を取得します.
431 ワード
let arr = [1,2,3,4,5,6,7,8,9]
function getRandom(arr, count) {
let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
console.log(getRandom(arr, 4))