feat(tools): add get_random_words tool with Fisher-Yates shuffle
This commit is contained in:
11
helpers/shuffle.ts
Normal file
11
helpers/shuffle.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
const fishersYatesShuffle = <Type>(arr: Type[]): Type[] => {
|
||||
const shuffled = [...arr]
|
||||
for (let index = shuffled.length - 1; index > 0; index--) {
|
||||
const secondIndex = Math.floor(Math.random() * (index + 1))
|
||||
// biome-ignore lint/style/noNonNullAssertion: indices are guaranteed to be valid in Fisher-Yates shuffle
|
||||
;[shuffled[index]!, shuffled[secondIndex]!] = [shuffled[secondIndex]!, shuffled[index]!]
|
||||
}
|
||||
return shuffled
|
||||
}
|
||||
|
||||
export default fishersYatesShuffle
|
||||
Reference in New Issue
Block a user