A class provided in Java from the beginning, java.util.Random java.lang.Math There are these two.

This time, I will briefly explain the random method of Math class (java.lang.Math). Math.random() point -The return type is double ・ Get random numbers in the range of 0.0 to less than 1.0

Example ①double rand = Math.random(); ②double rand = Math.random() * 10; ③int rand = (int)(Math.random() * 10);

(1) Since the return type is double, a compile error will occur unless the variable type is basic double. (2) When acquiring a random number in the range of 0.0 to less than 10.0, add * 10 after the random method. (3) If you want to get a random number as an integer, you need to cast (2) to int type.

Recommended Posts