Make randomNonNegativeLong() draw from a uniform distribution (#27856)

Currently randomNonNegativeLong() returns 0 half as often as any positive long,
but random number generators are typically expected to return
uniformly-distributed values unless otherwise specified. This fixes this issue
by mapping Long.MIN_VALUE directly onto 0 rather than resampling.
This commit is contained in:
David Turner 2017-12-18 09:57:40 +00:00 committed by GitHub
parent 7a27a2770b
commit f0b21e3182
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 5 deletions

View File

@ -509,12 +509,12 @@ public abstract class ESTestCase extends LuceneTestCase {
return random().nextInt();
}
/**
* @return a <code>long</code> between <code>0</code> and <code>Long.MAX_VALUE</code> (inclusive) chosen uniformly at random.
*/
public static long randomNonNegativeLong() {
long randomLong;
do {
randomLong = randomLong();
} while (randomLong == Long.MIN_VALUE);
return Math.abs(randomLong);
long randomLong = randomLong();
return randomLong == Long.MIN_VALUE ? 0 : Math.abs(randomLong);
}
public static float randomFloat() {