Uploaded image for project: 'Commons RNG'
  1. Commons RNG
  2. RNG-130

UnitSphereSampler for 1 dimension can return invalid length samples

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.3
    • 1.4
    • sampling
    • None

    Description

      The UnitSphereSampler for 1 dimension currently creates a sample from a standard normal distribution and then normalises it to unit length. This can create values that are not 1 or -1.

      The following code shows this occurs approximately 14% of the time:

      final UniformRandomProvider rng =
          RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP, 0x1a2b3cL);
      final UnitSphereSampler generator = new UnitSphereSampler(1, rng);
      int count = 0;
      int size = 1000000;
      for (int i = size; i-- > 0; ) {
          final double[] v = generator.nextVector();
          if (Math.abs(v[0]) != 1.0) {
              count++;
          }
      }
      System.out.printf("%d / %d (%.3f)%n", count, size, 100.0 * count / size);
      

      Outputs:

      139977 / 1000000 (13.998)
      

      This can be fixed by switching the sampling algorithm to use a bit from the random generator to pick either a 1 or -1 for the return value. This can be fixed as part of RNG-129 which is creating specialisations for sampling for lower order dimensions.

      Attachments

        Issue Links

          Activity

            People

              aherbert Alex Herbert
              aherbert Alex Herbert
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: