diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java index 789c05aadc..d1d65118a6 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java @@ -166,7 +166,7 @@ public RealMatrix getRootMatrix() { public double[] get() { // generate uncorrelated vector for (int i = 0; i < normalized.length; ++i) { - normalized[i] = generator.nextNormalizedDouble(); + normalized[i] = generator.getAsDouble(); } // compute correlated vector diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/NormalizedRandomGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/NormalizedRandomGenerator.java index e41a008c44..19fe5024a4 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/NormalizedRandomGenerator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/NormalizedRandomGenerator.java @@ -17,21 +17,19 @@ package org.apache.commons.math4.legacy.random; +import java.util.function.DoubleSupplier; + /** * This interface represent a normalized random generator for * scalars. * Normalized generator provide null mean and unit standard deviation scalars. + * Supply a random scalar with null mean and unit standard deviation. + *

This supplier does not specify the shape of the + * distribution, it is the implementing class that provides it. The + * only contract here is to generate numbers with null mean and unit + * standard deviation.

* @since 1.2 */ -public interface NormalizedRandomGenerator { - - /** Generate a random scalar with null mean and unit standard deviation. - *

This method does not specify the shape of the - * distribution, it is the implementing class that provides it. The - * only contract here is to generate numbers with null mean and unit - * standard deviation.

- * @return a random scalar with null mean and unit standard deviation - */ - double nextNormalizedDouble(); +public interface NormalizedRandomGenerator extends DoubleSupplier { } diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/StableRandomGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/StableRandomGenerator.java index a842544cb8..f8c5e4a1f3 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/StableRandomGenerator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/StableRandomGenerator.java @@ -85,7 +85,7 @@ public StableRandomGenerator(final UniformRandomProvider generator, * @return a random scalar with zero location and unit scale */ @Override - public double nextNormalizedDouble() { + public double getAsDouble() { // we need 2 uniform random numbers to calculate omega and phi double omega = -AccurateMath.log(generator.nextDouble()); double phi = AccurateMath.PI * (generator.nextDouble() - 0.5); diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UniformRandomGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UniformRandomGenerator.java index a8ef515411..be4d7b0a0b 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UniformRandomGenerator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UniformRandomGenerator.java @@ -52,7 +52,7 @@ public UniformRandomGenerator(UniformRandomProvider generator) { * @return a random scalar in the range \( [-\sqrt{3}, +\sqrt{3}] \). */ @Override - public double nextNormalizedDouble() { + public double getAsDouble() { return SQRT3 * (2 * generator.nextDouble() - 1); } } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java index a6622c6f0b..5c19c495b7 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java @@ -200,7 +200,7 @@ private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng return new NormalizedRandomGenerator() { /** {@inheritDoc} */ @Override - public double nextNormalizedDouble() { + public double getAsDouble() { return n.sample(); } }; diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/StableRandomGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/StableRandomGeneratorTest.java index 69899f3146..dfe0ff897c 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/StableRandomGeneratorTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/StableRandomGeneratorTest.java @@ -38,12 +38,12 @@ public class StableRandomGeneratorTest { * TODO: verify that tolerance this wide is really OK */ @Test - public void testNextDouble() { + public void testGetAsDouble() { StableRandomGenerator generator = new StableRandomGenerator(rg, 1.3, 0.1); double[] sample = new double[2 * sampleSize]; for (int i = 0; i < sample.length; ++i) { - sample[i] = generator.nextNormalizedDouble(); + sample[i] = generator.getAsDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.3); } @@ -57,7 +57,7 @@ public void testGaussianCase() { double[] sample = new double[sampleSize]; for (int i = 0; i < sample.length; ++i) { - sample[i] = generator.nextNormalizedDouble(); + sample[i] = generator.getAsDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.02); Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02); @@ -72,7 +72,7 @@ public void testCauchyCase() { DescriptiveStatistics summary = new DescriptiveStatistics(); for (int i = 0; i < sampleSize; ++i) { - double sample = generator.nextNormalizedDouble(); + double sample = generator.getAsDouble(); summary.addValue(sample); } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UniformRandomGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UniformRandomGeneratorTest.java index 86a5efb82f..f5c986f673 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UniformRandomGeneratorTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UniformRandomGeneratorTest.java @@ -32,7 +32,7 @@ public void testMeanAndStandardDeviation() { UniformRandomGenerator generator = new UniformRandomGenerator(rg); double[] sample = new double[10000]; for (int i = 0; i < sample.length; ++i) { - sample[i] = generator.nextNormalizedDouble(); + sample[i] = generator.getAsDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.07); Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02); diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java index 0e7bf3ffac..8c06daaa8d 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java @@ -310,7 +310,7 @@ private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng return new NormalizedRandomGenerator() { /** {@inheritDoc} */ @Override - public double nextNormalizedDouble() { + public double getAsDouble() { return n.sample(); } };