Math.random():隨機取得亂數

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
2
3
4
5
Math.ceil(Math.random()*10);  
console.log(Math.ceil(Math.random()*10))
//2
//1
//9

Math.round():四捨五入

Math.round(Math.random());

1
2
// 可均衡獲取 0 到 1 的隨機整數。
console.log(Math.round(Math.random()); )
1
2
//Math.floor(Math.random()*10); 
//可均衡獲取 0 到 9 的隨機整數。