How Do I Return A Number Of Random Numbers From This String?

19 October 2018 - 1 answer

I have converted my string, `just_numbers`, to an array format with:

``````System.out.println("just_numbers" + Arrays.toString(just_numbers));
``````

It looks like `[28, 27, 49, 25, 2, 23, 48, 18, 57, 58, 60, 20]`

How can I select 3 random numbers from the above? The order isn't important.

Right now I am trying with:

``````            Random random = new Random();

//make random numbers
int number1 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
int number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);

while (number1 != number2 && number1 != number3) {
number2 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);
number3 = Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]);

}

System.out.println("random numbers are :" + number1 + " " + number2  + " " + number3);
``````

But I am still getting things like:

``````            random numbers are : 25 25 58
``````

That is - duplicates, where each number shoud be listed just once.

``````private Integer[] getThreeRandomNumber() {
HashSet<Integer> integers = new HashSet<>(3);
Random random = new Random();
while (integers.size() < 3) {
// add number generation logic below