stat/distuv: improve Pareto.Rand performance, also adding Pareto.Rand benchmark

This commit is contained in:
Argusdusty
2021-09-19 10:41:07 -07:00
committed by Dan Kortschak
parent 2394b570ca
commit ce6167c3f2
2 changed files with 9 additions and 1 deletions

View File

@@ -104,7 +104,7 @@ func (p Pareto) Rand() float64 {
} else {
rnd = rand.New(p.Src).ExpFloat64()
}
return math.Exp(math.Log(p.Xm) + 1/p.Alpha*rnd)
return p.Xm * math.Exp(rnd/p.Alpha)
}
// StdDev returns the standard deviation of the probability distribution.

View File

@@ -204,3 +204,11 @@ func TestParetoNotExists(t *testing.T) {
t.Errorf("Expected standard deviation == +Inf for Alpha == 1, got %v", stdDev)
}
}
func BenchmarkParetoRand(b *testing.B) {
src := rand.New(rand.NewSource(1))
p := Pareto{1, 1, src}
for i := 0; i < b.N; i++ {
p.Rand()
}
}