shuffle
This commit is contained in:
parent
441713675a
commit
d02eba1c5d
|
|
@ -105,6 +105,19 @@ export function unique<T>(array: T[]): T[] {
|
||||||
return [...new Set(array)]
|
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
|
// random number between 1 and 10, with decreasing probability
|
||||||
export function weightedRand(): number {
|
export function weightedRand(): number {
|
||||||
// Weights: 1 has weight 10, 2 has weight 9, ..., 10 has weight 1
|
// Weights: 1 has weight 10, 2 has weight 9, ..., 10 has weight 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user