Files
Tarokka/tools/getRandomItems.ts
2025-07-04 14:18:38 -04:00

12 lines
362 B
TypeScript

export const getRandomItems = <T>(items: T[], count: number): T[] => {
const shuffled = [...items];
// Fisher-Yates shuffle
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
};