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:
- What is Math.random()?
- Basic Syntax
- What Does Math.random() Method Return?
- How to get a Specific Range of Values using Math.random()?
- How to use Math.random() in Java
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.
The basic syntax of the Math.random() method is shown in the below-given snippet:
What Does Math.random() Method Return?
The below expression will provide you better clarity of this concept:
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:
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:
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.
In this example, we will generate two random values using Math.random() method:
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.
In the below-given snippet, we will generate an integer value between 0 and 10(inclusive):
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.
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:
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.
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.