Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a random number that's a multiple of 10

I'm looking to create a random number between two ranges that is a multiple of 10.

For example, if I fed the function the parameters 0, 100 it would return one of these numbers:

0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

but nothing like 63 or 55.

And yes I'm aware this defeats the point of true "randomness", but I just need a quick easy way to get a number that's a multiple of 10 between two ranges.

Thanks. :)

like image 438
Josh Avatar asked Dec 03 '22 09:12

Josh


2 Answers

I guess it can help:

var randomnumber=Math.floor(Math.random()*11)*10
like image 188
Michael M. Avatar answered Dec 31 '22 17:12

Michael M.


it's just one line:

function rand_10(min, max){
    return Math.round((Math.random()*(max-min)+min)/10)*10;
}
like image 37
Floern Avatar answered Dec 31 '22 18:12

Floern