Kaper Townnn Kaper Townnn - 2 months ago 4x
Java Question

How do I "keep" generated integers?

How to I "keep" random generated ints in a list? I'd like to be able to access this list and extract data from it. Should I use an array?

Here's how i generate randoms integers:

Random rnd = new Random();
int month = rnd.Next(1, 13);


You can simply create a list and add them

List<Integer> list = new ArrayList<>();
Random rnd = new Random();
list.add(rnd.Next(1, 13));
list.add(rnd.Next(1, 13));

You can also use streams:

List<Integer> list = IntStream.generate(() -> rnd.Next(1, 13)).limit(HOW_MANY_NUMBERS_YOU_WANT).boxed().collect(Collectors.toList())