Get random number - Java program

Q. Write a Java Program to get random number.

Answer:

Java provides the random method and class to generate the random number.
In this example, we are using both random method and random class to get random number.

RandomNumber.java

import java.util.*;
class RandomNumber
{
        public static void main(String[] args)
        {
                //Using random class object
                Random ran = new Random();
                System.out.println("Random number using Random class:");
                for (int i=1;i<=5 ;i++ )
                {
                        System.out.println(ran.nextInt(100));
                }
                //using ramdom() method
                System.out.println("\nRandom number using random method:");
                for (int i=1;i<=5 ;i++ )
                {
                        System.out.println(Math.random());
                }
        }
}


Output:

random number