Wednesday, January 29, 2014

How to generate random numbers within a range?

Source:
import java.util.Random;
 
public class RandomNumber {
   public static void main(String[] args) {
 
      int low =  10;
      int high = 80;
      Random r = new Random();
 
      System.out.println( r.nextInt(high-low) + low );
      System.out.println( r.nextInt(high-low) + low );
      System.out.println( r.nextInt(high-low) + low );
      System.out.println( r.nextInt(high-low) + low );
   }
}

Output:
   $ java RandomNumber 
   37
   28
   14
   35

Blog Archive