Home » Java programming language

Java StrictMath random() Method with Example

StrictMath Class random() method: Here, we are going to learn about the random() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020

StrictMath Class random() method

  • random() Method is available in java.lang package.
  • random() Method is used to return a random positive double type value which will be greater than or equal to 0.0 but less than 1.0.
  • random() Method is a static method so it is accessible with the class name and if we try to access the method with the class object then also we will not get any error.
  • In this method, we get random values by the pseudorandom-number generator.
  • random() Method generate one new pseudorandom-number generator which is similar to when we write an expression is given below:
    Random r = new Random();
  • random() Method is a synchronized method that means this method is accessible by more than one thread, but one thread at a time.
  • We should go for this method if we generate pseudorandom-number at a huge rate by more than one thread.
  • random() Method does not throw any exception.

Syntax:

    public static double random();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is double, it returns the double floating point number which will be greater than or equal to 0.0 but less than 1.0.

Example:

// Java program to demonstrate the example
// of random() method of StrictMath class.

public class Random {
    public static void main(String[] args) {
        // variable declarations
        double d1 = StrictMath.random();
        double d2 = StrictMath.random();
        double d3 = StrictMath.random();

        // Here , we will get (random value) because we are 
        // passing parameter whose value is (unknown)
        System.out.println("d1: " + d1);

        // Here , we will get (random value) and we are 
        // passing parameter whose value is (unknown)
        System.out.println("d2: " + d2);

        // Here , we will get (random value) and we are 
        // passing parameter whose value is (unknown)
        System.out.println("d3: " + d3);
    }
}

Output

d1: 0.5311570792784119
d2: 0.06676096670488973
d3: 0.7796299828368791


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.