github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/exchange/bitswap/strategy/math.go (about) 1 package strategy 2 3 import ( 4 "math" 5 "math/rand" 6 ) 7 8 type strategyFunc func(*ledger) bool 9 10 // TODO avoid using rand.Float64 method. it uses a singleton lock and may cause 11 // performance issues. Instead, instantiate a rand struct and use that to call 12 // Float64() 13 func standardStrategy(l *ledger) bool { 14 return rand.Float64() <= probabilitySend(l.Accounting.Value()) 15 } 16 17 func yesManStrategy(l *ledger) bool { 18 return true 19 } 20 21 func probabilitySend(ratio float64) float64 { 22 x := 1 + math.Exp(6-3*ratio) 23 y := 1 / x 24 return 1 - y 25 } 26 27 type debtRatio struct { 28 BytesSent uint64 29 BytesRecv uint64 30 } 31 32 func (dr *debtRatio) Value() float64 { 33 return float64(dr.BytesSent) / float64(dr.BytesRecv+1) 34 }