From f0b21e3182a56f5424ae2924ea96f49769b0b318 Mon Sep 17 00:00:00 2001 From: David Turner Date: Mon, 18 Dec 2017 09:57:40 +0000 Subject: [PATCH] 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. --- .../main/java/org/elasticsearch/test/ESTestCase.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 84e750f6e28..d2712e268ad 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -509,12 +509,12 @@ public abstract class ESTestCase extends LuceneTestCase { return random().nextInt(); } + /** + * @return a long between 0 and Long.MAX_VALUE (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() {