-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipf.go
57 lines (46 loc) · 953 Bytes
/
zipf.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
package zipf
import (
"math"
"math/rand"
)
func NewZipf(r *rand.Rand, s float64, imax uint64) *Zipf {
z := &Zipf{}
z.rng = r
// convert to int for convenience, not sure any side effect
var n int = int(imax)
tmp := make([]float64, n)
for i := 1; i < n+1; i++ {
tmp[i-1] = 1.0 / math.Pow(float64(i), s)
}
zeta := make([]float64, n+1)
distMap := make([]float64, n+1)
z.prob = make([]float64, n)
zeta[0] = 0
for i := 1; i < n+1; i++ {
zeta[i] += zeta[i-1] + tmp[i-1]
}
for i := 0; i < n+1; i++ {
distMap[i] = zeta[i] / zeta[n]
}
for i := 1; i < n+1; i++ {
z.prob[i-1] = distMap[i] - distMap[i-1]
}
return z
}
type Zipf struct {
s float64
prob []float64
rng *rand.Rand
}
func (z *Zipf) Uint64() uint64 {
u := z.rng.Float64()
for i := 0; i < len(z.prob); i++ {
if u < z.prob[i] {
return uint64(i)
}
u -= z.prob[i]
}
// should not come here
// return last index for safty
return uint64(len(z.prob)-1)
}