Java

# How to Use Math.random() Method in Java?

Java provides numerous ways to generate a random numeric value, and among them the most frequently used is Math.random() method. Java presents a built-in class named  java.lang.Math that offers several mathematical methods including Math.random() method. The Math.random() method is a static method so it can be invoked/called without creating the object of the math class.

This write-up will provide a thorough overview of the below-listed concepts related to Math.random() method:

So, let’s begin!

## What is Math.random()?

It is a built-in method that belongs to java’s java.lang.Math class that is used to generate a random value of double data type. The generated value will be greater than or equal to 0.0 and less than 1.0.

## Basic Syntax

The basic syntax of the Math.random() method is shown in the below-given snippet:

public static double random()  {
}

## What Does Math.random() Method Return?

The below expression will provide you better clarity of this concept:

0.0 <= Math.random() < 1.0

From the above snippet, you can observe that 0.0 is included while 1.0 is not included. This means the Math.random() method returns a minimum value of 0.0 and a maximum value of 0.99999999(0.9 repeatings).

## How to get Specific Range of Values using Math.random()?

Let’s assume we want to generate a random integer between 0 and 10, can we do this? Yes! In java, we can get a specific range of values using the Math.random() method, and to do so, all we need to do is multiply the returned value of the Math.random() method with the specific range. The below-given expression will provide a better understanding of this concept:

int randval = (int) (Math.random() * 10);

The above snippet will generate random values between 0 and 10(10 not included).

In order to include 10 as well, we have to specify the range as (n+1) i.e. 11:

int randval = (int) (Math.random() * 11);

Now the above snippet will generate random values between 0 and 10.

## How to use Math.random() in Java

Let’s consider the below-given examples to understand how the Math.random() method works in java.

## Example 1

In this example, we will generate two random values using Math.random() method:

public class RandomMethodExample
{
public static void main(String[] args)
{
double number1 = Math.random();
double number2 = Math.random();
System.out.println("First Number: " + number1);
System.out.println("Second Number: "+number2);
}
}

The above code snippet will generate the following output: Whenever we run this code, we will get a different number because the Math.random() generates a random numeric value.

Let’s consider another example to generate a random value between a specific range.

## Example 2

In the below-given snippet, we will generate an integer value between 0 and 10(inclusive):

public class RandomMethodExample {
public static void main(String[] args) {
int number1 = (int) (Math.random() * 11);
System.out.println("First Number: " + number1);
}
}

Every time we execute this program we will get a different random number between the specified range: From the above code snippet we can observe that this time Math.random() method generates a random integer value between the specified range which authenticates the appropriateness of our program.

## Example 3

In the above examples, we have seen that we can specify the range of values but in all these cases the initial value is zero.

However, we can specify the initial range/value as well and in such a case, the Math.random() method will generate a random number between the initial value and (initial value + (final value-1)). The below code snippet will assist you to understand this concept:

public class RandomMethodExample {

public static void main(String[] args) {

int number1 = 5 + (int) (Math.random() * 11);
System.out.println("Number: " + number1);

}

}

In the above snippet, we specified the initial value as “5” and the final value as “11”. Now, the Math.random() method will generate a random number between 5 and  15 (5 + (11-1)). Output verifies the working of the Math.random() method.

## Conclusion

In java, the Math.random() method of java’s Math class is used to generate a pseudo random double value between 0.0 and 1.0. Math.random() generates a random value so whenever a program will execute it will generate a different value. Using the Math.random() method a random value can be generated within a specified range of your choice. This write-up covers different aspects of the Math.random() method such as what is Math.random(), what does it return, how to specify a particular range for the random numbers, and how to use the Math.random() method in java. 