Advertisement
dimipan80

Random Numbers in Given Range

Aug 8th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. /* Write a program that enters 3 integers n, min and max (min ≤ max)
  2.  * and prints n random numbers in the range [min...max]. */
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class _11_RandomNumbersInGivenRange {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.print("Enter a whole positive number for count of random numbers N: ");
  13.         int countN = scan.nextInt();
  14.         System.out.print("Enter a 2 Integer numbers, the second number must been non-smaller than first: ");
  15.         int minNum = scan.nextInt();
  16.         int maxNum = scan.nextInt();
  17.         scan.close();
  18.  
  19.         if (countN > 0 && maxNum >= minNum) {
  20.             System.out.printf("The Random numbers in the range [%d .. %d] are:\n",
  21.                     minNum, maxNum);
  22.             Random randomGen = new Random();
  23.             for (int i = 0; i < countN; i++) {
  24.                 int randomNumber = minNum + randomGen.nextInt((maxNum - minNum) + 1);
  25.                 System.out.print(randomNumber);
  26.                 if (i < countN - 1) {
  27.                     System.out.print(" ");
  28.                 }
  29.             }
  30.         } else {
  31.             System.out.println("Error! - Invalid Input!!!");
  32.         }
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement