+ 1

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

2nd Aug 2016, 6:48 PM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
4 Antworten
+ 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; }
4th Oct 2016, 11:56 AM
Sjoerd Flameling
Sjoerd Flameling - avatar
+ 1
no sorry I meant Math.floor(Math.random(1) * 10); but random does take a parameter. the minimum number.
3rd Aug 2016, 5:11 AM
Mark Waid
Mark Waid - avatar
0
yes. you want to use. Math.floor(Math.random(10) * 1). will give random whole number between 1 and 10.
3rd Aug 2016, 4:55 AM
Mark Waid
Mark Waid - avatar
0
But the random() function doesn't take a parameter. So i think you mean Math.floor(Math.random()*10). @Mark
3rd Aug 2016, 4:58 AM
Sumitabha Banerjee
Sumitabha Banerjee - avatar