15 lines
499 B
TypeScript
15 lines
499 B
TypeScript
export function shuffleArray<T>(array: T[]): T[] {
|
|
let currentIndex = array.length,
|
|
temporaryValue: T | undefined,
|
|
randomIndex: number;
|
|
while (0 !== currentIndex) {
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex -= 1;
|
|
// use non-null assertions because we're swapping indices that exist
|
|
temporaryValue = array[currentIndex] as T;
|
|
array[currentIndex] = array[randomIndex] as T;
|
|
array[randomIndex] = temporaryValue as T;
|
|
}
|
|
return array;
|
|
}
|