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);
};
网友评论