+ 1

# Can we generate a random number within a given range using javascript?

4 Answers

+ 3

You can also use this code:
function randomNumberBetween(a, b) {
var minNum = a;
var maxNum = b;
var output = 0;
output = Math.random();
output = output * (maxNum - minNum);
output = output + minNum;
output = Math.round(output);
return output;
}

+ 1

no sorry I meant Math.floor(Math.random(1) * 10);
but random does take a parameter. the minimum number.

0

yes. you want to use. Math.floor(Math.random(10) * 1). will give random whole number between 1 and 10.

0

But the random() function doesn't take a parameter. So i think you mean Math.floor(Math.random()*10). @Mark