package module
Version: v2.0.0+incompatible Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2020 License: MIT Imports: 2 Imported by: 8



Tickers with random jitter

Godoc Reference Go Report Card


go get -u github.com/lthibault/jitterbug


Jitterbug is used by instantiating a jitterbug.Ticker with an interval and a jitterbug.Jitter. The former specifies a baseline interval for the ticker, to which a jitter is added by the latter.

package main

import (


func main() {
    t := jitterbug.New(
        time.Millisecond * 300,
        &jitterbug.Norm{ Stdev: time.Millisecond * 100 },

    // jitterbug.Ticker behaves like time.Ticker
    for tick := <- range t.C {

Jitterbug is compatible with the univariate distributions from GoNum. For example:

t := jitterbug.New(
    time.Millisecond * 300,
        Sampler: &distruv.Gamma{
            // Tip: cast time.Duration as float64 when using gonum's distruv
            Alpha: float64(time.Millisecond * 100),
            Beta:  float64(time.Millisecond * 200),

Compatible libraries


If you find this useful please let me know: l.thibault@sentimens.com

Seriously, even if you just used it in your weekend project, I'd like to hear about it :)




This section is empty.


This section is empty.


This section is empty.


type Jitter

type Jitter interface {
	// Jitter consumes an interval from a ticker and returns the final, jittered
	// duration.
	Jitter(time.Duration) time.Duration

Jitter can compute a jitter

type Norm

type Norm struct {
	Source      *rand.Rand
	Mean, Stdev time.Duration

Norm is a normal distribution

func (Norm) Jitter

func (n Norm) Jitter(d time.Duration) time.Duration

Jitter the duration by drawing form a normal distribution

type Sampler

type Sampler interface {
	Rand() float64

Sampler can sample from any univariate distribution. It is used in conjunction with the Univariate type and is compatible with GoNum: https://godoc.org/gonum.org/v1/gonum/stat/distuv.

type Ticker

type Ticker struct {
	C <-chan time.Time

	Interval time.Duration
	// contains filtered or unexported fields

Ticker behaves like time.Ticker

func New

func New(d time.Duration, j Jitter) (t *Ticker)

New Ticker with the base interval d and the jitter source j.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop the Ticker

type Uniform

type Uniform struct {
	Source *rand.Rand
	Min    time.Duration

Uniform distribution

func (Uniform) Jitter

func (u Uniform) Jitter(d time.Duration) time.Duration

Jitter the duration by drawing from a uniform distribution

type Univariate

type Univariate struct {

Univariate distribution

func (Univariate) Jitter

func (u Univariate) Jitter(d time.Duration) time.Duration

Jitter the duration by drawing from a univariate distribution

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL