Skip to content

Commit c840f81

Browse files
committed
fix upper bound of Xorshf32b
1 parent f1eddf2 commit c840f81

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/main/java/de/tilman_neumann/jml/random/Xorshf32b.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public int nextInt() {
4444
* @return a random int number N with 0 <= N <= max.
4545
*/
4646
public int nextInt(int max) {
47-
final long i = nextInt();
48-
final long l = i<0 ? -i : i; // up to unsigned 2^31 - 1
49-
final long prod = l * max; // up to max * (2^31 - 1)
47+
final long l = nextInt() & 0xFFFFFFFFL; // take it as unsigned
48+
final long prod = l * max;
5049
return (int) (prod >>> 32);
5150
}
5251

src/test/java/de/tilman_neumann/jml/random/Xorshf32bTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testNextIntLowerUpperBound() {
8080
if (n>max) max = n;
8181
}
8282
LOG.debug(NCOUNT + " numbers from " + rng.getClass().getSimpleName() + ".nextInt(" + LOWER + ", " + UPPER + ") gave min = " + min + ", max = " + max);
83-
assertTrue(min >= LOWER);
84-
assertTrue(max <= UPPER);
83+
assertTrue("Expected " + min + " >= " + LOWER, min >= LOWER);
84+
assertTrue("Expected " + max + " <= " + UPPER, max <= UPPER);
8585
}
8686
}

0 commit comments

Comments
 (0)