array.sort(() => Math.random() - 0.5);
sort()는 배열을 정렬할 때 두 요소(a, b)를 비교함
Math.random()은 0~1 사이의 랜덤한 숫자를 반환
그래서 Math.random() - 0.5는:
이 결과로 인해 배열 전체가 무작위 순서로 섞이게 됨
const arr = [1, 2, 3, 4, 5];
const shuffled = arr.sort(() => Math.random() - 0.5);
console.log(shuffled);
const arr = [1, 2, 3, 4];
arr.sort((a, b) => {
const rand = Math.random() - 0.5;
console.log(`비교: ${a} vs ${b} → 결과: ${rand}`);
return rand;
});
Fisher–Yates Shuffle 추천배열을 간단히 섞고 싶다면
array.sort(() => Math.random() - 0.5)외워두자!