-4
import java.util.Scanner;
import java.util.Random;

public class Lab04b 
{       
    public static void main(String []args)
    {    
        Random generator = new Random (); 
        Scanner scan = new Scanner(System.in);

        int num1;
        int num2;
        int num3;

        System.out.println("Enter X:");
        num1 = scan.nextInt();

        System.out.println("Enter Y:");
        num2 = scan.nextInt();

        num3 = generator.nextInt(num2) + num1;
        System.out.println("3 random integers in the range " + num1 + ".." + num2 + " are: " + num3);
    }
}

I am stuck on how to get 3 random integers between the x and y range. Y being the biggest integer.

Floris Velleman
  • 4,848
  • 4
  • 29
  • 46

5 Answers5

1

The trick is finding the difference between x and y. Here is what you need to do -

int diff = Math.abs(num1 - num2);
num3 = generator.nextInt(diff) + Math.min(num1, num2);

Just do it 3 times and you get your 3 numbers.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
1

From the docs

nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this
random number generator's sequence.

so random.nextInt(Y) would give you numbers 0..Y, I guess you are missing how to get the lower bound correctly.

X + random.nextInt(Y-X) does the trick.

Ledhund
  • 1,228
  • 10
  • 24
0

Firstly have a loop which will run 3 times, to generate 3 random numbers(as you said you need 3 random numbers, but you're just generating only 1).

Next, the pattern you've used to generate a random number seems to be flawed. You can use the below type 1 pattern to accomplish that.

min + Math.random() * ((max - min) + 1));

Or this type 2 pattern

rand.nextInt((max - min) + 1) + min;

So you can do something like this:-

for (int i = 0; i < 3; i++) {
    // Type 1
    num3 = num1 + (int)(Math.random() * ((num2 - num1) + 1));
    // Type 2
    // num3 = generator.nextInt((num2 - num1) + 1) + num1;
    System.out.println("3 random integers in the range " + num1 + ".." + num2 + " are: " + num3);
}

P.S:- You need to first determine the max and min, yourself. I've just given the pattern and a sample.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Read the documentation. The nextInt(n) function returns an Integer in [0, n). So, in your case, you can use the formula min + nextInt(max-min), which will give you a number in [min, max).

Random generator = new Random(); 
int max = (x >= y ? x : y);
int min = (x < y ? x : y);
int aRandomNumber = min + generator.nextInt(max-min);
aUserHimself
  • 1,589
  • 2
  • 17
  • 26
0
import java.util.Scanner;

public class GenerateRandomX_Y_numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the numbers x and y: ");
        int x = Math.abs(sc.nextInt()), y = Math.abs(sc.nextInt());//we need //non-negative integers, that is why we use here Math.abs. which means the //absolute value

        print3RandomNumbers_between_x_and_y(x, y);

    }

public static void print3RandomNumbers_between_x_and_y(int x, int y) {//here //I create a method with void type that takes two int inputs
    boolean isTrue = (x < y);//according to our conditions X should less //than Y

    if (isTrue) {//if the condition is true do => generate three int in the //range x .... y
        int rand1 = (int) (Math.random() * (y - x) + 1);// y - x means our //range, we then multiply this substraction by Math.random()
        int rand2 = (int) (Math.random() * (y - x) + 1);//the productof this //multiplication we cast to int type that is why we have 
        int rand3 = (int) (Math.random() * (y - x) + 1);//(int) before //(Math.random() * (y - x));

        System.out.println("rand1 = " + rand1);//
        System.out.println("rand2 = " + rand2);//
        System.out.println("rand3 = " + rand3);//here print our result

    } else
        System.out.println("Error input: X should be less than Y. Try it again!");//if the condition is not true, i mean if x is not less than or equal //to Y, print this message
}

}

Cleb
  • 25,102
  • 20
  • 116
  • 151