From d02eba1c5d11bd6f3fe5451a5de06072f2c6a985 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 30 Nov 2025 09:11:00 -0800 Subject: [PATCH] shuffle --- src/utils.tsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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