Skip to content

Commit

Permalink
Add NonNegative flag to Uniform jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed May 20, 2019
1 parent 0ac5086 commit d079099
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d079099

Please sign in to comment.