github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/random_jitter.go (about)

     1  package libkb
     2  
     3  import (
     4  	cryptorand "crypto/rand"
     5  	"math/big"
     6  	"time"
     7  )
     8  
     9  // RandomJitter takes a duration of d, and output a duration uniformly
    10  // and randomly distributed in [.5d, 1.5d].
    11  func RandomJitter(d time.Duration) time.Duration {
    12  	r := int64(100000)
    13  	nBig, err := cryptorand.Int(cryptorand.Reader, big.NewInt(r))
    14  	if err != nil {
    15  		return d
    16  	}
    17  	x := int64(d)
    18  	n := nBig.Int64()
    19  
    20  	return time.Duration(3*x/2 - x*n/r)
    21  }