js獲取0-1之間的隨機數,獲取1-10之間的隨機數
1.js
//獲取0-1之間的隨機數
var num = Math.random();
-
console.log(num);
-
//獲取1-10之間的隨機數
-
var num = Math.floor(Math.random() * 10+ 1);
-
console.log(num);
2.獲取兩個數之間的隨機整數
function getRandomNumberByRange(start, end) {
return Math.floor(Math.random() * (end - start) + start)
}
getRandomNumberByRange(0,100)
效果: