Home >>Java Math Methods >Java Math.random() Method

Java Math.random() Method

Java Math.random() Method

The Math.random() in Java Math is used to return a double type number pseudorandom greater than or equal to 0.0 and less than 1.0. The random number generated by default always between 0 and 1.

Syntax

public static double random( )  

Returns

It is used to returns a pseudorandom double value greater than or equal to 0.0 and less than 1.0.

Java Math.random() Method Example 1


public class MyClass  
{  
    public static void main(String[] args)   
    {  
        double x = Math.random();  
        double y = Math.random();  
        System.out.println(x);  
        System.out.println(y);  
    }  
}  

Output:
0.3245082772470055
0.6309499015023217

Java Math.random() Method Example 2


public class MyClass 
{  
    public static void main(String[] args)   
    {  
        double x = Math.random() * 15;  
        double y = Math.random() * 15;  
        System.out.println(x);  
        System.out.println(y);  
    }  
}  

Output:
4.505532884543209 2.082995789994383

No Sidebar ads