0

How do I make a method that has an array of size x, fill it with random numbers between y and z, then return the array? When I call the method I will have a certain size of the array(x) and then I will have the range of z and y.

1 Answers1

0

Use Random.ints(streamSize, origin, bound) to create an IntStream of random values which are collected into array:

public static int[] buildArray(int size, int from, int to) {
    return new Random() // or ThreadLocalRandom.current()
            .ints(size, from, to) // IntStream
            .toArray();
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42