initialization.go
package eaopt
import "math/rand"
// InitUnifFloat64 generates random float64s x such that lower < x < upper.
func InitUnifFloat64(n uint, 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 uint, 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 uint, 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 uint, 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 uint, 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
}