You have a reluctant +1 according to your estimates. Must be to-fromwithout +1:
var randomNumber = function (from, to, dec) {
var num = Math.random() * (to - from +1) + from;
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
};
Your code should be as follows:
var randomNumber = function (from, to, dec) {
var num = Math.random() * (to - from) + from;
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
};
Actually, it can be shortened by omitting the variable resultas follows:
var randomNumber = function (from, to, dec) {
var num = Math.random() * (to - from) + from;
return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
};
source
share