diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java index 27337a3f5..cc97a632e 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java @@ -21,6 +21,7 @@ import org.apache.commons.math4.legacy.exception.DimensionMismatchException; import org.apache.commons.math4.legacy.exception.NotPositiveException; import org.apache.commons.math4.legacy.exception.NullArgumentException; +import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException; import org.apache.commons.math4.legacy.exception.OutOfRangeException; /** @@ -95,18 +96,20 @@ public HaltonSequenceGenerator(final int dimension) { * The length of the bases array defines the space dimension and is required to be > 0. * * @param dimension the space dimension - * @param bases the base number for each dimension, entries should be (pairwise) prime, may not be null + * @param bases the base number for each dimension, entries should be (pairwise) prime and at least 2, + * may not be null * @param weights the weights used during scrambling, may be null in which case no scrambling will be performed * @throws NullArgumentException if base is null * @throws OutOfRangeException if the space dimension is outside the range [1, len], where * len refers to the length of the bases array * @throws DimensionMismatchException if weights is non-null and the length of the input arrays differ + * @throws NumberIsTooSmallException if any of the bases is smaller than 2 */ public HaltonSequenceGenerator(final int dimension, final int[] bases, final int[] weights) { NullArgumentException.check(bases); if (dimension < 1 || dimension > bases.length) { - throw new OutOfRangeException(dimension, 1, PRIMES.length); + throw new OutOfRangeException(dimension, 1, bases.length); } if (weights != null && weights.length != bases.length) { @@ -117,6 +120,12 @@ public HaltonSequenceGenerator(final int dimension, final int[] bases, final int this.base = bases.clone(); this.weight = weights == null ? null : weights.clone(); count = 0; + + for (final int b : base) { + if (b < 2) { + throw new NumberIsTooSmallException(b, 2, true); + } + } } /** {@inheritDoc} */ diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java index c1931f517..70295f453 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java @@ -20,6 +20,7 @@ import org.apache.commons.math4.legacy.exception.DimensionMismatchException; import org.apache.commons.math4.legacy.exception.NotPositiveException; import org.apache.commons.math4.legacy.exception.NullArgumentException; +import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException; import org.apache.commons.math4.legacy.exception.OutOfRangeException; import org.junit.Before; import org.junit.Test; @@ -117,6 +118,27 @@ public void testConstructor2() throws Exception{ } catch (DimensionMismatchException e) { // expected } + + try { + new HaltonSequenceGenerator(3, new int[] { 2, 3 }, null); + Assert.fail("an exception should have been thrown"); + } catch (OutOfRangeException e) { + Assert.assertEquals(2, e.getHi()); + } + } + + @Test + public void testConstructorInvalidBase() { + // A base of 1 never reduces the index (infinite loop), 0 divides by zero + // and a negative base yields points outside [0, 1]. + for (int b : new int[] { 1, 0, -2 }) { + try { + new HaltonSequenceGenerator(2, new int[] { 2, b }, null); + Assert.fail("an exception should have been thrown for base " + b); + } catch (NumberIsTooSmallException e) { + // expected + } + } } @Test