How to create a random number generator using Reactor?

I want to simulate measurement data by emitting random numbers repeatedly. I tried to do it with Reactor, but it doesn’t produce any output:

private static Random random = new Random();

public static void main(String[] args) throws InterruptedException {
    Flux<Double> doubleGenerator = Flux.generate(
            () -> random.nextDouble(),
            (ignored, sink) -> {
                return 0.0;
    Flux<Long> timer = Flux.intervalMillis(1000);
    Flux.combineLatest(doubleGenerator, timer, (value, ignored) -> value)

How can I create a stream of random values with Reactor?

Not sure why are you trying this way, but you can try generating a UUID


And remove the dashes, pretty long hex number, always different.


If you want just the decimal, you can try:

String lUUID = String.format("%040d", new BigInteger(UUID.randomUUID().toString().replace("-", ""), 16));

I solved this using Reactor 3.1 so Flux.intervalMillis(long) has changed to Flux.interval(Duration) in my solution below. Essentially I create a Flux that emits a number at an interval (once per second in my case) and then maps that to a random number.

final Random random = new Random();
        .map(pulse -> random.nextDouble())

I just did my example in a plain main() method, so in order for the Flux to actually be around long enough to emit data, I added that sleep.