fix golint issues in core/mathx (#498)

master
Kevin Wan 4 years ago committed by GitHub
parent 334ee4213f
commit 8872d7cbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ import "math"
const epsilon = 1e-6 const epsilon = 1e-6
// CalcEntropy calculates the entropy of m.
func CalcEntropy(m map[interface{}]int) float64 { func CalcEntropy(m map[interface{}]int) float64 {
if len(m) == 0 || len(m) == 1 { if len(m) == 0 || len(m) == 1 {
return 1 return 1

@ -1,5 +1,6 @@
package mathx package mathx
// MaxInt returns the larger one of a and b.
func MaxInt(a, b int) int { func MaxInt(a, b int) int {
if a > b { if a > b {
return a return a
@ -8,6 +9,7 @@ func MaxInt(a, b int) int {
return b return b
} }
// MinInt returns the smaller one of a and b.
func MinInt(a, b int) int { func MinInt(a, b int) int {
if a < b { if a < b {
return a return a

@ -6,18 +6,21 @@ import (
"time" "time"
) )
// A Proba is used to test if true on given probability.
type Proba struct { type Proba struct {
// rand.New(...) returns a non thread safe object // rand.New(...) returns a non thread safe object
r *rand.Rand r *rand.Rand
lock sync.Mutex lock sync.Mutex
} }
// NewProba returns a Proba.
func NewProba() *Proba { func NewProba() *Proba {
return &Proba{ return &Proba{
r: rand.New(rand.NewSource(time.Now().UnixNano())), r: rand.New(rand.NewSource(time.Now().UnixNano())),
} }
} }
// TrueOnProba checks if true on given probability.
func (p *Proba) TrueOnProba(proba float64) (truth bool) { func (p *Proba) TrueOnProba(proba float64) (truth bool) {
p.lock.Lock() p.lock.Lock()
truth = p.r.Float64() < proba truth = p.r.Float64() < proba

@ -6,12 +6,14 @@ import (
"time" "time"
) )
// A Unstable is used to generate random value around the mean value base on given deviation.
type Unstable struct { type Unstable struct {
deviation float64 deviation float64
r *rand.Rand r *rand.Rand
lock *sync.Mutex lock *sync.Mutex
} }
// NewUnstable returns a Unstable.
func NewUnstable(deviation float64) Unstable { func NewUnstable(deviation float64) Unstable {
if deviation < 0 { if deviation < 0 {
deviation = 0 deviation = 0
@ -26,6 +28,7 @@ func NewUnstable(deviation float64) Unstable {
} }
} }
// AroundDuration returns a random duration with given base and deviation.
func (u Unstable) AroundDuration(base time.Duration) time.Duration { func (u Unstable) AroundDuration(base time.Duration) time.Duration {
u.lock.Lock() u.lock.Lock()
val := time.Duration((1 + u.deviation - 2*u.deviation*u.r.Float64()) * float64(base)) val := time.Duration((1 + u.deviation - 2*u.deviation*u.r.Float64()) * float64(base))
@ -33,6 +36,7 @@ func (u Unstable) AroundDuration(base time.Duration) time.Duration {
return val return val
} }
// AroundInt returns a randome int64 with given base and deviation.
func (u Unstable) AroundInt(base int64) int64 { func (u Unstable) AroundInt(base int64) int64 {
u.lock.Lock() u.lock.Lock()
val := int64((1 + u.deviation - 2*u.deviation*u.r.Float64()) * float64(base)) val := int64((1 + u.deviation - 2*u.deviation*u.r.Float64()) * float64(base))

Loading…
Cancel
Save