Java

What is Math.random() and How to use it in Java?

Java provides a predefined class named Math that belongs to the java.lang package and is used to perform various numeric operations such as finding square root, logarithm, etc. To achieve such functionalities, the Math class presents different built-in methods like sqrt(), random(), ceil(), etc. If we talk about Math.random() method it is one of the simpleset and widely used methods of java.lang.Math class used to generate a random floating number between 0.0 to 1.0.

This article will explain what Math.random() is and how to use it in java with help of suitable examples. So, let’s begin!

What is Math.random() in Java?

It is a predefined method of java.lang.Math class that returns a pseudo-random number between the below given range:

0.0 <= Math.random() < 1.0

The above snippet shows that the math.random() method will return at least 0.0 and at most 0.99999999.

How to use Math.random() method in Java

In this section, we will learn the different aspects of the Math.random() method in java. To do so, we will consider some examples and implement them practically.

Example1
Let’s consider the below example which will provide the basic understanding of how to use Math.random() method in java:

public class RandomMethod {
    public static void main(String[] args) {
        System.out.println("Resultant random value= " + Math.random());
    }
}

The above code block will return a random number between 0.0(included) to 1.0(not included):

The above snippet verifies the working of Math.random() method.

Example2
How to generate 10 random numbers between a user-specified range(let’s say 1 to 100) using the Math.random() method:

public class RandomMethod {

    public static void main(String[] args) {
        int upBound = 100;
        int lowBound = 1;
        int range = (upBound - lowBound) + 1;
        System.out.println("Ten Random values between 1 and 100: ");

        for (int i = 1; i <= 10; i++) {
            int randomValues;
            randomValues = (int) (Math.random() * range) + lowBound;
            System.out.print(randomValues + "\n");
        }
    }
}
  • This program performed the following functionalities:
  • Firstly, we created two values and assigned them 100 and 1 as upper bound and lower bound respectively.
  • Next, we specified the range as “ (upBound – lowBound) + 1 “, here “+1” represents that the upper bound is also included.
  • Next, we utilized the traditional for-loop which will iterate ten times.
  • Within for-loop we created an int type variable to store the random values.
  • We utilized (int) to convert the double value into an integer value.
  • Next, we utilized the Math.random() method to get the random numbers, and afterward, we multiplied the returned values of the Math.random() method with the specified range to get the values/numbers according to the user-specified range:

Output verified the working of math.random() method.

Conclusion

Math.random() is a predefined method of java.lang.Math class that returns a pseudo-random number between the following range “0.0 <= Math.random() < 1.0”. The Math.random() method can also be used to generate a value between the customized/user-specified range. This article explained the various aspects of java’s Math.random() method e.g. what is Math.random() and how to use it in java. Moreover, it considered some examples to provide a profound understanding of the Math.random() method.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.