Create a random number that is a multiple of 10

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

For example, if I passed a function to parameters 0, 100, it will return one of these numbers:

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

but nothing like that 63or 55.

And yes, I know that this defeats the point of true “randomness," but I just need a quick and easy way to get a number that is a multiple of 10 between two ranges.

Thank.:)

+3
source share
7 answers

I think this may help:

var randomnumber=Math.floor(Math.random()*11)*10
+8
source

this is just one line:

function rand_10(min, max){
    return Math.round((Math.random()*(max-min)+min)/10)*10;
}
+6
source

, :

function GetRandom( min, max ) {
    if( min > max ) {
        return( -1 );
    }
    if( min == max ) {
        return( min );
    }

    return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

( " 1" ), 10 10.

randomNumberMultipleOfTen = GetRandom(0,10) * 10;

, , , .

0
  • .
  • 10.
  • 0 .
  • 10.

.

0

Math.floor(Math.random() * 10) * 10

, .

0
var a = 67;
var b = 124;
var lo = a + 10 - (a % 10) 
var hi = b - (b % 10)
var r = lo + 10 * parseInt(Math.random() * ((hi - lo)/10 + 1));
0
function rand(maxNum, factorial) { 
    return Math.floor(Math.floor(Math.random() * (maxNum + factorial)) / factorial) * factorial; 
};

  • maxNum ,
  • factorial /

    • , maxNum.
    • .
    • .
    • .
    • then again multiplied by factorial.
0
source

Source: https://habr.com/ru/post/1796152/


All Articles