const ram = (N) => Math.floor(Math.random() * N);
/**
* @param {string[]} pokers
* @param {number} count 取多少個
* @return {string[]}
*/
function randomPoker (pokers, count) {
let randomPokers = [];
for(; count > 0 ; count--){
let len = pokers.length;
// 隨機取一個 index
let pick = ram(len);
randomPokers.push(pokers[pick]);
if(pick !== (len - 1)){
pokers[pick] = pokers[len - 1]
}
// pop 是 Big O(1) 比 unshift O(n) 好
pokers.pop();
}
return randomPokers;
}
randomPoker(['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], 13);
function getRandomNumbers(pickNumbers=0, range = 13){
let numbers = Array.from({length: range}, (_, i)=>i)
let result = []
// console.log('numbers: ', numbers)
for(let i=0; i<pickNumbers; i++){
const random = Math.floor(Math.random()*range) // 0 - 12
result[i] = numbers[random]
numbers[random] = numbers.pop()
}
return result
}