From 8872d7cbd3c1d844cbea4593a31ba5fb3845bf01 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 21 Feb 2021 20:47:01 +0800 Subject: [PATCH] fix golint issues in core/mathx (#498) --- core/mathx/entropy.go | 1 + core/mathx/int.go | 2 ++ core/mathx/proba.go | 3 +++ core/mathx/unstable.go | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/core/mathx/entropy.go b/core/mathx/entropy.go index e1b589b8..fd3f2851 100644 --- a/core/mathx/entropy.go +++ b/core/mathx/entropy.go @@ -4,6 +4,7 @@ import "math" const epsilon = 1e-6 +// CalcEntropy calculates the entropy of m. func CalcEntropy(m map[interface{}]int) float64 { if len(m) == 0 || len(m) == 1 { return 1 diff --git a/core/mathx/int.go b/core/mathx/int.go index d2fb4274..4c1fe63e 100644 --- a/core/mathx/int.go +++ b/core/mathx/int.go @@ -1,5 +1,6 @@ package mathx +// MaxInt returns the larger one of a and b. func MaxInt(a, b int) int { if a > b { return a @@ -8,6 +9,7 @@ func MaxInt(a, b int) int { return b } +// MinInt returns the smaller one of a and b. func MinInt(a, b int) int { if a < b { return a diff --git a/core/mathx/proba.go b/core/mathx/proba.go index 9db6f9d9..56e79db7 100644 --- a/core/mathx/proba.go +++ b/core/mathx/proba.go @@ -6,18 +6,21 @@ import ( "time" ) +// A Proba is used to test if true on given probability. type Proba struct { // rand.New(...) returns a non thread safe object r *rand.Rand lock sync.Mutex } +// NewProba returns a Proba. func NewProba() *Proba { return &Proba{ r: rand.New(rand.NewSource(time.Now().UnixNano())), } } +// TrueOnProba checks if true on given probability. func (p *Proba) TrueOnProba(proba float64) (truth bool) { p.lock.Lock() truth = p.r.Float64() < proba diff --git a/core/mathx/unstable.go b/core/mathx/unstable.go index 198568d7..23eccc39 100644 --- a/core/mathx/unstable.go +++ b/core/mathx/unstable.go @@ -6,12 +6,14 @@ import ( "time" ) +// A Unstable is used to generate random value around the mean value base on given deviation. type Unstable struct { deviation float64 r *rand.Rand lock *sync.Mutex } +// NewUnstable returns a Unstable. func NewUnstable(deviation float64) Unstable { if 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 { u.lock.Lock() 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 } +// AroundInt returns a randome int64 with given base and deviation. func (u Unstable) AroundInt(base int64) int64 { u.lock.Lock() val := int64((1 + u.deviation - 2*u.deviation*u.r.Float64()) * float64(base))