forked from DataDog/sketches-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
67 lines (46 loc) · 1.73 KB
/
generator.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021 Datadog, Inc.
package dataset
import (
"math"
"math/rand"
)
type Generator interface {
Generate() float64
}
// Constant stream
type Constant struct{ constant float64 }
func NewConstant(constant float64) *Constant { return &Constant{constant: constant} }
func (s *Constant) Generate() float64 { return s.constant }
// Linearly increasing stream
type Linear struct{ currentVal float64 }
func NewLinear() *Linear { return &Linear{0} }
func (g *Linear) Generate() float64 {
value := g.currentVal
g.currentVal++
return value
}
// Normal distribution
type Normal struct{ mean, stddev float64 }
func NewNormal(mean, stddev float64) *Normal { return &Normal{mean: mean, stddev: stddev} }
func (g *Normal) Generate() float64 { return rand.NormFloat64()*g.stddev + g.mean }
// Lognormal distribution
type Lognormal struct{ mu, sigma float64 }
func NewLognormal(mu, sigma float64) *Lognormal { return &Lognormal{mu: mu, sigma: sigma} }
func (g *Lognormal) Generate() float64 {
r := rand.NormFloat64()
return math.Exp(r*g.sigma + g.mu)
}
// Exponential distribution
type Exponential struct{ rate float64 }
func NewExponential(rate float64) *Exponential { return &Exponential{rate: rate} }
func (g *Exponential) Generate() float64 { return rand.ExpFloat64() / g.rate }
// Pareto distribution
type Pareto struct{ shape, scale float64 }
func NewPareto(shape, scale float64) *Pareto { return &Pareto{shape: shape, scale: scale} }
func (g *Pareto) Generate() float64 {
r := rand.ExpFloat64() / g.shape
return math.Exp(math.Log(g.scale) + r)
}