mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-04-28 17:18:59 +00:00
16 lines
416 B
TypeScript
16 lines
416 B
TypeScript
/**
|
|
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
|
*/
|
|
export function shuffleInPlace<T>(array: T[]) {
|
|
for (let i = array.length - 1; i >= 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[array[i], array[j]] = [array[j]!, array[i]!];
|
|
}
|
|
}
|
|
|
|
export function shuffle<T>(array: T[]): T[] {
|
|
const result = [...array];
|
|
shuffleInPlace(result);
|
|
return result;
|
|
}
|