Skip to content

Commit

Permalink
Unbias the uniform distribution (#5)
Browse files Browse the repository at this point in the history
Fix #4
* de-bias the uniform distribution
* throw panic if duration <= min
  • Loading branch information
zenhorace authored Mar 12, 2021
1 parent 37ff5f4 commit 7edacbc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ type Uniform struct {
}

// Jitter the duration by drawing from a uniform distribution
// over the range [Min, d). Panics if d <= Min
func (u Uniform) Jitter(d time.Duration) time.Duration {
drawUniform := rand.Int63n
if u.Source != nil {
drawUniform = u.Source.Int63n
}

d = time.Duration(drawUniform(int64(d)))
return max(d, u.Min)
delta := d - u.Min
if delta <= 0 {
panic("duration must exceed min")
}
return u.Min + time.Duration(drawUniform(int64(delta)))
}

0 comments on commit 7edacbc

Please sign in to comment.