You are here

Java program to generate random numbers

Java program to generate random numbers: This code generates random numbers in range 0 to 100 (both inclusive).

Java programming code

  1. import java.util.*;
  2.  
  3. class RandomNumbers {
  4.   public static void main(String[] args) {
  5.     int c;
  6.     Random t = new Random();
  7.  
  8.     // random integers in [0, 100]
  9.  
  10.     for (c = 1; c <= 10; c++) {
  11.       System.out.println(t.nextInt(100));
  12.     }
  13.   }
  14. }

Download Random Numbers program class file.

Output of program:
Java random numbers program output

nextInt(c) method returns next integer in 0 to c (both inclusive), c must be positive. To generate random float's use nextFloat which returns float between 0.0 to 1.0.