-
Notifications
You must be signed in to change notification settings - Fork 0
/
croupier.go
74 lines (58 loc) · 1.38 KB
/
croupier.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
68
69
70
71
72
73
74
package croupier
import "math/rand"
// Given a distribution of float64 numbers, returns a sample of a given size
func SampleFloat64(size int, dist chan float64) []float64 {
arr := make([]float64, size)
for i := 0; i < size; i++ {
arr = append(arr, <-dist)
}
return arr
}
// Maps a distribution with a given fn
func MapDistributionFloat64(dist chan float64, fn func(float64) float64) chan float64 {
out := make(chan float64)
go func() {
for {
num, ok := <-dist
if !ok {
break
}
out <- fn(num)
}
close(out)
}()
return out
}
// Returns a distribution of floats in [0.0, 1.0)
func NewUniform() chan float64 {
dist := make(chan float64)
go func() {
for {
dist <- rand.Float64()
}
}()
return dist
}
// Returns a distribution of floats in [min, max)
func NewUniformInRange(min, max float64) chan float64 {
distRange := max - min
return MapDistributionFloat64(NewUniform(), func(num float64) float64 {
return num*distRange + min
})
}
// Returns a new normal distribution, mean 0 and stddev 1
func NewNormal() chan float64 {
dist := make(chan float64)
go func() {
for {
dist <- rand.NormFloat64()
}
}()
return dist
}
// Returns a new normal distribution with given means and stddev
func NewCustomNormal(mean, stddev float64) chan float64 {
return MapDistributionFloat64(NewNormal(), func(num float64) float64 {
return num*stddev + mean
})
}