Skip to content

Commit cbb0c27

Browse files
committed
update lcg
1 parent ca3be80 commit cbb0c27

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/SampleReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ static class RandomLinearCongruentialMapper {
100100
private final int randomLinearShift;
101101

102102
RandomLinearCongruentialMapper(long smaller, long larger, Random random) {
103-
assert smaller > 0 && larger > 0;
103+
assert smaller > 0 && larger > 0 : "smaller and larger must be positive; received: " + smaller + ", " + larger;
104104
assert smaller < larger;
105105
this.n = smaller;
106106
this.m = larger;
107107
this.multiplier = findLargeOddCoprime(n);
108-
this.randomLinearShift = random.nextInt(0, 1024 * 1024);
108+
this.randomLinearShift = random.nextInt(1024, 1024 * 1024) | 1;
109109
}
110110

111111
// need to ensure positive modulus only
@@ -123,9 +123,9 @@ long map(long i) {
123123
}
124124

125125
private static long findLargeOddCoprime(long n) {
126-
long candidate = n | 1; // make sure it's odd
126+
long candidate = Math.max(n | 1, 5); // make sure it's odd
127127
while (MathUtil.gcd(candidate, n) != 1) {
128-
candidate += 2;
128+
candidate += 4;
129129
}
130130
return candidate;
131131
}

server/src/test/java/org/elasticsearch/index/codec/vectors/SampleReaderTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,25 @@ public void testRandomSampling() {
3131
}
3232
}
3333

34+
public void testRandomSamplingEdges() {
35+
for (int base = 1; base < 24; base++) {
36+
int smaller = 1 << base;
37+
for (int upperBase = base + 1; upperBase <= 24; upperBase++) {
38+
int larger = 1 << upperBase;
39+
SampleReader.RandomLinearCongruentialMapper mapper = new SampleReader.RandomLinearCongruentialMapper(
40+
smaller,
41+
larger,
42+
random()
43+
);
44+
FixedBitSet valueSeen = new FixedBitSet(larger + 1);
45+
for (int i = 0; i < smaller; i++) {
46+
long mapped = mapper.map(i);
47+
assertTrue(mapped >= 0);
48+
assertTrue(mapped <= larger);
49+
assertFalse(valueSeen.getAndSet((int) mapped));
50+
}
51+
}
52+
}
53+
}
54+
3455
}

0 commit comments

Comments
 (0)