A developer wants to create a stream that repeatedly generates random integers. Which method should be used?
This creates an infinite stream of random ints; it's the idiomatic choice for primitive streams.
Why this answer
Option C is correct because `IntStream.generate()` accepts a `Supplier<Integer>` and produces an infinite sequential unordered stream, making it ideal for repeatedly generating random integers. The lambda `() -> new Random().nextInt()` supplies a new random integer each time the stream is evaluated, fulfilling the requirement exactly.
Exam trap
The trap here is that candidates confuse `Stream.generate()` with `Stream.iterate()` or `Stream.of()`, mistakenly thinking that a single random call or a finite range can produce an infinite stream of random values, when only `generate()` with a `Supplier` correctly models repeated independent generation.
How to eliminate wrong answers
Option A is wrong because `IntStream.range(0, 100)` generates a finite stream of integers from 0 to 99, not random values. Option B is wrong because `Stream.of(new Random().nextInt())` creates a single-element stream containing one random integer, not a stream that repeatedly generates random integers. Option D is wrong because `Stream.iterate(0, i -> new Random().nextInt())` produces an infinite stream but the first element is always 0, and the seed value is never used in the generation logic, which is not a clean or intended approach for random generation; additionally, `Stream.iterate` is typically used for sequential transformations, not for independent random suppliers.