Languages
[Edit]
EN

Java 8 - Int Stream API generate list with 10 random numbers

11 points
Created by:
JustMike
3488

Java 8 - generate list with 10 random numbers

int size = 10;
int min = 55;
int max = 123;

List<Integer> randList = new Random().ints(size, min, max)
        .boxed().collect(Collectors.toList());

// [69, 93, 83, 61, 63, 62, 107, 82, 110, 103]
System.out.println(randList);


Above example uses IntStream.ints with 3 args

IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

Param description:

@param streamSize the number of values to generate
@param randomNumberOrigin the origin (inclusive) of each random value
@param randomNumberBound the bound (exclusive) of each random value


Reference:

Random - Java Docs
IntStream - Java Docs

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.