How to create a random number in java between 450 and 150, which is a multiple of 10?

I still have this code

int w = (int)((450-150)*random()+150);  

This generates a number from 450 to 150 ... But I do not know how to make this number a multiple of 10.

+5
source share
5 answers

Easy. Just create random numbers between 15 and 45 ... then multiply by 10:

int w = ((int)((45 - 15) * random() + 15)) * 10;
+13
source

Choose a random number between 45 and 15 and multiply it by 10 -

int w = (int)((45-15)*random()+15) * 10; 
+11
source

, :

, , , .

30 , ? 0 30, .

( 10 150.)

+7

15 45, 10:

int w = 10*(int)((45-15)*random()+15);

+4

, , OP : 150 450, , , .

Math.random() 0.0 () 1.0 (). 15 45 ( ), (45 - 15 + 1)... ..: 31 .

:

int w = ( (int)(((45 - 15 + 1) * random()) + 15) ) * 10;

(45 - 15 + 1) * random() = [0.0..30.999999)
[0..31) + 15 = [15,0..45,999999)
(int) [15.0..46.0) = [15..45] - , [15..45] * 10 = {150, 160, 170, 180,..., 440, 450}

+1

All Articles