Skip to content

Commit 8253623

Browse files
committed
Avoid rejection bound that invalidates the test in ArraySamplerTest
1 parent 64a35a3 commit 8253623

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ArraySamplerTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,24 @@ public long nextLong() {
577577
};
578578

579579
for (int i = 0; i < 100; i++) {
580-
// Avoid an index bound of 0 or 1 by adding 2
581-
final int n = 2 + rng.nextInt(ABOVE_BATCH_LIMIT);
582-
final int[] bound1 = {n * (n - 1)};
580+
// Create the bound.
581+
// The random output of zero will only be rejected if
582+
// unused bits < (2^32 % bound).
583+
// Ensure this threshold is above zero
584+
int n = 0;
585+
int bound = 1;
586+
while ((1L << 32) % bound == 0) {
587+
// Avoid an index bound of 0 or 1
588+
n = rng.nextInt(2, ABOVE_BATCH_LIMIT);
589+
bound = n * (n - 1);
590+
}
591+
final int[] bound1 = {bound};
583592
final int[] i1 = ArraySampler.randomBounded2(n, n - 1, bound1, rng1);
584-
final int[] bound2 = {n * (n - 1)};
593+
final int[] bound2 = {bound};
585594
final int[] i2 = ArraySampler.randomBounded2(n, n - 1, bound2, rng2);
586-
Assertions.assertArrayEquals(i1, i2, "indices");
587-
Assertions.assertArrayEquals(bound1, bound2, "bounds");
595+
final int nn = n;
596+
Assertions.assertArrayEquals(i1, i2, () -> "indices: n=" + nn);
597+
Assertions.assertArrayEquals(bound1, bound2, () -> "bounds: n=" + nn);
588598
}
589599
}
590600

0 commit comments

Comments
 (0)