This question has already been answered:

Made a program of random number generator. I wanted to make sure that the user himself could set up the upper threshold of the number. But how to do it? (example):

System.out.println("Введи новое число"); Scanner userNewNum = new Scanner(System.in); int x = userNewNum.nextInt(); Random randNum = new Random(); int randNumVar = randNum.nextInt(1 - x); 

I know it doesn't work. What is the right way then?

Reported as a duplicate by Akina , iksuy , Kromster , zRrr , 0xdb on Dec 13 '18 at 15:58 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • If you find the answer you want, mark it as Correct. Thank you - Vlad Zherihov

2 answers 2

 Random random = new Random(); int num1 = random.nextInt(100); // случайное целое от 0 до 99 int num2 = 36 + random.nextInt(100 - 36); // от 36 до 99 

Instead of 100, substitute your x

    In this program, an array is created where random numbers are written, where you specify the maximum element yourself, in the same way you can specify the minimum element and the size of the array

     int [] array = new int[5]; int minRandom = 1; int maxRandom; Scanner scanner = new Scanner(System.in); System.out.println("введите макс элемент"); maxRandom = scanner.nextInt(); Random random = new Random(System.currentTimeMillis()); for (int i = 0; i < array.length; i++) { array[i] = minRandom + random.nextInt(maxRandom - minRandom + 1); System.out.print(array[i] + ", "); }