Skip to content
Merged
5 changes: 5 additions & 0 deletions docs/changelog/114177.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 114177
summary: "Make `randomInstantBetween` always return value in range [minInstant, `maxInstant]`"
area: Infra/Metrics
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,11 @@ public static long randomLongBetween(long min, long max) {
* @return a random instant between a min and a max value with a random nanosecond precision
*/
public static Instant randomInstantBetween(Instant minInstant, Instant maxInstant) {
return Instant.ofEpochSecond(
randomLongBetween(minInstant.getEpochSecond(), maxInstant.getEpochSecond()),
randomLongBetween(0, 999999999)
);
long epochSecond = randomLongBetween(minInstant.getEpochSecond(), maxInstant.getEpochSecond());
long minNanos = epochSecond == minInstant.getEpochSecond() ? minInstant.getNano() : 0;
long maxNanos = epochSecond == maxInstant.getEpochSecond() ? maxInstant.getNano() : 999999999;
long nanos = randomLongBetween(minNanos, maxNanos);
return Instant.ofEpochSecond(epochSecond, nanos);
}

/**
Expand Down