js生成指定范围的随机数
作者:互联网
/**
* 获取指定随机数
* randomNumer(最大值,最小值,保留几位小数)
*/
export function randomNumer(maxNum, minNum, decimalNum) {
// 获取指定范围内的随机数, 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));
break;
case 2:
return Math.floor(Math.random() * (max - min + 1) + min);
break;
case 3:
return (Math.random() * (max - min) + min).toFixed(decimalNum);
break;
default:
return Math.random();
break;
}
}
标签:randomNumer,decimalNum,指定,minNum,随机数,js,小数 来源: https://www.cnblogs.com/melancholys/p/15043927.html