• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

Implementation of random() Method of Math Class in Java

October 1, 2023 //  by Talha Malik

The math class consists of numerous methods and functions that are applied when needed to perform mathematical operations in Java. One of these methods is called the random() method belongs to the Math class which is static in nature. The random() method in Java is frequently required to perform tasks like dice rolling, statics, and hypothesis testing.

In this write-up, we will demonstrate in detail the implementation of the random() method of Math class in Java.

Implementation of random() Method of Math Class in Java

The random() method is static in nature therefore it can be called without declaring the class istance. The random() method by default returns random numbers larger than or equal to 0.0 and less than 1.0. The numbers are randomly generated therefore the output appears to be different every time the code is executed.

Example 1: Basic Implementation of Math.random() Method

The below code depicts the default implementation of the Math.random() method in Java.

//Declare the class for the Random method
public class RandomMethod1
{ 
    //Declare the Main class
    public static void main(String[] args)  
    {  //To generate a random number
        double x = Math.random(); 
        double y = Math.random(); 
        //The output will be different every time when the code is executed
        System.out.println(x); 
        System.out.println(y); 
    } 
}

In the above code:

  • The class is created for the Random method.
  • Two integer values are declared as x and y.
  • When the numbers are not declared, the random numbers appear between 0 and 1.

Output

The below output shows that random numbers appear between 0 and 1. The output varies each time the code executes.

Example 2: Printing Random Numbers Using the random() Method

In the code below, the maximum and the minimum values are declared and random numbers appear in between the range of declared minimum and maximum range.

//Import the required package
import java.lang.Math;
//Declare the class for the random method
class RandomMethod {
public static void main(String args[])
{//Declare the range for the random numbers to be printed
System.out.println("The numbers in random order are: ");
int maxim = 15;
//Declare the minimum value
int minim = 2;
//The overall, result is declared using the result
int result = maxim - minim + 1;
//The for loop for initialization
for (int i = 0; i < 10; i++) {
    //The random() method
int random = (int)(Math.random() * result) + minim;
System.out.println(random);
}
}
}

In the above code:

  • A class is declared as ”RandomMethod”.
  • The minimum and maximum values are declared as 2 and 15 respectively.
  • The integer declared as “int” holds the overall result.
  • Using the integer that is declared as “random”, the numbers are printed in random order starting from 2 and ending at 15.

Output

In the output below, the random numbers are printed from the declared minimum value of 2 to the maximum value of 15. The numbers appear in random order each time the code is executed.

Conclusion

To implement the random() method, first, the “java.util.Math” package is imported. After that, the “Math.random()” function can be executed to get the random numbers. In this write-up, we have implemented the random() method using the Math class of Java with examples.

Category: Java

Previous Post: « Secure Hash Algorithm(SHA-1) in Java
Next Post: How to Implement Lambda Expressions in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Math.min() Method in Java?

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact