random() range | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

random() range

How do I get a random number between a certain range in JavaScript? Say range 95 to 100 for example

26th Nov 2017, 3:23 PM
Owolawi Kehinde
Owolawi Kehinde - avatar
2 Answers
+ 14
you can use this function randVal(min,max){ return Math.floor(Math.random()*(max-min+1)+min); } then just pass the min and max values var x=randVal(95,100);
26th Nov 2017, 3:27 PM
Burey
Burey - avatar
+ 4
Math.random() produces a random no. between 0 and 0.99.... So we can map it between the no.s we want by multiplying it with the max no. , remove the decimal , adding min as @Burey wrote , Try solving it on a paper with random no. between 0 and 1 then you will understand it better
26th Nov 2017, 6:14 PM
Utkαrsh
Utkαrsh - avatar