jeudi 3 décembre 2020

How can create reproducable random numbers

how can create reproducable random numbers in java 8+ parallel Streams?

Example:

Random r = new Random(1337);

Arrays.stream(new int[]{1,2,3})
.parallel()
.map(i -> i + r.nextInt(10))
.forEach(i -> System.out.println(i));

Random and ThreadLocalRandom are not generating the same sequence if i execute serveral times.

My expected output is that if i execute this Code snippet serveral times i get always the same output. I dont need a random sequence of ints. I need that the same random number is generate for the same i.




Aucun commentaire:

Enregistrer un commentaire