forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialization.go
52 lines (46 loc) · 1.65 KB
/
initialization.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
package gago
import "math/rand"
// InitUnifFloat64 generates random float64s x such that lower < x < upper.
func InitUnifFloat64(n int, lower, upper float64, rng *rand.Rand) (floats []float64) {
floats = make([]float64, n)
for i := range floats {
floats[i] = lower + rng.Float64()*(upper-lower)
}
return
}
// InitJaggFloat64 generates random float64s x such that lower < x < upper with jagged bounds
func InitJaggFloat64(n int, lower, upper []float64, rng *rand.Rand) (floats []float64) {
floats = make([]float64, n)
for i := range floats {
floats[i] = lower[i] + rng.Float64()*(upper[i]-lower[i])
}
return
}
// InitNormFloat64 generates random float64s sampled from a normal distribution.
func InitNormFloat64(n int, mean, std float64, rng *rand.Rand) (floats []float64) {
floats = make([]float64, n)
for i := range floats {
floats[i] = rng.NormFloat64()*std + mean
}
return
}
// InitUnifString generates random strings based on a given corpus. The strings
// are not necessarily distinct.
func InitUnifString(n int, corpus []string, rng *rand.Rand) (strings []string) {
strings = make([]string, n)
for i := range strings {
strings[i] = corpus[rng.Intn(len(corpus))]
}
return
}
// InitUniqueString generates random string slices based on a given corpus, each
// element from the corpus is only represented once in each slice. The method
// starts by shuffling, it then assigns the elements of the corpus in increasing
// index order to an individual.
func InitUniqueString(n int, corpus []string, rng *rand.Rand) (strings []string) {
strings = make([]string, n)
for i, v := range randomInts(n, 0, len(corpus), rng) {
strings[i] = corpus[v]
}
return
}