You were very close, what you need is not to work with decimal numbers as min and max. Let's have max = 1000 and min = 100, so after your Math.floor you will need to divide by 100:
var randomnum = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
Or if you want to work with decimals:
var precision = 100; // 2 decimals
var randomnum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1*precision);
Answer from Observer on Stack OverflowYou were very close, what you need is not to work with decimal numbers as min and max. Let's have max = 1000 and min = 100, so after your Math.floor you will need to divide by 100:
var randomnum = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
Or if you want to work with decimals:
var precision = 100; // 2 decimals
var randomnum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1*precision);
Multiply the original random number by 10^decimalPlaces, floor it, and then divide by 10^decimalPlaces. For instance:
floor(8.885729840652472 * 100) / 100 // 8.88
function genRand(min, max, decimalPlaces) {
var rand = Math.random()*(max-min) + min;
var power = Math.pow(10, decimalPlaces);
return Math.floor(rand*power) / power;
}
for (var i=0; i<20; i++) {
document.write(genRand(0, 10, 2) + "<br>");
}
Edit in response to comments:
For an inclusive floating-point random function (using this answer):
function genRand(min, max, decimalPlaces) {
var rand = Math.random() < 0.5 ? ((1-Math.random()) * (max-min) + min) : (Math.random() * (max-min) + min); // could be min or max or anything in between
var power = Math.pow(10, decimalPlaces);
return Math.floor(rand*power) / power;
}
I can't find a dupetarget for this, so:
You do what John Coleman said: You generate a random number between 6000 and 6500:
var num = Math.floor(Math.random() * 500) + 6000;
...and divide by 100:
var num = (Math.floor(Math.random() * 500) + 6000) / 100;
// New bit ---------------------------------------^^^^^^
That gives you a number from 60 (inclusive) to 65 (exclusive) with a fractional portion that will in theory be roughly two digits, but because of the way IEEE-754 double-precision binary floating point (the kind of numbers JavaScript uses) works, if you output that, you may get more or fewer digits right of the decimal.
To output it with two digits, you use toFixed(2):
console.log(num.toFixed(2));
Example:
var counter = 0;
tick();
function tick() {
var num = (Math.floor(Math.random() * 500) + 6000) / 100;
console.log(num.toFixed(2));
if (counter++ < 100) {
setTimeout(tick, 250);
}
}
.as-console-wrapper {
max-height: 100% !important;
}
Try the following function
function getRandomArbitrary(min, max) {
return ((Math.random() * (max - min)) + min).toFixed(2);
}
console.log(getRandomArbitrary(60, 65));
If you want the random numbers with 0.1 steps, the easiest would be to generate a random number between 1 and 15, then divide the result by 10.
(Math.floor(Math.random() * 15) + 1) / 10;
Mah.random() * (max - min) + min is always your best bet. If you want to round it to n decimals later, just wrap it like so: Math.round(random * 10 ** n) / (10 ** n)
In the case of one decimal, that is Math.round(10 * (Math.random() * (max - min) + min)) / 10