Refactor (1)

This commit is contained in:
2025-09-19 18:54:12 +09:00
committed by cannorin
parent 6b15542c40
commit c3b1bf39a4
26 changed files with 1684 additions and 105 deletions

View File

@@ -0,0 +1,12 @@
export const sample = <T>(arr: readonly T[]): T =>
arr[Math.floor(Math.random() * arr.length)] as T;
export function sampleMany<T>(arr: T[], n: number = arr.length): T[] {
if (n > arr.length) return sampleMany(arr, arr.length);
const copy = [...arr];
for (let i = 0; i < n; i++) {
const j = i + Math.floor(Math.random() * (copy.length - i));
[copy[i], copy[j]] = [copy[j] as T, copy[i] as T];
}
return copy.slice(0, n);
}