go-chart/sequence/generate.go
Will Charczuk 814470733e snapshot.
2017-04-29 21:12:39 -07:00

61 lines
1.4 KiB
Go

package sequence
import (
"math/rand"
"time"
)
var (
// Generate contains some sequence generation utilities.
// These utilities can be useful for generating test data.
Generate = &generate{
rnd: rand.New(rand.NewSource(time.Now().Unix())),
}
)
type generate struct {
rnd *rand.Rand
}
// Values produces an array of floats from [start,end] by optional steps.
func (g generate) Values(start, end float64, steps ...float64) Seq {
var values []float64
step := 1.0
if len(steps) > 0 {
step = steps[0]
}
if start < end {
for x := start; x <= end; x += step {
values = append(values, x)
}
} else {
for x := start; x >= end; x = x - step {
values = append(values, x)
}
}
return Seq{Array(values)}
}
// Random generates a fixed length sequence of random values between (0, scale).
func (g generate) RandomValues(samples int, scale float64) Seq {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
values[x] = g.rnd.Float64() * scale
}
return Seq{Array(values)}
}
// Random generates a fixed length sequence of random values with a given average, above and below that average by (-scale, scale)
func (g generate) RandomValuesWithAverage(samples int, average, scale float64) Seq {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
jitter := scale - (g.rnd.Float64() * (2 * scale))
values[x] = average + jitter
}
return Seq{Array(values)}
}