Skip to content

Commit

Permalink
Fix LiLSampling skip becoming Long.MIN_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 2, 2018
1 parent 087ab88 commit 0e76bbb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/gr/james/sampling/LiLSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ public static <E> RandomSamplingCollector<E> collector(int sampleSize, Random ra

@Override
void init(int sampleSize, Random random) {
W = Math.exp(Math.log(random.nextDouble()) / sampleSize);
W = Math.exp(Math.log(RandomSamplingUtils.randomExclusive(random)) / sampleSize);
}

@Override
long skipLength(long streamSize, int sampleSize, Random random) {
final double random1 = RandomSamplingUtils.randomExclusive(random);
final double random2 = RandomSamplingUtils.randomExclusive(random);
final long skip = (long) (Math.log(random1) / Math.log(1 - W));
long skip = (long) (Math.log(random1) / Math.log(1 - W));
assert skip >= 0 || skip == Long.MIN_VALUE;
if (skip == Long.MIN_VALUE) { // Sometimes when W is very small, 1 - W = 1 and Math.log(1) = +0 instead of -0
skip = Long.MAX_VALUE; // This results in negative infinity skip
}
W = W * Math.exp(Math.log(random2) / sampleSize);
return skip;
}
Expand Down

0 comments on commit 0e76bbb

Please sign in to comment.