Write the valid statements for the following in Java :-
Print a random integer 120 & 159
Answers
Answered by
0
_________
|ANSWER|
(int)(Math.random()*40) + 120;
Explanation:
Math.random() can be used in java as the following:-
int range=max-min+1;
(first your Max no i.e., 159 and min no i.e., 120 is substracted and added by one and then stored in a variable or you can just calculate it..)
(int)(Math.random()*range) +min;
(then use Math.random function multiply with the variable where you had stored the range earlier or just can calculate the range and then add the whole answer with minimum digit in this case it was 120. and then convert your answer to "int" data type so that the no generated by Math.random is not in fraction)
Similar questions