forked from leesper/go_rng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weibull.go
37 lines (30 loc) · 1.04 KB
/
weibull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package rng
import (
"fmt"
"math"
)
// WeibullGenerator is a random number generator for weibull distribution.
// The zero value is invalid, use NewWeibullGenerator to create a generator
type WeibullGenerator struct {
uniform *UniformGenerator
}
// NewWeibullGenerator returns a weibull-distribution generator
// it is recommended using time.Now().UnixNano() as the seed, for example:
// wrng := rng.NewWeibullGenerator(time.Now().UnixNano())
func NewWeibullGenerator(seed int64) *WeibullGenerator {
urng := NewUniformGenerator(seed)
return &WeibullGenerator{urng}
}
// Weibull returns a random number of weibull distribution (lambda > 0.0 and k > 0.0)
func (wrng WeibullGenerator) Weibull(lambda, k float64) float64 {
if !(lambda > 0.0) {
panic(fmt.Sprintf("Invalid parameter lambda: %.2f", lambda))
}
if !(k > 0.0) {
panic(fmt.Sprintf("Invalid parameter k: %.2f", k))
}
return wrng.weibull(lambda, k)
}
func (wrng WeibullGenerator) weibull(lambda, k float64) float64 {
return lambda * math.Pow(-math.Log(1-wrng.uniform.Float64()), 1/k)
}