Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

new_bird-hyun

JavaScript - 배열 요소를 랜덤 섞기 (Fisher-Yates Shuffle) 본문

코딩 공부

JavaScript - 배열 요소를 랜덤 섞기 (Fisher-Yates Shuffle)

새혀니 2025. 4. 13. 12:44

function shuffle(array) {
  const result = [...array];
  for (let i = result.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [result[i], result[j]] = [result[j], result[i]];
  }
  return result;
}

console.log(shuffle([1, 2, 3, 4, 5]));