You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2021/04/12 21:22:00 UTC

[jira] [Created] (RNG-130) UnitSphereSampler for 1 dimension can return invalid length samples

Alex Herbert created RNG-130:
--------------------------------

             Summary: UnitSphereSampler for 1 dimension can return invalid length samples
                 Key: RNG-130
                 URL: https://issues.apache.org/jira/browse/RNG-130
             Project: Commons RNG
          Issue Type: Bug
          Components: sampling
    Affects Versions: 1.3
            Reporter: Alex Herbert


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:

{code:java}
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);
{code}
Outputs:

{noformat}
139977 / 1000000 (13.998)
{noformat}

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-128] which is creating specialisations for sampling for lower order dimensions.





--
This message was sent by Atlassian Jira
(v8.3.4#803005)