Skip to content

Commit 654fa37

Browse files
committed
rng clean up
1 parent b92c400 commit 654fa37

File tree

7 files changed

+21
-342
lines changed

7 files changed

+21
-342
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ public class Xorshf32 {
2525

2626
private int x=123456789, y=362436069, z=521288629;
2727

28+
/**
29+
* @return a random long number N with Integer.MIN_VALUE <= N <= Integer.MAX_VALUE.
30+
*/
2831
public int nextInt() {
29-
return nextInt(Integer.MAX_VALUE);
30-
}
31-
32-
public int nextInt(int max) {
3332
int t;
3433
x ^= x << 16;
3534
x ^= x >> 5;
@@ -40,9 +39,19 @@ public int nextInt(int max) {
4039
y = z;
4140
z = t ^ x ^ y;
4241

43-
return Math.abs(z % max);
42+
return z;
4443
}
45-
44+
45+
/**
46+
* @param max
47+
* @return a random int number N with 0 <= N <= max.
48+
*/
49+
public int nextInt(int max) {
50+
final long l = nextInt() & 0xFFFFFFFFL; // take it as unsigned
51+
final long prod = l * max;
52+
return (int) (prod >>> 32);
53+
}
54+
4655
public int nextInt(int min, int max) {
4756
return min + nextInt(max - min);
4857
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ public class RngPerformanceTest {
4343
private static final long LOWER_LONG = 1L<<20;
4444
private static final long UPPER_LONG = 1L<<50;
4545

46+
// any-size generators
4647
private static final SecureRandom secureRandom = new SecureRandom();
4748
private static final Random random = new Random();
4849
private static final Rng rng = new Rng();
50+
51+
// pure int generators
4952
private static final SpRand32 spRand32 = new SpRand32();
5053
private static final Xorshf32 xorshf32 = new Xorshf32();
51-
private static final Xorshf32b xorshf32b = new Xorshf32b();
54+
55+
// pure long generators
5256
private static final LehmerRng64 lehmer64 = new LehmerRng64();
5357
private static final LehmerRng64MH lehmer64MH = new LehmerRng64MH();
54-
private static final Xorshf64 xorshf64 = new Xorshf64();
5558
private static final SplitMix64 splitMix64 = new SplitMix64();
5659
private static final Xorshift64Star xorshift64Star = new Xorshift64Star();
5760
private static final Xorshift128Plus xorshift128Plus = new Xorshift128Plus();
@@ -82,10 +85,6 @@ private static void testNextInt() {
8285
xorshf32.nextInt();
8386
}
8487
LOG.debug("Xorshf32.nextInt() took " + timer.capture() + " ms");
85-
for (int i=0; i<NCOUNT; i++) {
86-
xorshf32b.nextInt();
87-
}
88-
LOG.debug("Xorshf32b.nextInt() took " + timer.capture() + " ms");
8988
}
9089

9190
private static void testNextIntWithUpperBound() {
@@ -112,10 +111,6 @@ private static void testNextIntWithUpperBound() {
112111
xorshf32.nextInt(UPPER_INT);
113112
}
114113
LOG.debug("Xorshf32.nextInt(" + UPPER_INT + ") took " + timer.capture() + " ms");
115-
for (int i=0; i<NCOUNT; i++) {
116-
xorshf32b.nextInt(UPPER_INT);
117-
}
118-
LOG.debug("Xorshf32b.nextInt(" + UPPER_INT + ") took " + timer.capture() + " ms");
119114
}
120115

121116
private static void testNextIntWithLowerUpperBound() {
@@ -144,10 +139,6 @@ private static void testNextIntWithLowerUpperBound() {
144139
xorshf32.nextInt(LOWER_INT, UPPER_INT);
145140
}
146141
LOG.debug("Xorshf32.nextInt(" + LOWER_INT + ", " + UPPER_INT + ") took " + timer.capture() + " ms");
147-
for (int i=0; i<NCOUNT; i++) {
148-
xorshf32b.nextInt(LOWER_INT, UPPER_INT);
149-
}
150-
LOG.debug("Xorshf32b.nextInt(" + LOWER_INT + ", " + UPPER_INT + ") took " + timer.capture() + " ms");
151142
}
152143

153144
private static void testNextLong() {
@@ -174,10 +165,6 @@ private static void testNextLong() {
174165
lehmer64MH.nextLong();
175166
}
176167
LOG.debug("LehmerRng64MH.nextLong() took " + timer.capture() + " ms");
177-
for (int i=0; i<NCOUNT; i++) {
178-
xorshf64.nextLong();
179-
}
180-
LOG.debug("Xorshf64.nextLong() took " + timer.capture() + " ms");
181168
for (int i=0; i<NCOUNT; i++) {
182169
splitMix64.nextLong();
183170
}
@@ -202,10 +189,6 @@ private static void testNextLong() {
202189

203190
private static void testNextLongWithUpperBound() {
204191
Timer timer = new Timer();
205-
for (int i=0; i<NCOUNT; i++) {
206-
xorshf64.nextLong(UPPER_LONG);
207-
}
208-
LOG.debug("Xorshf64.nextLong(" + UPPER_LONG + ") took " + timer.capture() + " ms");
209192
for (int i=0; i<NCOUNT; i++) {
210193
splitMix64.nextLong(UPPER_LONG);
211194
}
@@ -230,10 +213,6 @@ private static void testNextLongWithUpperBound() {
230213

231214
private static void testNextLongWithLowerUpperBound() {
232215
Timer timer = new Timer();
233-
for (int i=0; i<NCOUNT; i++) {
234-
xorshf64.nextLong(LOWER_LONG, UPPER_LONG);
235-
}
236-
LOG.debug("Xorshf64.nextLong(" + LOWER_LONG + ", " + UPPER_LONG + ") took " + timer.capture() + " ms");
237216
for (int i=0; i<NCOUNT; i++) {
238217
splitMix64.nextLong(LOWER_LONG, UPPER_LONG);
239218
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testNextIntNoArgs() {
5757
}
5858
LOG.debug("First 100 elements: " + Arrays.toString(firstElements));
5959
LOG.debug(NCOUNT + " numbers from " + rng.getClass().getSimpleName() + ".nextInt() gave min = " + min + ", max = " + max);
60-
assertTrue(min >= 0);
60+
assertTrue(min < 0);
6161
assertTrue(generatesEven);
6262
assertTrue(generatesOdd);
6363
}

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

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)