34. Filling a long array with pseudo-random numbers
When we want to fill up a large array with data, we can consider the Arrays.setAll()
and Arrays.parallelSetAll()
. These methods can fill up an array by applying a generator function to compute each element of the array.
Since we have to fill up the array with pseudo-random data, we should consider that the generator function should be a pseudo-random generator. If we want to do this in parallel, then we should consider the SplittableRandom
(JDK 8+)/SplittableGenerator
(JDK 17+), which are dedicated to generating pseudo-random numbers in isolated parallel computations. In conclusion, the code may look as follows (JDK 17+):
SplittableGenerator splittableRndL64X256
= RandomGeneratorFactory
.<SplittableGenerator>of("L64X256MixRandom").create();
long[] arr = new long[100_000_000];
Arrays.parallelSetAll(arr,
x ->splittableRndL64X256.nextLong());
Alternatively, we can use SplittableRandom...