Online Tutorials & Training Materials | STechies.com
Register Login

Generate Random Number in Java

23 Sep 2019 7:29 am || 0

Java Math.random() method is math function used to generate and return pseudorandom double type numbers with a positive sign, that is greater than or equal to 0.0 and less than 1.0

When we call Math.random(), a java.util.Random pseudorandom-number generator object is created and used.

Math.random() function can be used with or without passing parameters. If in case you provide parameters, then the Math.random() generates random numbers within the given parameters.

Syntax 

public static double random( )  

Returns

This function returns pseudorandom double type numbers greater than or equal to 0.0 and less than 1.0.

Example of Math.random() in Java

//Java Example program for random function of Math Library

//Importing Math Library
import java.lang.Math;

//Public Class
class Main
{
    //main function
    public static void main (String[] args) throws java.lang.Exception
    {
        //Declaring and Initializing the random number of double type
        double randomNumber = Math.random();
        //Printing the captured random number
        System.out.println("Random Number in between 0.0 to 1.0 : " + randomNumber);
    }
} 

OUTPUT:

Random Number in between 0.0 to 1.0: 0.7882868458541263

Example 2

//Importing Math Library
import java.lang.Math;

//Public Class
class Main
{
    //main function
    public static void main (String[] args) throws java.lang.Exception
    {
        //For loop for 10 iterations
        for(int i = 1; i <= 10; i++){
            //Declaring and Initializing the random number of double type
            double randomNumber = Math.random();
            //Printing the captured random number on every iteration
            System.out.println(i+" Random Number in between 0.0 to 1.0 : " + randomNumber);
        }
    }
} 

OUTPUT :

1 Random Number in between 0.0 to 1.0 : 0.3003655151707656
2 Random Number in between 0.0 to 1.0 : 0.7364832919535224
3 Random Number in between 0.0 to 1.0 : 0.9583723106877373
4 Random Number in between 0.0 to 1.0 : 0.35063776440084793
5 Random Number in between 0.0 to 1.0 : 0.10083071735555382
6 Random Number in between 0.0 to 1.0 : 0.958161149095786
7 Random Number in between 0.0 to 1.0 : 0.690570863345631
8 Random Number in between 0.0 to 1.0 : 0.08514487681887206
9 Random Number in between 0.0 to 1.0 : 0.32728295048645895
10 Random Number in between 0.0 to 1.0 : 0.46364767051746814

Java Program to Add 10 random numbers

//Importing Math Library
import java.lang.Math;

//Public Class
class Main
{
    //main function
    public static void main (String[] args) throws java.lang.Exception
    {
        //Declaring an array
        double[] rndList = new double[20];
         //For loop for 10 itterations
        double total = 0;
        for(int i = 1; i <= 10; i++){
            //Declaring and Initializing the random number of double type
            double randomNumber = Math.random();
            //Initilizing captured random number on array's `i` index
            rndList[i] = randomNumber;
        }
        //Using this for loop for adding iteration
        for(int j = 1; j <= 10; j++){
            //Add array value on index `j` with the already got total
            total = rndList[j] + total;
        }
        //printing the value of total
        System.out.println(total);
    }
} 

OUTPUT :

4.867967804651614