我有这个功能:
Number.random = function(minimum, maximum, precision) {
minimum = minimum === undefined ? 0 : minimum;
maximum = maximum === undefined ? 9007199254740992 : maximum;
precision = precision === undefined ? 0 : precision;
var random = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
return random;
}
目前还没有实现精确度,有没有人对如何实现它有任何好的想法?精度选项的目的是为您提供固定数量的多个小数位,例如:
// A number from 0 to 10 that will always come back with two decimal places
Number.random(0, 10, 2); // 3.14
答案 0 :(得分:5)
试试这个:
Number.random = function(minimum, maximum, precision) {
minimum = minimum === undefined ? 0 : minimum;
maximum = maximum === undefined ? 9007199254740992 : maximum;
precision = precision === undefined ? 0 : precision;
var random = Math.random() * (maximum - minimum) + minimum;
return random.toFixed(precision);
}
它使用.toFixed()函数来设置精度
答案 1 :(得分:1)
您可以尝试将数字乘以10^precision
,对其进行四舍五入,然后将其除以10^precision
。那会给你想要的结果。下面是这个逻辑的粗略实现。您可能希望在precision参数上添加一些错误检查,或者使用其他一些方法来计算(例如Math.pow
)。
Number.random = function(minimum, maximum, precision) {
minimum = minimum === undefined ? 0 : minimum;
maximum = maximum === undefined ? 9007199254740992 : maximum;
precision = precision === undefined ? 0 : precision;
var random = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
for (var i = 0; i < precision; i++) {
random = random * 10;
}
random = Math.round(random);
for (var i = 0; i < precision; i++) {
random = random / 10;
}
return random;
}
答案 2 :(得分:1)
Number.random = function(minimum, maximum, precision) {
minimum = minimum||0;
maximum = maximum ||9007199254740992;
precision =precision ||0;
var random = Math.random() * (maximum - minimum + 1) + minimum;
return random.toFixed(precision);
}
//call Number.random(0,10.10,3)
答案 3 :(得分:1)
试试这个:
Number.random = function(minimum, maximum, precision) {
minimum = minimum === undefined ? 0 : minimum;
maximum = maximum === undefined ? 9007199254740992 : maximum;
precision = precision === undefined ? 0 : precision;
var random = Math.random() * (maximum - minimum + 1) + minimum;
return Math.round(random * Math.pow(10, precision)) / Math.pow(10, precision);
}
答案 4 :(得分:1)
因为你没有定义任何限制w.r.t.你可能想要的precision
,并牢记Javascript中Number
的限制(准确的数字表示),那么你可能会发现string
表示可以接受。在这种情况下,你可能想要做这样的事情。
的Javascript
var maxInteger = 9007199254740992;
function randomInteger(minimum, maximum) {
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
}
Number.random = function (minimum, maximum, precision) {
minimum = typeof minimum !== 'number' ? 0 : minimum;
minimum = minimum < -maxInteger ? -maxInteger : minimum;
maximum = typeof maximum !== 'number' || maximum > maxInteger ? maxInteger : maximum;
precision = typeof precision !== 'number' || precision < 0 ? 0 : precision;
var random = randomInteger(minimum, maximum).toString(),
decimals;
if (precision) {
decimals = '';
while (decimals.length < precision) {
decimals += randomInteger(0, maxInteger)
}
random += '.' + decimals.slice(0, precision);
}
return random;
}
console.log(Number.random(-maxInteger, maxInteger, 40));
上