Creating Stream of Generators

If the initial generator implements the interface RandomGenerator.StreamableGenerator, then call the method rngs(), jumps() (for jumpable generators), or leaps() (for leapable generators) to create a stream of generators. Call the map() method on the stream to assign each generator to its own thread.

When you call the jumps() method, the generator changes its state by jumping forward a large fixed distance within its state cycle, then creates a new generator based on the generator’s new state. The generator repeatedly jumps and creates generators, creating a stream of generators. The leaps() method is similar; the size of the jump is much larger.

The following example creates a jumpable generator, then creates a stream of generators based on this initial generator by calling the jumps() method. The first several generators in the stream (defined by NUM_TASKS) are wrapped in a Task instance, then each Task is run in its own thread.

       int NUM_TASKS = 10;
       
       RandomGeneratorFactory<JumpableGenerator> factory =
            RandomGeneratorFactory.of("Xoshiro256PlusPlus");
       JumpableGenerator random = factory.create();
       
       class Task implements Runnable {
            private int p;
            private RandomGenerator r;
            public Task(RandomGenerator prng) {
                r = prng;
            }
            public void run() {
                System.out.println(r.nextLong());
            }
        }
        
        List<Thread> taskList = random
            .jumps()
            .limit(NUM_TASKS)
            .map(prng -> new Thread(new Task(prng)))
            .collect(Collectors.toList());
        taskList.stream().forEach(t -> t.start());