Random numbers in Java

Source code :

import java.util.Random;
public class RandomNumbers{
 
    public static void main(String args[])
    {
       
        Random rand = new Random();
 
        int rand_int1 = rand.nextInt(10000);
        
        System.out.println("Random Integers: "%04d%n",rand_int1);
      
        
    }
}

Output :

Random Integer: 3915 

Algorithm :

  1. Import random class
  2. Initialize the random function variable
  3. Use rand.nextInt(10000) keyword
  4. Use "%04d%n" for 4 digit random number

Output :


                

Comments :