math random method in java

Math Random Method in Java With Examples

Java is a general-purpose object-oriented programming language, it is class-based, and is created for having lesser implementation dependencies.

Java is a fast, secure, and reliable computing platform for application development. Therefore, it is largely utilized for making Java applications in data centers, game consoles, cell phones, laptops, scientific supercomputers, etc.

Some Important Java Applications Include the Following:

  • It is utilized for creating Android Apps.
  • Helps you to build Enterprise Software.
  • A broad range of Mobile java Applications.
  • Scientific Computing Applications.
  • Used for Big Data Analytics.
  • Used for Server-Side Technologies like JBoss, GlassFish, Apache, etc.
  • Java Programming of Hardware devices.

 

When you’re involved in programming and software development, there can be a situation when you need to produce a random number, sometimes random numbers within a range e.g. 1 to 100, etc.

In Java, several methods can be utilized to generate a random number which makes this random number generation simple and manageable.

Java API offers great support for random numbers through java.util.Random class, Math.random() utility method, and through recently added ThreadLocalRandom class in Java 7, with other more popular features like ARM blocks and String in Switch.

This random() method looks to be the most suitable way of producing random numbers in Java, it just returns random doubles, additionally by utilizing Random, you can further create floating-point numbers and pseudo-random integers.

Example – double and even random Boolean values. For example, if you wish to generate a random number for a guessing game, or assume that you are running a cruise line.

You need to generate a random number that can be added to the end of a customer’s order and is utilized as their booking reference.

Now, we are going to explain the most common method used to generate a random number in Java: Math.random(). We’ll further discuss a step-by-step illustration of how to use the Math.random() method.

What is a Java Platform?

Java Platform is a set of programs that help programmers to create and run Java programming applications. It comprises a compiler, an execution engine, and a collection of libraries. It is a combination of computer software and specifications.

Math Random() Method in Java

Description

The java.lang.Math.random() returns a double value, greater than or equal to 0.0 and less than 1.0. Returned values are determined pseudorandomly with (approximately) equal distribution from that range.

This method produces a single new pseudorandom-number generator, when it is called for the first time, precisely as if by the expression new java.util.Random.

This new pseudorandom-number generator is utilized consequently for all calls to this method and is employed nowhere else.

This method is accurately synchronized to enable correct usage by more than one thread. But, if several threads need to create pseudorandom numbers at a high rate, it may decrease contention for every thread to have its pseudorandom-number generator.

Declaration

Following is the declaration for java.lang.Math.random() method:

public static double random()

For this, we are going to use one method from the Math library:

Java Math Library

The Java Math class includes several features utilized to implement mathematical functions on numbers. For example, the Math library incorporates the round() method that is employed to round a number and the pow() method that is utilized to calculate the power of a number.

To utilize the Java Math library, we need to first import it into our code. We can do this by using an import statement like this:

import java.lang.Math;

Now, let us see how to use Math.Random() in java, with the help of some examples.

1) Math.Random() Example 1

Mentioned below is an example of the Math.random() method in action:

import java.lang.Math;

class Main {

public static void main(String[] args) {

double number = Math.random();

System.out.println("Random number: " + number);

}

}

 

2) Math.Random() Example 2

// generate random numbers between 0 to 4

    public static void main(String[] args) {

        // Math.random() generates random number from 0.0 to 0.999

        // Hence, Math.random()*5 will be from 0.0 to 4.999

        double doubleRandomNumber = Math.random() * 5;

        System.out.println("doubleRandomNumber = " + doubleRandomNumber);

        // cast the double to whole number

        int randomNumber = (int)doubleRandomNumber;

        System.out.println("randomNumber = " + randomNumber);

    }

    /* Output #1

    doubleRandomNumber = 2.431392914284627

    randomNumber = 2

    */


3) Math.Random() Example 3

public class MathExample {

    public static void main(String[] args) {

        int min = 20;

        int max = 80;

        int n = (int) (min + (max-min)*Math.random());

        System.out.println(n);

    }

}

 

Conclusion

That’s all on how to generate random numbers in Java using Math.Random() in java. Now you must have got a clear understanding of what Java Math.random() function is, with example programs.

Defined above were examples of generating random integers in a range say 1 to 10, which is pretty common and extremely useful as well.

You can even utilize ThreadLocalRandom from Java 1.7, which is a Random number generator separated to a particular thread, which decreases contention if used in a multi-threaded environment.

Math.random() utilizes the random class. You can use this Math.random() function if you want double type pseudo-random numbers in your application.

You can likewise use this method here to produce random numbers in a range or without replication by adding that extra logic on top of this. Happy Coding!