This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git


The following commit(s) were added to refs/heads/master by this push:
     new e391c9380 Reject bases below 2 in HaltonSequenceGenerator (#355)
e391c9380 is described below

commit e391c938086aff7da00897f003bf685615e78da2
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:40:21 2026 +0530

    Reject bases below 2 in HaltonSequenceGenerator (#355)
---
 .../legacy/random/HaltonSequenceGenerator.java     | 13 +++++++++++--
 .../legacy/random/HaltonSequenceGeneratorTest.java | 22 ++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

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 java.util.function.Supplier;
 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 class HaltonSequenceGenerator implements 
Supplier<double[]> {
      * The length of the bases array defines the space dimension and is 
required to be &gt; 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 class HaltonSequenceGenerator implements 
Supplier<double[]> {
         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.junit.Assert;
 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 class HaltonSequenceGeneratorTest {
         } 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

Reply via email to