Approach 1: Using Stream.generate()
List<Double> rnd = Stream.generate(Math::random).limit(5).collect(Collectors.toList()); System.out.println(rnd);
Why it works:
- Infinite supplier stream limited to 5 elements.
Approach 2: Using Random.ints()
(Alternative)
Random r = new Random(); List<Integer> ints = r.ints(5, 1, 101).boxed().collect(Collectors.toList()); System.out.println(ints);
Why it works:
- Produces bounded integers directly; boxed to Integer list.