diff --git a/src/utils.tsx b/src/utils.tsx index 0ede4ea..0e06877 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -105,6 +105,19 @@ export function unique(array: T[]): T[] { return [...new Set(array)] } +// shuffle a copy of an array +export function shuffle(arr: readonly T[]): T[] { + const out = arr.slice() + for (let i = out.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + + const tmp = out[i]! + out[i] = out[j]! + out[j] = tmp + } + return out +} + // random number between 1 and 10, with decreasing probability export function weightedRand(): number { // Weights: 1 has weight 10, 2 has weight 9, ..., 10 has weight 1