[英]What is the purpose of subtracting from Math.random?
我正在瀏覽 javascript.info 並為數組方法做練習任務。 這個是“隨機排列數組”,說明是:
編寫 function shuffle(array) 來打亂(隨機重新排序)數組元素。
我無法弄清楚,所以我查了一下,但現在我很難理解它。 .5 - Math.random
行中的“.5 -”有什么作用?
let arr = [1, 2, 3];
function shuffle(array) {
return arr.sort((a, b) => .5 - Math.random())
}
console.log(shuffle(arr))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.