blog icon indicating copy to clipboard operation
blog copied to clipboard

我怎样才能真正洗牌一个数组?

Open wuxianqiang opened this issue 5 years ago • 0 comments

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

wuxianqiang avatar Jan 09 '20 02:01 wuxianqiang