Java Math.random() Method with Examples

Generating a random number (Java Math Random)

Now let’s try a method, which returns a random double number, between 0.0 and 1.0. Note that 1.0 is not included. Using the random method, we can easily generate a random double number. Let’s have a look at the implementation part now –

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.random());
}
}

If you execute the above program, you get a random double number that is greater than or equal to 0.0, and less than 1.0.
We can choose to use this method whenever we need to generate some random number, along with some other things, as and when needed. For example, if you want to generate random numbers from 0 to 10, you can do this using some methods collectively, and doing some arithmetic operations for getting the random number in the right range.