JS生成随机数
2020-04-21 本文已影响0人
清霆
const randomNum = (min = 0, max = 0, decimal) => {
if (typeof decimal === 'undefined') {
let minDecimal = String(min);
minDecimal =
minDecimal.indexOf('.') + 1
? minDecimal.length - (minDecimal.indexOf('.') + 1)
: 0;
let maxDecimal = String(max);
maxDecimal =
maxDecimal.indexOf('.') + 1
? maxDecimal.length - (maxDecimal.indexOf('.') + 1)
: 0;
decimal = minDecimal > maxDecimal ? minDecimal : maxDecimal;
}
return (Math.random() * (max - min) + min).toFixed(decimal);
};