caio.co/de/go-tdigest


Add TDigest.FromBytes and cleanup RNG interface by Vladimir Mihailenco 6 years ago (log)

Blob rng.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
package tdigest

import (
	"math/rand"
)

// RNG is an interface that wraps the needed random number
// generator calls that tdigest uses during its runtime
type RNG interface {
	Float32() float32
	Intn(int) int
}

type globalRNG struct{}

func (r globalRNG) Float32() float32 {
	return rand.Float32()
}

func (r globalRNG) Intn(i int) int {
	return rand.Intn(i)
}

type localRNG struct {
	localRand *rand.Rand
}

func newLocalRNG(seed int64) *localRNG {
	return &localRNG{
		localRand: rand.New(rand.NewSource(seed)),
	}
}

func (r *localRNG) Float32() float32 {
	return r.localRand.Float32()
}

func (r *localRNG) Intn(i int) int {
	return r.localRand.Intn(i)
}