Skip to content

Commit 3f404fd

Browse files
committed
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.
1 parent f7b55d0 commit 3f404fd

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ public static int randomInt() {
509509
return random().nextInt();
510510
}
511511

512+
/**
513+
* @return a <code>long</code> between <code>0</code> and <code>Long.MAX_VALUE</code> (inclusive) chosen uniformly at random.
514+
*/
512515
public static long randomNonNegativeLong() {
513-
long randomLong;
514-
do {
515-
randomLong = randomLong();
516-
} while (randomLong == Long.MIN_VALUE);
517-
return Math.abs(randomLong);
516+
long randomLong = randomLong();
517+
return randomLong == Long.MIN_VALUE ? 0 : Math.abs(randomLong);
518518
}
519519

520520
public static float randomFloat() {

0 commit comments

Comments
 (0)