From d079099d6698901a43e8b9aa0301e260e2f177ba Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Mon, 20 May 2019 11:04:17 +0100 Subject: [PATCH] Add NonNegative flag to `Uniform` jitter --- uniform.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/uniform.go b/uniform.go index 1cb6c68..dfe1f98 100644 --- a/uniform.go +++ b/uniform.go @@ -9,6 +9,9 @@ import ( type Uniform struct { Source *rand.Rand Limit time.Duration + + // If true, only a positive jitter will be added to the base duration + NonNegative bool } // Jitter the duration by drawing from a uniform distribution @@ -18,15 +21,17 @@ func (u Uniform) Jitter(d time.Duration) time.Duration { f = u.Source.Int63n } - sign := rand.Intn - if u.Source != nil { - sign = u.Source.Intn - } - samp := time.Duration(f(int64(u.Limit))) - if sign(1) == 0 { - return d - samp + if u.NonNegative { + sign := rand.Intn + if u.Source != nil { + sign = u.Source.Intn + } + + if sign(1) == 0 { + samp = -samp + } } return d + samp