mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 12:30:12 +02:00
23 lines
742 B
TypeScript
23 lines
742 B
TypeScript
export function shuffle<T>(array: T[]): T[] {
|
|
// Create a copy of the array to avoid mutating the original
|
|
const shuffled = [...array];
|
|
|
|
// Loop through the array from the last element to the first
|
|
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
// Generate a random index from 0 to i
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
// Swap elements at positions i and j
|
|
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
}
|
|
|
|
return shuffled;
|
|
}
|
|
|
|
export function shuffleInPlace<T>(array: T[]): 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]];
|
|
}
|
|
return array;
|
|
}
|