go-chart/sequence/generate.go

62 lines
1.4 KiB
Go
Raw Normal View History

2017-04-30 00:12:39 -04:00
package sequence
2017-04-28 19:07:36 -04:00
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
}
2017-04-29 16:19:17 -04:00
// Values produces an array of floats from [start,end] by optional steps.
2017-04-30 00:12:39 -04:00
func (g generate) Values(start, end float64, steps ...float64) Seq {
2017-04-28 19:07:36 -04:00
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)
}
}
2017-04-30 00:12:39 -04:00
return Seq{Array(values)}
2017-04-28 19:07:36 -04:00
}
// Random generates a fixed length sequence of random values between (0, scale).
2017-04-30 00:12:39 -04:00
func (g generate) RandomValues(samples int, scale float64) Seq {
2017-04-28 19:07:36 -04:00
values := make([]float64, samples)
for x := 0; x < samples; x++ {
values[x] = g.rnd.Float64() * scale
}
2017-04-30 00:12:39 -04:00
return Seq{Array(values)}
2017-04-28 19:07:36 -04:00
}
// Random generates a fixed length sequence of random values with a given average, above and below that average by (-scale, scale)
2017-04-30 00:12:39 -04:00
func (g generate) RandomValuesWithAverage(samples int, average, scale float64) Seq {
2017-04-28 19:07:36 -04:00
values := make([]float64, samples)
for x := 0; x < samples; x++ {
jitter := scale - (g.rnd.Float64() * (2 * scale))
values[x] = average + jitter
}
2017-04-30 00:12:39 -04:00
return Seq{Array(values)}
2017-04-28 19:07:36 -04:00
}