Friday, January 20, 2012

Java program to select a random number within a range


import java.util.Random;
public class Exercise
      {
       public static void main(String arg[])
              {
              Random rndNumbers = new Random();
              int min=10, max=50;
               int randomNum = rndNumbers.nextInt(max - min + 1) + min;
               System.out.println("Number: " + randomNum);
              }
      }


The above given program select a number randomly within the range 10 (min=10) and 20 (max=20).
Save the program as Exercise.java

No comments :

Post a Comment