// 随机选取n个元素 不允许重复
// 思路:生成未选数组个数的随机数 把选中的元素与未选数组最后一位进行交换
const arr = [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10]
const fn = (arr, size) => {
let res = []
for(let i = 0; i < size; i++){
let cursor = Math.floor(Math.random() * (arr.length - i))
let temp = arr[cursor]
res.push(temp)
arr[cursor] = arr[arr.length - 1 - i]
arr[arr.length - 1 - i] = temp
}
return res
}
console.log(fn(arr, 3))