Math.random():隨機取得亂數
const oneMathRandom=Math.random(); console.log(oneMathRandom); //0.5852392367031296
Math.floor():取得整數
const twoMathFloorRandom= Math.floor(Math.random() * 5); console.log(twoMathFloorRandom); //4
Math.ceil()無條件進位
Math.ceil(Math.random()*10);
// 獲取從 1 到 10 的隨機整數,取 0 的概率極小。
1 | Math.ceil(Math.random()*10); |
Math.round():四捨五入
Math.round(Math.random());
1 | // 可均衡獲取 0 到 1 的隨機整數。 |
1 | //Math.floor(Math.random()*10); |