js 随机生成之间的数(包括小数和负数)
2019-04-25 本文已影响0人
凡凡的小web
getRandomNum: function(minNum, maxNum, decimalNum) {
var max = 0, min = 0
minNum <= maxNum ? (min = minNum, max = maxNum) : (min = maxNum, max = minNum)
switch (arguments.length) {
case 1:
return Math.floor(Math.random() * (max + 1))
case 2:
return Math.floor(Math.random() * (max - min + 1) + min)
case 3:
return (Math.random() * (max - min) + min).toFixed(decimalNum)
default:
return Math.random()
}
},