This commit is contained in:
Chris Wanstrath 2025-11-30 09:11:00 -08:00
parent 441713675a
commit d02eba1c5d

View File

@ -105,6 +105,19 @@ export function unique<T>(array: T[]): T[] {
return [...new Set(array)]
}
// shuffle a copy of an array
export function shuffle<T>(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