[Java] [Java] Random method

less than 1 minute read

Java.util.Random java.lang.Math, a class that comes with Java from the beginning There are these two.

This time, I will briefly explain the random method of Math class (java.lang.Math). Math.random() point

  • Return value type is double ・Obtain a random number in the range of 0.0 to less than 1.0

An 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 obtaining a random number in the range of 0.0 to less than 10.0, add * 10 after the random method. (3) To obtain a random number with an integer, it is necessary to cast (2) to an int type.