You are here

Java program to generate random numbers

Java program to generate random numbers. We print 10 random numbers in the range [0, 100].

Program to generate random numbers in Java

import java.util.*;

class RandomNumbers {
  public static void main(String[] args) {
    int c;
    Random t = new Random();

    // random integers in [0, 100]

    for (c = 1; c <= 10; c++) {
      System.out.println(t.nextInt(100));
    }
  }
}

Download Random Numbers program class file.

Output of program:
Java random numbers program output

Method nextInt(x) returns an integer in the range of 0 to x (both inclusive), x must be positive. To generate random float's use nextFloat, which returns a floating-point number between 0.0 to 1.0.