Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8022807

j.u.SplittableRandom does not (always) generate identical sequences of values for the same seed if parallel stream is consumed

    XMLWordPrintable

Details

    Description

      Constructor SplittableRandom(long) specifies:

      "SplittableRandom instances created with the same seed in the same program generate identical sequences of values."

      Implementation does not conform to this assertion when consuming parallel version of a sized stream which contains pseudo random values.

      Please see the following code sample:
      -------------------------------------------------------
              // identical
              System.out.println("Sequential: ");
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).toArray()));
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).toArray()));
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).toArray()));
              SplittableRandom splittableRandom = new SplittableRandom(0);
              System.out.println("[" + splittableRandom.nextInt() + ", " + splittableRandom.nextInt() + ", " + splittableRandom.nextInt() + "]");

              // identical but different from sequential stream
              System.out.println("Parallel toArray():");
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).parallel().toArray()));
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).parallel().toArray()));
              System.out.println(Arrays.toString(new SplittableRandom(0).ints(3).parallel().toArray()));


              System.out.println("Parallel forEach():");
              new SplittableRandom(0).ints(3).parallel().forEach((value) -> System.out.print(value + ", "));
              System.out.println();
              new SplittableRandom(0).ints(3).parallel().forEach((value) -> System.out.print(value + ", "));
              System.out.println();

              System.out.println("Parallel iterator():");
              PrimitiveIterator.OfInt iterator = new SplittableRandom(0).ints(3).parallel().iterator();
              System.out.println("[" + iterator.next() + ", " + iterator.next() + ", " + iterator.next() + "]");
      -------------------------------------------------------

      The output will be like:

      Sequential:
      [-720254151, -1440508302, -800994831]
      [-720254151, -1440508302, -800994831]
      [-720254151, -1440508302, -800994831]
      [-720254151, -1440508302, -800994831]
      Parallel toArray():
      [-413681818, 352542945, -800994831]
      [-413681818, 352542945, -800994831]
      [-413681818, 352542945, -800994831]
      Parallel forEach():
      -800994831, 352542945, -413681818,
      352542945, -800994831, -413681818,
      Parallel iterator():
      [-720254151, -1440508302, -800994831]


      One problem is that parallel version of the stream produces values different than the golden sequence for a particular seed. If this is expected it needs to be specified more explicitly.

      Another problem is that parallel stream produces different values for different types of stream consumption.


      The following tests will fail due to this:
      api/java_util/SplittableRandom/index.html#IntsSized[checkParallel]
      api/java_util/SplittableRandom/index.html#LongsSized[checkParallel]

      Attachments

        Issue Links

          Activity

            People

              psandoz Paul Sandoz
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: