Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that enters 3 integers n, min and max (min ≤ max)
- * and prints n random numbers in the range [min...max]. */
- import java.util.Random;
- import java.util.Scanner;
- public class _11_RandomNumbersInGivenRange {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a whole positive number for count of random numbers N: ");
- int countN = scan.nextInt();
- System.out.print("Enter a 2 Integer numbers, the second number must been non-smaller than first: ");
- int minNum = scan.nextInt();
- int maxNum = scan.nextInt();
- scan.close();
- if (countN > 0 && maxNum >= minNum) {
- System.out.printf("The Random numbers in the range [%d .. %d] are:\n",
- minNum, maxNum);
- Random randomGen = new Random();
- for (int i = 0; i < countN; i++) {
- int randomNumber = minNum + randomGen.nextInt((maxNum - minNum) + 1);
- System.out.print(randomNumber);
- if (i < countN - 1) {
- System.out.print(" ");
- }
- }
- } else {
- System.out.println("Error! - Invalid Input!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement